在 Spring 3.0.5 中并对其进行编码以提供 JSON,此日志输出试图告诉我什么?
使用 Spring 3.0.5,我尝试返回 JSON 格式的字符串列表。
我可以使用 jackson-mapper代码 1.4.2,它在我的类路径中。
我还使用
,它应该满足
要求。我正在返回一个
List
,尽管我也只尝试过一个bean。当我将原始 JSON 作为字符串返回时,它可以工作。
以下日志输出告诉我什么?是否说它找不到 Jackson JSON Mapper,因此未加载 MappingJacksonHttpMessageConverter
?
17:19:01,037 DEBUG [AnnotationMethodHandlerExceptionResolver:132] Resolving exception from handler [com.funfun.foofoo2.CarModelsController@181fa4b]: org.springframework.web.HttpMediaTypeNotAcceptableException: Could not find acceptable representation
17:19:01,037 DEBUG [ResponseStatusExceptionResolver:132] Resolving exception from handler [com.funfun.foofoo2.CarModelsController@181fa4b]: org.springframework.web.HttpMediaTypeNotAcceptableException: Could not find acceptable representation
17:19:01,037 DEBUG [DefaultHandlerExceptionResolver:132] Resolving exception from handler [com.funfun.foofoo2.CarModelsController@181fa4b]: org.springframework.web.HttpMediaTypeNotAcceptableException: Could not find acceptable representation
17:19:01,053 DEBUG [DispatcherServlet:824] Null ModelAndView returned to DispatcherServlet with name 'appServlet': assuming HandlerAdapter completed request handling
更新:我正在使用 jQuery 的 getJSON。在 Firefox Firebug 中,我看到标头以 application/json 形式出现。 Spring 方法仅限于处理 JSON 请求。
Using Spring 3.0.5, I am trying to return a list of strings in JSON format.
I read the post spring-mvc-json-response
I have available to me jackson-mapper code 1.4.2 and it's on my classpath.
I am also using
<context:component-scan base-package="com.funfun.foofoo2" />
which should satisfy the<mvc:annotation-driven>
requirement.I am returning a
List<String>
although I have tried just a bean too.When I return raw JSON as a String, it works.
What is the following log output telling me? Is it saying it cannot find the Jackson JSON Mapper and thus the MappingJacksonHttpMessageConverter
is not loaded?
17:19:01,037 DEBUG [AnnotationMethodHandlerExceptionResolver:132] Resolving exception from handler [com.funfun.foofoo2.CarModelsController@181fa4b]: org.springframework.web.HttpMediaTypeNotAcceptableException: Could not find acceptable representation
17:19:01,037 DEBUG [ResponseStatusExceptionResolver:132] Resolving exception from handler [com.funfun.foofoo2.CarModelsController@181fa4b]: org.springframework.web.HttpMediaTypeNotAcceptableException: Could not find acceptable representation
17:19:01,037 DEBUG [DefaultHandlerExceptionResolver:132] Resolving exception from handler [com.funfun.foofoo2.CarModelsController@181fa4b]: org.springframework.web.HttpMediaTypeNotAcceptableException: Could not find acceptable representation
17:19:01,053 DEBUG [DispatcherServlet:824] Null ModelAndView returned to DispatcherServlet with name 'appServlet': assuming HandlerAdapter completed request handling
Update: I am using jQuery's getJSON. In Firefox Firebug, I see the header coming over as application/json. The Spring method is constrained to only handle requests for JSON.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
您需要的不仅仅是
jackson-mapper
,还需要jackson-core
,并且最好是比相当旧的 1.4.x 版本更新的版本。You need more than
jackson-mapper
, you needjackson-core
as well, and preferably something newer than the rather old 1.4.x versions.基本上,此异常 (
HttpMediaTypeNotAcceptableException
) 的含义是客户端在其 HTTP 请求中指定accepts
标头,并且可能不接受 JSON。你是如何提出这个要求的? AJAX 调用?如果是这样,请使用 Firebug 或其他工具检查 XHR 并查看 Accept header 设置为什么。Basically what this exception (
HttpMediaTypeNotAcceptableException
) means is that the client is specifying anaccepts
header in their HTTP request, and it likely isn't accepting JSON. How are you making this request? AJAX call? If so, use Firebug or something to inspect the XHR and see what the accepts header is set to.只需检查 HTTP 请求标头中的 Accept 参数即可。如果您使用 XMLHttpRequest 进行 ajax 调用,请不要忘记更改 requestHeader,如下所示:
问候,
David。
Just check the Accept param in your HTTP request header. If you are using XMLHttpRequest to make the ajax call, don't forget to change the requestHeader as follow:
Regards,
David.