首页 > 代码库 > RestEasy 3.x 系列之三:jsonp

RestEasy 3.x 系列之三:jsonp

跨域请求解决方法(JSONP, CORS)提到解决跨域可以使用jsonp,RestEasy自带jsonp的拦截器

一、参考http://stackoverflow.com/questions/5350924/how-enable-jsonp-in-resteasy里面的方法:

  1、In your web.xml add:

<context-param>    <param-name>resteasy.providers</param-name>    <param-value>org.jboss.resteasy.plugins.providers.jackson.JacksonJsonpInterceptor</param-value></context-param>

  2、Make sure you have a WEB-INF/jboss-deployment-structure.xml with:

<jboss-deployment-structure>    <deployment>        <dependencies>            <module name="org.jboss.resteasy.resteasy-jackson-provider" services="import" annotations="true"/>        </dependencies>    </deployment></jboss-deployment-structure>

  3、Make sure you have a resteasy-jackson-provider dependency in your pom.xml, something like:

<dependency>    <groupId>org.jboss.resteasy</groupId>    <artifactId>resteasy-jackson-provider</artifactId>    <scope>provided</scope></dependency>

二、但是RestEasy的文档提到的是使用:

    <dependency>       <groupId>org.jboss.resteasy</groupId>       <artifactId>resteasy-json-p-provider</artifactId>       <version>3.0.9.Final</version>    </dependency>

三、最后我自己试验了一下

  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}

注意后面少了一个")",他返回的是json object

但是

POST http://localhost:8080/RestEasy_02_Jsonp/json/product/post?callback=back

返回:back(Product created : com.tutu.domain.Product@5ad557c2) 正常,他返回的是string。

不知道什么问题,有知道的麻烦告诉一下

 

四、源码下载

  附件:RestEasy_02_jsonp.zip

RestEasy 3.x 系列之三:jsonp