Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/java/402.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Java SpringMVC请求映射传递不应传递的请求_Java_Spring_Rest_Spring Mvc_Http Status Codes - Fatal编程技术网

Java SpringMVC请求映射传递不应传递的请求

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

编辑-现在解决了这个问题,错误出现在我的spring配置中。我错过了
标签。添加它时,一切都按预期进行

我已经使用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?是的,无法解释您看到的行为