Rest SpringMVCServlet因POST请求被命中两次

Rest SpringMVCServlet因POST请求被命中两次,rest,tomcat,spring-mvc,servlets,Rest,Tomcat,Spring Mvc,Servlets,在使用SpringMVC为我的web应用程序实现RESTful服务时,我遇到了一个疏远的问题。在执行GET请求时,一切似乎都正常工作,但是,我在处理POST请求时的行为让我感到困惑。我已经实现了这个非常基本的控制器代码: @控制器 @请求映射(“/services”) 公共类服务{ @RequestMapping(value=“/test/post”,method=RequestMethod.post) public void postest(@RequestBody String postSt

在使用SpringMVC为我的web应用程序实现RESTful服务时,我遇到了一个疏远的问题。在执行GET请求时,一切似乎都正常工作,但是,我在处理POST请求时的行为让我感到困惑。我已经实现了这个非常基本的控制器代码:

@控制器
@请求映射(“/services”)
公共类服务{
@RequestMapping(value=“/test/post”,method=RequestMethod.post)
public void postest(@RequestBody String postString)
抛出PersistenceException{
System.out.println(postString);
}
}
当我使用Curl对其执行POST请求时:

curl --data "Hello world" http://localhost:8080/SpringMVC-REST/services/test/post
第一次正确到达控制器并显示字符串时。但是,不知道为什么,之后会再次调用,在本例中是错误的url请求。框架找不到匹配的服务案例,显然:

2014年6月2日下午4:06:21 org.springframework.web.servlet.DispatcherServlet noHandlerFound 警告:在名为“mvc dispatcher”的DispatcherServlet中找不到URI为[/SpringMVC REST/services/test/services/test/post]的HTTP请求的映射

对于第一种和第二种情况,调试器的堆栈似乎略有不同:

这似乎是SpringMVC框架第二次尝试呈现输出,即使我对它不感兴趣,因为我没有通过WebUI访问它。我使用的servlet配置是标准配置:


春季MVC休息
上下文配置位置
/WEB-INF/mvc-dispatcher-servlet.xml
org.springframework.web.context.ContextLoaderListener
mvc调度器
org.springframework.web.servlet.DispatcherServlet
1.
mvc调度器
/

在Tomcat6和7以及SpringWeb3.2.8.RELEASE中,这种情况发生在我身上。有人能看到这里的问题吗?

在方法返回类型之前使用
@ResponseBody
。它应该可以解决您的问题

所以应该是这样

@Controller
@RequestMapping("/services")
public class RestService {

    @RequestMapping(value = "/test/post", method = RequestMethod.POST)
    public @ResponseBody void postTest(@RequestBody String postString)
            throws PersistenceException {
        System.out.println(postString);
    }

}