跨域请求解决方法(JSONP, CORS)提到解决跨域可以使用jsonp,RestEasy自带jsonp的拦截器
一、RestEasy的文档如下:
If you're using Jackson, Resteasy has JSONP that you can turn on by adding the providerorg.jboss.resteasy.plugins.providers.jackson.JacksonJsonpInterceptor
(Jackson2JsonpInterceptor if you're using the Jackson2 provider) to your deployments. If the media type of the response is json and a callback query parameter is given, the response will be a javascript snippet with a method call of the method defined by the callback parameter. For example:
GET /resources/stuff?callback=processStuffResponse
will produce this response:
processStuffResponse(<nomal JSON body>)
This supports the default behavior of jQuery.
You can change the name of the callback parameter by setting the callbackQueryParameter property.
意思是:1、添加org.jboss.resteasy.plugins.providers.jackson.Jackson2JsonpInterceptor拦截器。2、media type为json。3、有一个callback参数。
二、试验一下
1、json和jsonp都在resteasy-jackson2-provider里面,pom.xml:
<dependency>
<groupId>org.jboss.resteasy</groupId>
<artifactId>resteasy-jackson2-provider</artifactId>
<version>3.0.9.Final</version>
</dependency>
2、web.xml:
<context-param>
<param-name>resteasy.providers</param-name>
<param-value>org.jboss.resteasy.plugins.providers.jackson.Jackson2JsonpInterceptor</param-value>
</context-param>
但是有问题,最后使用
GET http://localhost:8080/RestEasy_02_Jsonp/json/product/get?callback=aaa
返回:aaa({"name":"iPad 3","age":999}
注意后面少了一个")"
但是
POST http://localhost:8080/RestEasy_02_Jsonp/json/product/post?callback=back
返回:back(Product created : com.tutu.domain.Product@5ad557c2) 正常
不知道什么问题,有知道的麻烦告诉一下
三、源码下载
参考:http://*.com/questions/5350924/how-enable-jsonp-in-resteasy