Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/spring/11.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 Spring能否自动生成一个';允许';选项方法上的标题?_Java_Spring_Spring Mvc_Http Options Method - Fatal编程技术网

Java Spring能否自动生成一个';允许';选项方法上的标题?

Java Spring能否自动生成一个';允许';选项方法上的标题?,java,spring,spring-mvc,http-options-method,Java,Spring,Spring Mvc,Http Options Method,在SpringMVC中配置我的RequestMappings时,我希望在使用OPTIONS方法时自动生成适当的Allow头 例如,使用此控制器: @Controller @RequestMapping("/test") public class TestController { @RequestMapping(method = RequestMethod.GET) ResponseEntity<String> getTest() { return n

在SpringMVC中配置我的
RequestMapping
s时,我希望在使用
OPTIONS
方法时自动生成适当的
Allow

例如,使用此控制器:

@Controller
@RequestMapping("/test")
public class TestController {

    @RequestMapping(method = RequestMethod.GET)
    ResponseEntity<String> getTest() {
        return new ResponseEntity<>("test", HttpStatus.OK);
    }
}

在我没有编写自定义拦截器的情况下,这个功能是否存在?如果没有,我该如何解决
TODO
问题,并查找
选项
调用发生在同一URL上的注释?

我不知道如何使其通用,尽管它不是通用的,但目前仍然有效

将web.xml中的dispatcher servlet的DispatcheOptions Request设置为true,否则会阻止servlet容器将选项路由到应用程序:

<servlet>
    <servlet-name>mvc-dispatcher</servlet-name>
    <servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
    <load-on-startup>1</load-on-startup>
    <init-param>
        <param-name>contextConfigLocation</param-name>
        <param-value>classpath:spring/mvc-dispatcher-servlet.xml</param-value>
    </init-param>
    <init-param>
        <param-name>dispatchOptionsRequest</param-name>
        <param-value>true</param-value>
    </init-param>
</servlet>

延伸索蒂罗斯和贾德斯捷夫的答案。如果使用Java配置(如在Spring Boot中),您可以通过如下方式配置
@Bean来配置
DispatchServlet
以启用
选项
请求:

@Bean
public DispatcherServlet dispatcherServlet() {
    DispatcherServlet servlet = new DispatcherServlet();
    servlet.setDispatchOptionsRequest(true);
    return servlet;
}
然后,我创建了一个接受HttpMethods varargs的静态帮助器,如下所示:

public static ResponseEntity<Void> allows(HttpMethod... methods) {
    HttpHeaders headers = new HttpHeaders();
    Set<HttpMethod> allow = new HashSet<>();
    for(HttpMethod method: methods){
        allow.add(method);
    }
    headers.setAllow(allow);

    return new ResponseEntity<>(headers, HttpStatus.NO_CONTENT);
}
@RequestMapping(method = RequestMethod.OPTIONS)
ResponseEntity<Void> getProposalsOptions() {
    return allows(HttpMethod.GET, HttpMethod.OPTIONS);
}
虽然我认为Spring MVC可以自动提供
选项
响应是有道理的,但您不能通过
拦截器
来实现,而可能是通过定制的
DispatcherServlet

编写自己的
选项
响应的好处在于,在某些情况下,可以根据用户的角色自定义
选项
。例如,未经身份验证的API用户可能会收到
Allow-GET,OPTIONS
,但管理员将获得完整的API
Allow-GET,PUT,DELETE,OPTIONS
。您可以在调用
OPTIONS
时根据检查用户角色来定制响应。

简化了该用例。从现在起,将自动为应用程序中的所有映射准备选项响应。无需手动配置框架,因为该功能是现成的

默认情况下,通过设置“允许”来处理HTTP选项请求 对所有上显式声明的HTTP方法的响应头 @具有匹配URL模式的RequestMapping方法。当没有HTTP时 方法被显式声明,并且“Allow”头被设置为 获取、头、发布、放置、修补、删除、选项


今天,任何人都可以通过以下方式查找此内容:

只需在请求映射中定义
RequestMethod.OPTIONS
,即可定义整个控制器的选项

以下是更多信息的参考:

在进一步的实验之后,似乎拦截器是不够的,因为在调用拦截器时,Spring MVC已经检测到选项
@RequestMapping
不存在,所以拦截器只通过
/error
URL
public static ResponseEntity<Void> allows(HttpMethod... methods) {
    HttpHeaders headers = new HttpHeaders();
    Set<HttpMethod> allow = new HashSet<>();
    for(HttpMethod method: methods){
        allow.add(method);
    }
    headers.setAllow(allow);

    return new ResponseEntity<>(headers, HttpStatus.NO_CONTENT);
}
@RequestMapping(method = RequestMethod.OPTIONS)
ResponseEntity<Void> getProposalsOptions() {
    return allows(HttpMethod.GET, HttpMethod.OPTIONS);
}
@RestController
@RequestMapping(value = "/user", method = RequestMethod.OPTIONS)
public class UserRestController {