更新时间:2023-11-17 22:08:10
我通过执行以下操作解决了这个问题(我认为这是 Spring Boot 问题).
I went around this issue (I would think that is a Spring Boot issue) by doing the following.
独立的 Rest 和 Mvc 控制器在此处查看我的问题:Spring MVC:在@ExceptionHandler 上的@RequestStatus 中获取 i18n 消息的原因
注入 Jackson 转换器并自己编写响应:
Inject Jackson converter and write response myself :
@ControllerAdvice(annotations = RestController.class)
@Priority(1)
@ResponseBody
public class RestControllerAdvice {
@Autowired
private MappingJackson2HttpMessageConverter jacksonMessageConverter;
@ExceptionHandler(RuntimeException.class)
@ResponseStatus(value = HttpStatus.BAD_REQUEST)
public void handleRuntimeException(HttpServletRequest request, HttpServletResponse response, RuntimeException exception) {
try {
jacksonMessageConverter.write(new MyRestResult(translateMessage(exception)), MediaType.APPLICATION_JSON, new ServletServerHttpResponse(response));
response.flushBuffer(); // Flush to commit the response
} catch (IOException e) {
e.printStackTrace();
}
}
}