Spring MVC 异常响应编码设置

直接代码说话

1
2
3
4
5
6
7
@ExceptionHandler(HumanReadableException.class)
public ResponseEntity<String> handleHumanReadableException(Exception e) {
logger.error("请求处理出错", e);
HttpHeaders headers = new HttpHeaders();
headers.setContentType(new MediaType("text", "plain", Charset.forName("UTF-8")));
return new ResponseEntity<>(e.getMessage(), headers, HttpStatus.INTERNAL_SERVER_ERROR);
}