1.在类上使用注解
@RestController public class HttpComentInterface { }
2.在方法是使用注解 @ResponseBody
@RequestMapping(path = "/interface/queryRemote", method = RequestMethod.POST) //可以指定请求方式 @ResponseBody public RemoteCommentResultData queryCommentPage(@RequestBody QueryCommentParamDTO queryCommentParamDTO, HttpServletRequest request) { }
3.使用 out
@RequestMapping(value ="/GetSomeInfos" ,produces = "application/json;charset=UTF-8") public ModelAndView getNearestAirport(HttpServletRequest request, HttpServletResponse response)throws Exception { logger.info("NearestAirportController.getNearestAirport begin"); RemoteAirportsData data = nearestAirportService.getNearestAirports(); response.setContentType("text/html;charset=UTF-8"); response.setCharacterEncoding("UTF-8"); String result = JSONObject.fromObject(data).toString(); response.getWriter().print(result); logger.info("NearestAirportController.getNearestAirport end"); return null; }
4.如果出现了 status 406 则是项目中缺少 jackson-core-asl-1.9.12.jar,jackson-mapper-asl-1.9.12.jar 这两个包
引起的原因:
由于设置了@ResponseBody,要把对象转换成json格式,缺少转换依赖的jar包,故此错。
也可以用response返回结果,就不用手动去加这两个jar包了