请求方法'OPTIONS'不受支持 - Spring Boot Application
Posted
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了请求方法'OPTIONS'不受支持 - Spring Boot Application相关的知识,希望对你有一定的参考价值。
我在服务器端使用Spring Boot,在客户端使用Angularjs,我使用Filter配置Spring Boot应用程序CORS,它适用于GET,POST方法,但是,当我尝试使用$ http模块发送PUT请求时,我得到了这个在我的浏览器控制台:
注意:url = http://localhost:8080/localbusinessusers/[object%20Object]
选项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无法加载网址。预检的响应具有无效的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客户端
如果禁止选项请求,您可以将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
以上是关于请求方法'OPTIONS'不受支持 - Spring Boot Application的主要内容,如果未能解决你的问题,请参考以下文章
js动态添加option 第一次能添加成功,第二次在请求的时候就 Cannot read property 'options' of null
+的不受支持的操作数类型:'RegexURLPattern'和'unicode'