请求方法'选项'不支持 - Spring Boot应用程序

时间:2016-03-08 22:50:49

标签: javascript angularjs spring spring-boot cors

我在服务器端使用 Spring Boot ,在客户端使用 Angularjs ,我配置 Spring Boot 应用程序 CORS 使用过滤器,它适用于 GET POST 方法,但是,当我尝试发送 PUT 时使用 $ http 模块请求,我在浏览器控制台中得到了这个:

注意:网址= http://localhost:8080/localbusinessusers/[object%20Object]

  

OPTIONS url   (匿名函数)@ angular.js:11442sendReq @   angular.js:11235serverRequest @ angular.js:10945processQueue @   angular.js:15552(匿名函数)@ angular.js:15568Scope。$ eval @   angular.js:16820Scope。$ digest @ angular.js:16636Scope。$ apply @   angular.js:16928done @ angular.js:11266completeRequest @   angular.js:11464requestLoaded @ angular.js:11405 app.html:1   XMLHttpRequest无法加载    URL 的。响应   对于预检具有无效的HTTP状态代码405 angular.js:11442 XHR   加载失败:PUT   " URL "

我在 Spring Boot控制台

上得到了这个
  

2016-03-08 23:19:51.212 WARN 27044 --- [XNIO-2 task-2]   o.s.web.servlet.PageNotFound:请求方法' OPTIONS'   不支持

我的 CORS 过滤

package com.datcom.fouras;

import java.io.IOException;

import javax.servlet.Filter;
import javax.servlet.FilterChain;
import javax.servlet.FilterConfig;
import javax.servlet.ServletException;
import javax.servlet.ServletRequest;
import javax.servlet.ServletResponse;
import javax.servlet.http.HttpServletResponse;

import org.springframework.stereotype.Component;

@Component
public class CorsFilter implements Filter {

    @Override
    public void init(FilterConfig filterConfig) throws ServletException {
        // TODO Auto-generated method stub

    }

    @Override
    public void doFilter(ServletRequest req, ServletResponse res, FilterChain chain)
            throws IOException, ServletException {
        HttpServletResponse response = (HttpServletResponse) res;
        response.setHeader("Access-Control-Allow-Origin", "http://localhost");
        response.setHeader("Access-Control-Allow-Methods", "POST, GET, OPTIONS, DELETE");
        response.setHeader("Access-Control-Max-Age", "3600");
        response.setHeader("Access-Control-Allow-Headers", "Content-Type, x-requested-with, X-Custom-Header");
        chain.doFilter(req, res);

    }

    @Override
    public void destroy() {
        // TODO Auto-generated method stub

    }

}

我的 RestController 中的 REST 方法:

@RequestMapping(value = "/localbusinessusers", method = RequestMethod.PUT)
    public LocalBusinessUser update(LocalBusinessUser localBusinessUser) {
        log.debug("REST request to update a local business user : {}", localBusinessUser);
        if (localBusinessUser.getId() == null) {
            return save(localBusinessUser);
        } else {
            LocalBusinessUser localBusinessUser2 = localBusinessUserRepository.findOne(localBusinessUser.getId());
            localBusinessUser2 = localBusinessUser;
            return localBusinessUserRepository.saveAndFlush(localBusinessUser2);
        }
    }

最后,我的 $ http 请求:



$http.put('http://localhost:8080/localbusinessusers/' + localBusinessUser)
                .success(function (data, status, headers) {
                    console.log('data: ' + data);
                })
                .error(function (data, status, header, config) {
                    console.log('data: ' + data);
                    console.log('status: ' + status);
                    console.log('header: ' + header);
                    console.log('config: ' + config);
                });




注意: PUT请求正在使用高级REST客户端

1 个答案:

答案 0 :(得分:5)

如果禁止选项请求,您可以将DispatcherServlet配置为通过将此选项添加到application.properties来发送所有选项请求:

spring.mvc.dispatch-options-request=true

有关使用Spring Boot的选项请求和Spring MVC的更多信息,Handling Options Request using Spring Boot

有一个很好的帖子

您还可以尝试本文中的示例,通过创建一个简单的控制器来为所有选项请求返回NO_CONTENT来处理CORS和支持选项请求:http://dontpanic.42.nl/2015/04/cors-with-spring-mvc.html