Spring 3,rest控制器,mvc:注释驱动,jdk 6但spring拒绝发回xml响应

发布于 2024-11-18 21:04:11 字数 3288 浏览 1 评论 0原文

这几天来这已经让我抓狂了。根据我所读到的内容,我了解到在使用 Spring MVC 自动处理内容时我有两种选择。我可以使用 messageConverters 通过 @RequestBody 自动检测请求,并通过 @ResponseBody 自动检测响应,或者(但不是和!)...我可以使用视图解析器根据各种其他条件(例如请求扩展)自动检测。

我正在尝试使用@RequestBody 和@ResponseBody。我在这里阅读了多篇文章和博客,但我不明白为什么这不起作用。我的 spring-servlet.xml 文件包含:

<context:annotation-config />
<context:component-scan base-package="au.com.mypackage.service" />
<mvc:annotation-driven />

我的控制器看起来像这样:

@Controller
public class MyController {

    @RequestMapping(value = "/data/{id}", method=RequestMethod.GET)
    @ResponseBody public Bean getData(@PathVariable String id) {
        Bean bean = new Bean(); // Simple bean not worth listing here.
        bean.setSomething("hello");
        bean.setSomethingElse(5);
        return bean;
    }
}

但日志

...
2011-07-07 17:23:58,119 DEBUG org.springframework.core.convert.support.GenericConversionService:186 Converted to '123'
2011-07-07 17:23:58,120 DEBUG org.springframework.web.bind.annotation.support.HandlerMethodInvoker:173 Invoking request handler method: public au.com.mypackage.service.MyController$Bean au.com.mypackage.service.MyController.getData(java.lang.String)
...
2011-07-07 17:23:58,143 DEBUG org.springframework.web.servlet.handler.AbstractHandlerExceptionResolver:132 Resolving exception from handler [au.com.mypackage.service.MyController@3d3c4c09]: org.springframework.web.HttpMediaTypeNotAcceptableException: Could not find acceptable representation
...
2011-07-07 17:23:58,144 DEBUG org.springframework.web.servlet.DispatcherServlet:824 Null ModelAndView returned to DispatcherServlet with name 'spring': assuming HandlerAdapter completed request handling
2011-07-07 17:23:58,145 DEBUG org.springframework.web.servlet.FrameworkServlet:674 Successfully completed request
2011-07-07 17:23:58,150 DEBUG org.springframework.web.servlet.DispatcherServlet:693 DispatcherServlet with name 'spring' processing GET request for [/spring/data/123]
...
2011-07-07 17:23:58,154 DEBUG org.springframework.web.bind.annotation.support.HandlerMethodInvoker:173 Invoking request handler method: public au.com.mypackage.service.MyController$Bean au.com.mypackage.service.MyController.getData(java.lang.String)
2011-07-07 17:23:58,175 DEBUG org.springframework.web.servlet.mvc.annotation.AnnotationMethodHandlerAdapter$ServletHandlerMethodInvoker:981 Written [au.com.mypackage.service.MyController$Bean@5b62f030] as "application/json;charset=UTF-8" using [org.springframework.http.converter.json.MappingJacksonHttpMessageConverter@3c870e2f]
2011-07-07 17:23:58,175 DEBUG org.springframework.web.servlet.DispatcherServlet:824 Null ModelAndView returned to DispatcherServlet with name 'smsManager2': assuming HandlerAdapter completed request handling
2011-07-07 17:23:58,175 DEBUG org.springframework.web.servlet.FrameworkServlet:674 Successfully completed request

我的阅读方式是:

  1. 点击控制器并取回 bean。
  2. 抛出 HttpMediaTypeNotAcceptableException。
  3. 点击控制器并取回豆子。
  4. 使用 JSON 消息转换器返回它。

有谁知道出了什么问题吗?我已经玩了好几天了,但无法让它发回 XML。

我检查了annotationMethodHandlerAdapter 中的MessageConverters,那里有一个JaxB 转换器。所以我不知道为什么它没有被使用。

我还将 SoapUI 的其余请求中的 Accept 标头设置为 application/xml,但它不起作用。

