在 Spring 3.0.5 中并对其进行编码以提供 JSON,此日志输出试图告诉我什么?

发布于 2024-10-21 14:42:18 字数 1704 浏览 3 评论 0原文

使用 Spring 3.0.5,我尝试返回 JSON 格式的字符串列表。

  • 我读了帖子spring-mvc-json-response

  • 我可以使用 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

可爱咩 2024-10-28 14:42:18

您需要的不仅仅是 jackson-mapper,还需要 jackson-core,并且最好是比相当旧的 1.4.x 版本更新的版本。

You need more than jackson-mapper, you need jackson-core as well, and preferably something newer than the rather old 1.4.x versions.

裂开嘴轻声笑有多痛 2024-10-28 14:42:18

基本上,此异常 (HttpMediaTypeNotAcceptableException) 的含义是客户端在其 HTTP 请求中指定 accepts 标头,并且可能不接受 JSON。你是如何提出这个要求的? AJAX 调用?如果是这样,请使用 Firebug 或其他工具检查 XHR 并查看 Accept header 设置为什么。

Basically what this exception (HttpMediaTypeNotAcceptableException) means is that the client is specifying an accepts 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.

哀由 2024-10-28 14:42:18

只需检查 HTTP 请求标头中的 Accept 参数即可。如果您使用 XMLHttpRequest 进行 ajax 调用,请不要忘记更改 requestHeader,如下所示:

xmlhttp.open("POST", custom_url, this.async);
xmlhttp.setRequestHeader("Accept", "text/html,application/xhtml+xml,application/xml,application/json;q=0.9,*/*;q=0.8");

问候,

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:

xmlhttp.open("POST", custom_url, this.async);
xmlhttp.setRequestHeader("Accept", "text/html,application/xhtml+xml,application/xml,application/json;q=0.9,*/*;q=0.8");

Regards,

David.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文