弹簧控制器被调用两次
这让我抓狂。我有一个简单的 spring 应用程序,只有一个 servlet 上下文文件,配置如下:
<context:component-scan base-package="au.com.mypackage.service" />
<context:annotation-config />
<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();
bean.setSomething("hello");
bean.setSomethingElse(5);
return bean;
}
它利用消息转换器来完成工作。然后我从 SoapUI 提交此请求:
GET http://localhost:8080/spring/data/123 HTTP/1.1
Accept-Encoding: gzip,deflate
Accept: application/json
User-Agent: Jakarta Commons-HttpClient/3.1
Host: localhost:8080
它看起来工作正常。但是,当我查看 tomcat 日志时,我发现控制器被调用了两次。这对于 JSON 来说没问题。但是当我转而请求 XML 时,它就出现了严重错误。由于套接字关闭等原因,第二个请求(无论如何都不应该发生)在 XStream 中触发了巨大的错误。
问题的根源是控制器的调用两次。有谁知道为什么会发生这种情况?
This is driving me nuts. I have a simple spring app with just a servlet context file configured like this:
<context:component-scan base-package="au.com.mypackage.service" />
<context:annotation-config />
<mvc:annotation-driven />
And a simple controller:
@Controller
public class MyController {
@RequestMapping(value = "/data/{id}", method=RequestMethod.GET)
@ResponseBody public Bean getData(@PathVariable String id) {
Bean bean = new Bean();
bean.setSomething("hello");
bean.setSomethingElse(5);
return bean;
}
Which makes use of message converters to do it's work. I then submit this request from SoapUI:
GET http://localhost:8080/spring/data/123 HTTP/1.1
Accept-Encoding: gzip,deflate
Accept: application/json
User-Agent: Jakarta Commons-HttpClient/3.1
Host: localhost:8080
And it appears to work ok. BUT, when I look in the tomcat logs I see that the controller is called twice. This is ok for JSON. But when I switch to requesting XML it goes horribly wrong. The second request (which should not happen anyway) triggers as massive error in XStream because of socket closures, etc.
The root of the problem is the calling of the controller twice. Does anyone know why this would be happening?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
解决了。问题似乎是SoapUI。我切换到 WizTools RESTClient 并且双重调用停止了。我不知道为什么 SoapUI 这样做,尽管我的印象是第二次调用总是针对 XML,无论第一次调用的目的是什么。
Solved it. The problem seems to be SoapUI. I switched to WizTools RESTClient and the double calls stopped. I don't know why SoapUI was doing this, although I got the impression the second call was always for XML regardless of what the first call was for.