我正在将使用Spring实现的Maven项目迁移到Spring Boot 1.5.20.RELEASE。
我有一个适配器类,它将由所有带注释的类扩展,@RestControllers以保证与前端的向后兼容性
public class RestControllerAdapter {
private MessageTemplate messageTemplate;
private MessageTemplate getMessageTemplate() {
if (messageTemplate == null) {
messageTemplate = ApplicationContextUtils.getBean(MessageTemplate.class);
}
return messageTemplate;
}
protected final String message(@NonNull String code) {
return getMessageTemplate().getMessage(code);
}
protected final String message(@NonNull String code, Object... args) {
return getMessageTemplate().getMessage(code, args);
}
protected final ModelMap success() {
val map = new ModelMap();
map.put("success", true);
map.put("message", getMessageTemplate().getMessage("message.success.default"));
return map;
}
protected final ModelMap error(@NonNull String message) {
val map = new ModelMap(2);
map.put("success", false);
map.put("error", message);
return map;
}
protected final ModelMap retry(@NonNull Exception ex) {
val map = new ModelMap(3);
map.put("success", false);
map.put("error", ex.getMessage());
map.put("confirmar", true);
return map;
}
protected final ModelMap empty() {
return new ModelMap();
}
@ExceptionHandler(JpaSystemException.class)
public ModelMap handleJpaSystemException(JpaSystemException ex) {
log.error(ex.getMessage(), ex);
return createError(ex.getMostSpecificCause());
}
@ExceptionHandler(DataIntegrityViolationException.class)
public ModelMap handleDataIntegrityViolationException(DataIntegrityViolationException ex) {
log.error(ex.getMessage(), ex);
return createError(ex.getMostSpecificCause());
}
}
慕村225694
相关分类