前言
springmvc在跳转@ExceptionHandler的处理中,setContentType一直无效,试过各种拦截器过滤器,都无效
,然后看了下这篇文章https://www.cnblogs.com/kaiblog/p/7565231.html,原来springmvc会重新处理一遍content-type
解决方法
返回ResponseEntity,并且设置content-type头
@ExceptionHandler({ CustomException.class})
public ResponseEntity<ResponseBean> exceptionHandler(Exception e, HttpServletResponse response) throws IOException {
HttpHeaders headers = new HttpHeaders();
headers.add("Content-Type", MediaType.APPLICATION_JSON_UTF8_VALUE);
return new ResponseEntity<>(ResponseBean.error(e.getMessage()),headers, HttpStatus.OK);
}