<dependency> <groupId>com.alibaba</groupId> <artifactId>fastjson</artifactId> <version>1.2.31</version> </dependency>
@Configuration public class SpringMVCConfig extends WebMvcConfigurerAdapter{ @Override public void configureMessageConverters(List<HttpMessageConverter<?>> converters) { FastJsonHttpMessageConverter4 fastJsonConverter = new FastJsonHttpMessageConverter4(); converters.add(fastJsonConverter); } }
使用fastjson中註解@JSONFieldhtml
@JSONField(format = "yyyy-MM-dd HH:mm:ss") private Date orderTime;
{
"id": 1,
"orderList": [
{
"id": 3,
"orderMoney": 998,
"orderTime": "2017-08-19 10:22:44",
"userId": 1
}
],
"userName": "w"
}java
補充:app
改用@ControllerAdviceide
繼承WebMvcConfigurerAdapter覆寫configureHandlerExceptionResolvers方法spa
@Override public void configureHandlerExceptionResolvers(List<HandlerExceptionResolver> exceptionResolvers) { exceptionResolvers.add(new HandlerExceptionResolver() { @Override public ModelAndView resolveException(HttpServletRequest request, HttpServletResponse response, Object handler, Exception ex) { Result result = new Result(); if (ex instanceof BizException){ BizException bizException = (BizException)ex; result.setCode(bizException.getErrorCode()); result.setMessaage(bizException.getMessage()); }else{ result.setCode(-1); result.setMessaage("系統異常,請聯繫管理員"); } response.setCharacterEncoding("UTF-8"); response.setHeader("Content-type", "application/json;charset=UTF-8"); try { response.getWriter().write(JSON.toJSONString(result)); } catch (IOException e) { LOG.info("異常處理獲取輸出流異常",e); e.printStackTrace(); } return new ModelAndView(); } }); }
{"code":1,"messaage":"業務校驗不經過"}