Cross-Domain on Jersey RESTful Web Services
来源:互联网 发布:js将图片转化为base64 编辑:程序博客网 时间:2024/04/25 02:14
ContainerRequestFilter
interface. Most of all, we can easily adapt or remove this feature in the Web configuration file.Here’s an example to resolve Access-Control-Origin not allowed error and you can set those parameters want you want:
src/main/java/com/example/filter/CORSFilter.java
1234567891011121314151617181920212223
package com.example.filter;import com.sun.jersey.spi.container.ContainerRequest;import com.sun.jersey.spi.container.ContainerResponse;import com.sun.jersey.spi.container.ContainerResponseFilter;public class CORSFilter implements ContainerResponseFilter { @Override public ContainerResponse filter(ContainerRequest request, ContainerResponse response) { response.getHttpHeaders().add("Access-Control-Allow-Origin", "*"); response.getHttpHeaders().add("Access-Control-Allow-Headers", "origin, content-type, accept, authorization"); response.getHttpHeaders().add("Access-Control-Allow-Credentials", "true"); response.getHttpHeaders().add("Access-Control-Allow-Methods", "GET, POST, PUT, DELETE, OPTIONS, HEAD"); return response; }}
The next step is to define your web.xml
like so for filtering the response:
src/main/webapp/WEB-INF/web.xml
1234567891011
<servlet> <servlet-name>Jersey Web Application</servlet-name> <servlet-class>com.sun.jersey.spi.container.servlet.ServletContainer</servlet-class> ... <init-param> <param-name>com.sun.jersey.spi.container.ContainerResponseFilters</param-name> <param-value>com.example.filter.CORSFilter</param-value> </init-param> ... <load-on-startup>1</load-on-startup></servlet>
However, the ajax cross-domain request may accompany with another OPTIONS
request, we can observe it in the Tomcat localhost_access log as following:
10.4.128.61 - - [06/Aug/2013:00:00:21 +0800] "OPTIONS /example/rest/logs HTTP/1.0" 200 52010.4.128.61 - - [06/Aug/2013:00:00:22 +0800] "POST /example/rest/logs HTTP/1.0" 200 34
“preflighted” requests first send an HTTP OPTIONS request header to the resource on the other domain, in order to determine whether the actual request is safe to send.
MOZILLA DEVELOPER NETWORK HTTP Access Control (CORS)
Also the catalina.out
throws the message as:
Aug 07, 2013 7:22:55 AM com.sun.jersey.server.wadl.generators.AbstractWadlGeneratorGrammarGenerator attachTypesINFO: Couldn't find grammar element for class java.io.InputStreamAug 07, 2013 7:22:55 AM com.sun.jersey.server.wadl.generators.AbstractWadlGeneratorGrammarGenerator attachTypesINFO: Couldn't find grammar element for class javax.ws.rs.core.Response
I add the corresponding OPTIONS handler for all the @Path resource to solve the errors, however, I have no idea whether there is a better way to do this?
12345678910
@OPTIONSpublic Response myResource() { return Response.ok().build();}@POSTpublic Response myResource() { // do something return Response.ok().build();}
- Cross-Domain on Jersey RESTful Web Services
- jersey创建Restful Web Services
- 使用Jersey框架创建RESTful Web Services
- JAVA RESTful Web Services - Jersey 入门
- Using Jersey (RESTful Web Services) and Spring (Dependency Injection) Together
- RESTful Web Services Example in Java with Jersey, Spring
- Jersey -RESTful Web Services in Java基础框架示例
- Jersery——RESTful Web Services in Java,RESTful服务,Jersey+Spring实现REST服务调用
- RESTFUL Service : based on Jersey
- Creating RESTful services with Jersey and Groovy
- Jersey RESTful Web中文总结
- jersey (RESTful Web Service框架)
- RESTful Web Services [ILLUSTRATED]
- RESTful Web Services
- RESTful Web Services
- RESTful Web Services初探
- RESTful Web Services初探
- RESTful Web Services - Methods
- 软件概要设计做什么,怎么做
- excel数据导入plsql
- [Leetcode] Palindrome Number (Java)
- 当程序不能正常工作时,程序员的常用借口
- sql 建表
- Cross-Domain on Jersey RESTful Web Services
- MySQL中文乱码问题
- mysql delete from 子查询限制
- 用MongoDB实现MapReduce(翻译)
- Oracle 并行讲解
- 可穿戴设备的一些想法:智能手表的应用。
- 普通情况下的图片懒性加载 简单封装图片预加载函数
- linux 终端快捷键汇总
- 使用SpringMVC提交数组时,如果list大小超过256,就会报错。