This has been driving me nuts for days now. From what I've read I understand that I have two choices when it comes to auto-handling content with Spring MVC. I can use messageConverters to autodetect the request through @RequestBody and the response through @ResponseBody, or (but not and!)... I can use view resolvers to autodetect based on various other criteria such as request extensions.

I'm trying to use @RequestBody and @ResponseBody. I've read multiple posts here and blogs and I cannot see why this is not working. My spring-servlet.xml file contains:

<context:annotation-config />
<context:component-scan base-package="au.com.mypackage.service" />
<mvc:annotation-driven />

My controller looks like this:

@Controller
public class MyController {

    @RequestMapping(value = "/data/{id}", method=RequestMethod.GET)
    @ResponseBody public Bean getData(@PathVariable String id) {
        Bean bean = new Bean(); // Simple bean not worth listing here.
        bean.setSomething("hello");
        bean.setSomethingElse(5);
        return bean;
    }
}

But the logs

...
2011-07-07 17:23:58,119 DEBUG org.springframework.core.convert.support.GenericConversionService:186 Converted to '123'
2011-07-07 17:23:58,120 DEBUG org.springframework.web.bind.annotation.support.HandlerMethodInvoker:173 Invoking request handler method: public au.com.mypackage.service.MyController$Bean au.com.mypackage.service.MyController.getData(java.lang.String)
...
2011-07-07 17:23:58,143 DEBUG org.springframework.web.servlet.handler.AbstractHandlerExceptionResolver:132 Resolving exception from handler [au.com.mypackage.service.MyController@3d3c4c09]: org.springframework.web.HttpMediaTypeNotAcceptableException: Could not find acceptable representation
...
2011-07-07 17:23:58,144 DEBUG org.springframework.web.servlet.DispatcherServlet:824 Null ModelAndView returned to DispatcherServlet with name 'spring': assuming HandlerAdapter completed request handling
2011-07-07 17:23:58,145 DEBUG org.springframework.web.servlet.FrameworkServlet:674 Successfully completed request
2011-07-07 17:23:58,150 DEBUG org.springframework.web.servlet.DispatcherServlet:693 DispatcherServlet with name 'spring' processing GET request for [/spring/data/123]
...
2011-07-07 17:23:58,154 DEBUG org.springframework.web.bind.annotation.support.HandlerMethodInvoker:173 Invoking request handler method: public au.com.mypackage.service.MyController$Bean au.com.mypackage.service.MyController.getData(java.lang.String)
2011-07-07 17:23:58,175 DEBUG org.springframework.web.servlet.mvc.annotation.AnnotationMethodHandlerAdapter$ServletHandlerMethodInvoker:981 Written [au.com.mypackage.service.MyController$Bean@5b62f030] as "application/json;charset=UTF-8" using [org.springframework.http.converter.json.MappingJacksonHttpMessageConverter@3c870e2f]
2011-07-07 17:23:58,175 DEBUG org.springframework.web.servlet.DispatcherServlet:824 Null ModelAndView returned to DispatcherServlet with name 'smsManager2': assuming HandlerAdapter completed request handling
2011-07-07 17:23:58,175 DEBUG org.springframework.web.servlet.FrameworkServlet:674 Successfully completed request

How I read this is:

  1. Hits the controller and gets back the bean.
  2. Throws a HttpMediaTypeNotAcceptableException.
  3. Hits the controller and gets back the bean.
  4. Returns it using a JSON message converter.

Does anyone have any idea whats wrong, I've been playing with this for days and cannot get it to send back XML.

I've checked the MessageConverters in the annotationMethodHandlerAdapter and there is a JaxB converter there. So I'm at a loss as to why it's not being used.

I've also set the Accept header in SoapUI's rest request to application/xml and it just doesn't work.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

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

发布评论

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

评论(1

顾忌 2024-11-25 21:04:11

你有:

@ResponseBody public Bean getData

我认为应该是

public @ResponseBody Bean getData

你正在注释响应类型,而不是方法

You have:

@ResponseBody public Bean getData

I think it should be

public @ResponseBody Bean getData

You're annotating the response type, not the method

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