Java SpringMVC请求映射传递不应传递的请求
编辑-现在解决了这个问题,错误出现在我的spring配置中。我错过了Java SpringMVC请求映射传递不应传递的请求,java,spring,rest,spring-mvc,http-status-codes,Java,Spring,Rest,Spring Mvc,Http Status Codes,编辑-现在解决了这个问题,错误出现在我的spring配置中。我错过了标签。添加它时,一切都按预期进行 我已经使用SpringMVC定义了一个控制器,只有当请求的内容类型为application/json时,它才应该接受对地址/controllertest的HTTP GET请求。它还返回application/json格式的数据,因此只有接受该格式数据的请求才会通过。然而,这是行不通的 这是我使用Spring 3.2.0的代码: package test.controllers; import
标签。添加它时,一切都按预期进行
我已经使用SpringMVC定义了一个控制器,只有当请求的内容类型为application/json
时,它才应该接受对地址/controllertest
的HTTP GET
请求。它还返回application/json
格式的数据,因此只有接受该格式数据的请求才会通过。然而,这是行不通的
这是我使用Spring 3.2.0的代码:
package test.controllers;
import static org.springframework.http.MediaType.APPLICATION_JSON_VALUE;
import static org.springframework.web.bind.annotation.RequestMethod.GET;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
@Controller
@RequestMapping("/controllertest")
public class TestController {
@RequestMapping(method = GET, consumes = APPLICATION_JSON_VALUE, produces = APPLICATION_JSON_VALUE)
@ResponseBody
public String helloJSON() {
return "{\"message\":\"HelloWorld\"}";
}
}
这就是我所期望的:
GET
以外的任何HTTP方法,我应该会收到405-不允许使用的方法
响应application/json
,我应该会收到406 not Acceptable
响应application/json
,我应该会收到415不支持的媒体类型
响应405-不允许的方法
。非常好Accept
标题和text/xml
的请求时,只有200-OK
响应,响应的内容类型是text/xml
,即使该方法只生成application/json
text/xml
的请求时,我得到200-OK
响应我可以补充一点,我在Jersey中实现了所有这些,结果都是正确的。对于Spring 3.2,行为与您所描述的一样,除了2,我得到了405(不允许使用方法)而不是406。您能否确认用于这些测试的Spring版本,以及升级到至少3.10+是否可行,并再次确认此行为。我确实使用Spring 3.2.0.RELEASE(来自maven repository)。很抱歉没有在我的问题中包括这一点。当您使用正确的HTTP方法但使用不包含“products”中指定的内容类型的Accept标头时,是否会得到405?是的,无法解释您看到的行为