使用SpsringMVC,使用restEasy调试,controller请求设置如下:
- @RequestMapping(value="/list",method=RequestMethod.GET,produces=MediaType.APPLICATION_JSON_VALUE)
- @ResponseBody
- public List<EditTimeout> list(){
- List<EditTimeout> list = editImpl.selectAll();
- return list;
- }
请求,debug跟踪list是返回值没有问题,但是前台页面报如下错误:
- Response Status: 406 (Not Acceptable)
Response RAW返回值如下,(前半部分)
- HTTP GET http://127.0.0.1:8080/onlineLibrary/rest/system/editTimeout/list
- Host: 127.0.0.1:8080
- 406 Not Acceptable
- Date: Sat, 17 Aug 2013 11:01:07 GMT
- Server: Apache-Coyote/1.1
- Content-Length: 1067
- Content-Type: text/html;charset=utf-8
- <html><head><title>Apache Tomcat/7.0.34 - Error report</title><style>
后台提示错误:
- 2013-08-17 19:01:03,116 DEBUG org.springframework.web.servlet.DispatcherServlet -Last-Modified value for [/onlineLibrary/rest/system/editTimeout/list] is: -1
- 2013-08-17 19:01:07,606 DEBUG org.springframework.web.servlet.mvc.method.annotation.ExceptionHandlerExceptionResolver -Resolving exception from handler [public java.util.List<com.online.ol.filter.EditTimeout> com.online.ol.filter.EditTimeoutContoller.list()]: org.springframework.web.HttpMediaTypeNotAcceptableException: Could not find acceptable representation
- 2013-08-17 19:01:07,680 DEBUG org.springframework.web.servlet.mvc.annotation.ResponseStatusExceptionResolver -Resolving exception from handler [public java.util.List<com.online.ol.filter.EditTimeout> com.online.ol.filter.EditTimeoutContoller.list()]: org.springframework.web.HttpMediaTypeNotAcceptableException: Could not find acceptable representation
- 2013-08-17 19:01:07,681 DEBUG org.springframework.web.servlet.mvc.support.DefaultHandlerExceptionResolver -Resolving exception from handler [public java.util.List<com.online.ol.filter.EditTimeout> com.online.ol.filter.EditTimeoutContoller.list()]: org.springframework.web.HttpMediaTypeNotAcceptableException: Could not find acceptable representation
java.lang.Exception]: org.springframework.web.HttpMediaTypeNotAcceptableException: Could not find acceptable representation
引起的原因:
由于设置了@ResponseBody,要把对象转换成json格式,缺少转换依赖的jar包,故此错。
解决办法:
加入依赖的jar,jackson-core-asl-1.9.12.jar,jackson-mapper-asl-1.9.12.jar问题解决。
pring 版本:3.2.4
跟踪到spring mvc中查看到的异常:
org.springframework.web.HttpMediaTypeNotAcceptableException: Could not find acceptable representation
spring中抛出异常的方法:
org.springframework.web.servlet.DispatcherServlet.doDispatch(HttpServletRequest request, HttpServletResponse response) throws Exception
错误的方法代码:
1 | @RequestMapping (value= "awardByPage" ,produces= "text/plain;charset=UTF-8" ) |
2 | public @ResponseBody JSONResponse awardByPage( @RequestParam int pageIndex) { |
3 | return JSONResponse.buildSuccess(awardService.awardLogByPage(pageIndex)); |
4 | } |
解决方案:
1. 删除代码中的:
1 | ,produces= "text/plain;charset=UTF-8" |
2. 网络上查找的其它解决方案:
1 | <!-- 启动JSON格式的配置 --> |
2 | < bean id = "jacksonMessageConverter" class = "org.springframework.http.converter.json.MappingJacksonHttpMessageConverter" > |
3 | <!--解决 HttpMediaTypeNotAcceptableException: Could not find acceptable representation --> |
4 | < property name = "supportedMediaTypes" > |
5 | < list > |
6 | < value >text/html;charset=UTF-8</ value > |
7 | </ list > |
8 | </ property > |
9 | </ bean > |
产生此问题的其它原因及解决方法:
1. controller中返回的内容没有get方法,
解决办法:添加get方法;
2. 由于设置了@ResponseBody,要把对象转换成json格式,缺少转换依赖的jar包,故此错。
解决办法: 加入依赖的jar,jackson-core-asl-1.9.12.jar,jackson-mapper-asl-1.9.12.jar问题解决。