相关疑难解决方法(0)

Spring JSON请求获得406(不可接受)

这是我的javascript:

    function getWeather() {
        $.getJSON('getTemperature/' + $('.data option:selected').val(), null, function(data) {
            alert('Success');                               
        });
    }
Run Code Online (Sandbox Code Playgroud)

这是我的控制器:

@RequestMapping(value="/getTemperature/{id}", headers="Accept=*/*", method = RequestMethod.GET)
@ResponseBody
public Weather getTemparature(@PathVariable("id") Integer id){
    Weather weather = weatherService.getCurrentWeather(id);
        return weather;
}
Run Code Online (Sandbox Code Playgroud)

为spring-servlet.xml

<context:annotation-config />
<tx:annotation-driven />
Run Code Online (Sandbox Code Playgroud)

得到此错误:

GET http://localhost:8080/web/getTemperature/2 406 (Not Acceptable)
Run Code Online (Sandbox Code Playgroud)

头:

响应标题

Server  Apache-Coyote/1.1
Content-Type    text/html;charset=utf-8
Content-Length  1070
Date    Sun, 18 Sep 2011 17:00:35 GMT
Run Code Online (Sandbox Code Playgroud)

请求标题

Host    localhost:8080
User-Agent  Mozilla/5.0 (Windows NT 6.1; WOW64; rv:6.0.2) Gecko/20100101 Firefox/6.0.2
Accept  application/json, text/javascript, */*; q=0.01
Accept-Language en-us,en;q=0.5
Accept-Encoding gzip, …
Run Code Online (Sandbox Code Playgroud)

javascript java ajax spring json

83
推荐指数
7
解决办法
18万
查看次数

Spring MVC - HttpMediaTypeNotAcceptableException

当与Spring MVC和JSON一起使用时,我一直收到AJAX请求的HttpMediaTypeNotAcceptableException错误.错误的完整堆栈跟踪是..

 org.springframework.web.HttpMediaTypeNotAcceptableException: Could not find acceptable representation
org.springframework.web.servlet.mvc.annotation.AnnotationMethodHandlerAdapter$ServletHandlerMethodInvoker.writeWithMessageConverters(AnnotationMethodHandlerAdapter.java:1032)
org.springframework.web.servlet.mvc.annotation.AnnotationMethodHandlerAdapter$ServletHandlerMethodInvoker.handleResponseBody(AnnotationMethodHandlerAdapter.java:972)
org.springframework.web.servlet.mvc.annotation.AnnotationMethodHandlerAdapter$ServletHandlerMethodInvoker.getModelAndView(AnnotationMethodHandlerAdapter.java:921)
org.springframework.web.servlet.mvc.annotation.AnnotationMethodHandlerAdapter.invokeHandlerMethod(AnnotationMethodHandlerAdapter.java:438)
org.springframework.web.servlet.mvc.annotation.AnnotationMethodHandlerAdapter.handle(AnnotationMethodHandlerAdapter.java:424)
org.springframework.web.servlet.DispatcherServlet.doDispatch(DispatcherServlet.java:863)
org.springframework.web.servlet.DispatcherServlet.doService(DispatcherServlet.java:792)
org.springframework.web.servlet.FrameworkServlet.processRequest(FrameworkServlet.java:851)
org.springframework.web.servlet.FrameworkServlet.doGet(FrameworkServlet.java:756)
javax.servlet.http.HttpServlet.service(HttpServlet.java:617)
javax.servlet.http.HttpServlet.service(HttpServlet.java:717)
Run Code Online (Sandbox Code Playgroud)

我做的小google搜索显示请求应该包含类似"accept:application/json"的东西,它确实有..这里是来自firebug的请求标头..

Response Headers
Server  Apache-Coyote/1.1
Content-Type    text/html;charset=utf-8
Content-Length  2503
Date    Thu, 25 Aug 2011 21:00:05 GMT
Connection  close

Request Headers
Host    localhost:8080
User-Agent  Mozilla/5.0 (Windows; U; Windows NT 5.1; en-US; rv:1.9.2.20) Gecko/20110803 Firefox/3.6.20 (.NET CLR 3.5.30729)
Accept  application/json, text/javascript, */*; q=0.01
Accept-Language en-us,en;q=0.5
Accept-Encoding gzip,deflate
Accept-Charset  ISO-8859-1,utf-8;q=0.7,*;q=0.7
Keep-Alive  115
Connection  keep-alive
X-Requested-With    XMLHttpRequest
Referer http://localhost:8080/legaldirectory/index.html
Cookie  JSESSIONID=5C97DA19AED4D5FA17F4A58470FAA93B
Run Code Online (Sandbox Code Playgroud)

现在我完全迷失在这里发生的事情......这里还有什么可能出错来解决这个错误......

spring json spring-mvc spring-json

43
推荐指数
7
解决办法
11万
查看次数

Spring 4.1.1 RELEASE和@ResponseBody返回HTTP 406

我正在使用@ResponseBody返回Spring MVC中的Json对象.它在版本4.0.7和3.2.11上按预期工作,但当我尝试使用最新的Spring版本4.1.1(截至10/16)而没有任何其他配置更改时,它返回HTTP状态406.这被认为是一个错误还是4.1.1需要不同的配置?

最新的jackson jar已经在classpath中了

<dependency>
    <groupId>org.codehaus.jackson</groupId>
    <artifactId>jackson-mapper-asl</artifactId>
    <version>1.9.13</version>
</dependency>
Run Code Online (Sandbox Code Playgroud)

Spring 文档上的示例运行正常

@RequestMapping(value = "/something", method = RequestMethod.PUT)
@ResponseBody
public String helloWorld() {
  return "Hello World";
}
Run Code Online (Sandbox Code Playgroud)

当返回类型是String时.当返回类型是POJO时会发生问题.

json spring-mvc http-status-code-406

11
推荐指数
2
解决办法
2万
查看次数