You are viewing a plain text version of this content. The canonical link for it is here.
Posted to users@cxf.apache.org by Jack Huang <hu...@gmail.com> on 2014/03/03 10:51:49 UTC

Re: JSONP is not works

Hi,

It not works. :(

I use 2.7.9 version and set JsonpInInterceptor's acceptType value to
"application/json".
The JSON data is presented, but without callback function.

You can try it again. Thanks!

Jack Huang



--
View this message in context: http://cxf.547215.n5.nabble.com/JSONP-is-not-works-tp5739858p5740684.html
Sent from the cxf-user mailing list archive at Nabble.com.

Re: JSONP is not works

Posted by Sergey Beryozkin <sb...@gmail.com>.
Yes, have a look at
https://fisheye6.atlassian.com/browse/cxf/systests/jaxrs/src/test/resources/jaxrs/WEB-INF/beans.xml?r=b3288ed94bd6518780839a6b9716c64575c68073

a resource with "/jsonp" uses JSONProvider and the pre stream handler 
registered in outInterceptors, a resource with "/jsonp2" uses Jackson 
and indeed registers a handler in providers.

The reason behind is is that now the response content is determined 
early and as such, when a write of the response data is starting, in 
this case CT would application/javascript or similar which is not 
supported by Jackson. So using a handler as a WriterInterceptor lets us 
temporarily replace "application/javascript" with "application/json" and 
then restore "application/javascript".

Cheers, Sergey

On 02/07/14 23:24, Sergey Beryozkin wrote:
> Hi
>
> As far as I recall it should work with JSONProvider pre-configured to
> support application/x+javascript or similar which is not possible with
> Jackson which is where a writer interceptor comes in where it
> temporarily sets application/json for Jackson to write the data out
>
> I'll double check a bit later
> Sergey
>
>
> On 01/07/14 09:30, Jack Huang wrote:
>> When I register JsonpPreStreamInterceptor into
>> <jaxrs:outInterceptors>, the
>> JSONP is not works, as below configuration:
>>
>>      <jaxrs:server address="/rest">
>>          <jaxrs:serviceBeans>
>>              <ref bean="productServiceImpl"/>
>>          </jaxrs:serviceBeans>
>>          <jaxrs:providers>
>>              <bean
>> class="com.fasterxml.jackson.jaxrs.json.JacksonJsonProvider"/>
>>          </jaxrs:providers>
>>          <jaxrs:inInterceptors>
>>              <bean
>> class="org.apache.cxf.jaxrs.provider.jsonp.JsonpInInterceptor"/>
>>          </jaxrs:inInterceptors>
>>          <jaxrs:outInterceptors>
>>              *<bean
>> class="org.apache.cxf.jaxrs.provider.jsonp.JsonpPreStreamInterceptor"/>*
>>              <bean
>> class="org.apache.cxf.jaxrs.provider.jsonp.JsonpPostStreamInterceptor"/>
>>          </jaxrs:outInterceptors>
>>      </jaxrs:server>
>>
>> But, when I put JsonpPreStreamInterceptor into <jaxrs:providers>, the
>> JSONP
>> is OK, as below configuration:
>>
>>      <jaxrs:server address="/rest">
>>          <jaxrs:serviceBeans>
>>              <ref bean="productServiceImpl"/>
>>          </jaxrs:serviceBeans>
>>          <jaxrs:providers>
>>              <bean
>> class="com.fasterxml.jackson.jaxrs.json.JacksonJsonProvider"/>
>>              *<bean
>> class="org.apache.cxf.jaxrs.provider.jsonp.JsonpPreStreamInterceptor"/>*
>>          </jaxrs:providers>
>>          <jaxrs:inInterceptors>
>>              <bean
>> class="org.apache.cxf.jaxrs.provider.jsonp.JsonpInInterceptor"/>
>>          </jaxrs:inInterceptors>
>>          <jaxrs:outInterceptors>
>>              <bean
>> class="org.apache.cxf.jaxrs.provider.jsonp.JsonpPostStreamInterceptor"/>
>>          </jaxrs:outInterceptors>
>>      </jaxrs:server>
>>
>> I have not configured the mediaType property to "application/json" in
>> JsonpPreStreamInterceptor.
>>
>> CXF Version is 3.0.0
>>
>>
>>
>> --
>> View this message in context:
>> http://cxf.547215.n5.nabble.com/JSONP-is-not-works-tp5739858p5745795.html
>> Sent from the cxf-user mailing list archive at Nabble.com.
>>
>
>


-- 
Sergey Beryozkin

Talend Community Coders
http://coders.talend.com/

Blog: http://sberyozkin.blogspot.com

Re: JSONP is not works

Posted by Sergey Beryozkin <sb...@gmail.com>.
Hi

As far as I recall it should work with JSONProvider pre-configured to 
support application/x+javascript or similar which is not possible with 
Jackson which is where a writer interceptor comes in where it 
temporarily sets application/json for Jackson to write the data out

I'll double check a bit later
Sergey


On 01/07/14 09:30, Jack Huang wrote:
> When I register JsonpPreStreamInterceptor into <jaxrs:outInterceptors>, the
> JSONP is not works, as below configuration:
>
>      <jaxrs:server address="/rest">
>          <jaxrs:serviceBeans>
>              <ref bean="productServiceImpl"/>
>          </jaxrs:serviceBeans>
>          <jaxrs:providers>
>              <bean
> class="com.fasterxml.jackson.jaxrs.json.JacksonJsonProvider"/>
>          </jaxrs:providers>
>          <jaxrs:inInterceptors>
>              <bean
> class="org.apache.cxf.jaxrs.provider.jsonp.JsonpInInterceptor"/>
>          </jaxrs:inInterceptors>
>          <jaxrs:outInterceptors>
>              *<bean
> class="org.apache.cxf.jaxrs.provider.jsonp.JsonpPreStreamInterceptor"/>*
>              <bean
> class="org.apache.cxf.jaxrs.provider.jsonp.JsonpPostStreamInterceptor"/>
>          </jaxrs:outInterceptors>
>      </jaxrs:server>
>
> But, when I put JsonpPreStreamInterceptor into <jaxrs:providers>, the JSONP
> is OK, as below configuration:
>
>      <jaxrs:server address="/rest">
>          <jaxrs:serviceBeans>
>              <ref bean="productServiceImpl"/>
>          </jaxrs:serviceBeans>
>          <jaxrs:providers>
>              <bean
> class="com.fasterxml.jackson.jaxrs.json.JacksonJsonProvider"/>
>              *<bean
> class="org.apache.cxf.jaxrs.provider.jsonp.JsonpPreStreamInterceptor"/>*
>          </jaxrs:providers>
>          <jaxrs:inInterceptors>
>              <bean
> class="org.apache.cxf.jaxrs.provider.jsonp.JsonpInInterceptor"/>
>          </jaxrs:inInterceptors>
>          <jaxrs:outInterceptors>
>              <bean
> class="org.apache.cxf.jaxrs.provider.jsonp.JsonpPostStreamInterceptor"/>
>          </jaxrs:outInterceptors>
>      </jaxrs:server>
>
> I have not configured the mediaType property to "application/json" in
> JsonpPreStreamInterceptor.
>
> CXF Version is 3.0.0
>
>
>
> --
> View this message in context: http://cxf.547215.n5.nabble.com/JSONP-is-not-works-tp5739858p5745795.html
> Sent from the cxf-user mailing list archive at Nabble.com.
>


-- 
Sergey Beryozkin

Talend Community Coders
http://coders.talend.com/

Blog: http://sberyozkin.blogspot.com

Re: JSONP is not works

Posted by Jack Huang <hu...@gmail.com>.
When I register JsonpPreStreamInterceptor into <jaxrs:outInterceptors>, the
JSONP is not works, as below configuration:

    <jaxrs:server address="/rest">
        <jaxrs:serviceBeans>
            <ref bean="productServiceImpl"/>
        </jaxrs:serviceBeans>
        <jaxrs:providers>
            <bean
class="com.fasterxml.jackson.jaxrs.json.JacksonJsonProvider"/>
        </jaxrs:providers>
        <jaxrs:inInterceptors>
            <bean
class="org.apache.cxf.jaxrs.provider.jsonp.JsonpInInterceptor"/>
        </jaxrs:inInterceptors>
        <jaxrs:outInterceptors>
            *<bean
class="org.apache.cxf.jaxrs.provider.jsonp.JsonpPreStreamInterceptor"/>*
            <bean
class="org.apache.cxf.jaxrs.provider.jsonp.JsonpPostStreamInterceptor"/>
        </jaxrs:outInterceptors>
    </jaxrs:server>

But, when I put JsonpPreStreamInterceptor into <jaxrs:providers>, the JSONP
is OK, as below configuration:

    <jaxrs:server address="/rest">
        <jaxrs:serviceBeans>
            <ref bean="productServiceImpl"/>
        </jaxrs:serviceBeans>
        <jaxrs:providers>
            <bean
class="com.fasterxml.jackson.jaxrs.json.JacksonJsonProvider"/>
            *<bean
class="org.apache.cxf.jaxrs.provider.jsonp.JsonpPreStreamInterceptor"/>*
        </jaxrs:providers>
        <jaxrs:inInterceptors>
            <bean
class="org.apache.cxf.jaxrs.provider.jsonp.JsonpInInterceptor"/>
        </jaxrs:inInterceptors>
        <jaxrs:outInterceptors>
            <bean
class="org.apache.cxf.jaxrs.provider.jsonp.JsonpPostStreamInterceptor"/>
        </jaxrs:outInterceptors>
    </jaxrs:server>

I have not configured the mediaType property to "application/json" in
JsonpPreStreamInterceptor.

CXF Version is 3.0.0



--
View this message in context: http://cxf.547215.n5.nabble.com/JSONP-is-not-works-tp5739858p5745795.html
Sent from the cxf-user mailing list archive at Nabble.com.

Re: JSONP is not works

Posted by Sergey Beryozkin <sb...@gmail.com>.
Hi
On 30/06/14 06:54, Jack Huang wrote:
> Dear Sergey
>
> Thank you for your help, I have upgraded CXF 3.0.0, and the JSONP is very
> great now!
>
OK, thanks for testing it,
> Maven configuration:
>
>          <dependency>
>              <groupId>org.apache.cxf</groupId>
>              <artifactId>cxf-rt-frontend-jaxrs</artifactId>
>              <version>${cxf.version}</version>
>          </dependency>
>          <dependency>
>              <groupId>org.apache.cxf</groupId>
>              <artifactId>cxf-rt-rs-extension-providers</artifactId>
>              <version>${cxf.version}</version>
>          </dependency>
>
>          <dependency>
>              <groupId>com.fasterxml.jackson.jaxrs</groupId>
>              <artifactId>jackson-jaxrs-json-provider</artifactId>
>              <version>${jackson.version}</version>
>          </dependency>
>
> Spring configuration:
>      <jaxrs:server address="/rest">
>          <jaxrs:serviceBeans>
>              <ref bean="productServiceImpl"/>
>          </jaxrs:serviceBeans>
>          <jaxrs:providers>
>              <bean
> class="com.fasterxml.jackson.jaxrs.json.JacksonJsonProvider"/>
>              <bean
> class="org.apache.cxf.jaxrs.provider.jsonp.JsonpPreStreamInterceptor"/>
>          </jaxrs:providers>
>          <jaxrs:inInterceptors>
>              <bean
> class="org.apache.cxf.jaxrs.provider.jsonp.JsonpInInterceptor"/>
>          </jaxrs:inInterceptors>
>          <jaxrs:outInterceptors>
>              <bean
> class="org.apache.cxf.jaxrs.provider.jsonp.JsonpPostStreamInterceptor"/>
>          </jaxrs:outInterceptors>
>      </jaxrs:server>
>
> Notes: JsonpPreStreamInterceptor must include in <jaxrs:providers> rather
> than <jaxrs:outInterceptors>.
>
I've checked the code, it implements JAX-RS WriteInterceptor but also 
CXF Interceptor so it should work if it is registered inside outInterceptors

Thanks, Sergey
>
>
>
> --
> View this message in context: http://cxf.547215.n5.nabble.com/JSONP-is-not-works-tp5739858p5745735.html
> Sent from the cxf-user mailing list archive at Nabble.com.
>

Re: JSONP is not works

Posted by Jack Huang <hu...@gmail.com>.
Dear Sergey

Thank you for your help, I have upgraded CXF 3.0.0, and the JSONP is very
great now!

Maven configuration:
        
        <dependency>
            <groupId>org.apache.cxf</groupId>
            <artifactId>cxf-rt-frontend-jaxrs</artifactId>
            <version>${cxf.version}</version>
        </dependency>
        <dependency>
            <groupId>org.apache.cxf</groupId>
            <artifactId>cxf-rt-rs-extension-providers</artifactId>
            <version>${cxf.version}</version>
        </dependency>
        
        <dependency>
            <groupId>com.fasterxml.jackson.jaxrs</groupId>
            <artifactId>jackson-jaxrs-json-provider</artifactId>
            <version>${jackson.version}</version>
        </dependency>

Spring configuration:
    <jaxrs:server address="/rest">
        <jaxrs:serviceBeans>
            <ref bean="productServiceImpl"/>
        </jaxrs:serviceBeans>
        <jaxrs:providers>
            <bean
class="com.fasterxml.jackson.jaxrs.json.JacksonJsonProvider"/>
            <bean
class="org.apache.cxf.jaxrs.provider.jsonp.JsonpPreStreamInterceptor"/>
        </jaxrs:providers>
        <jaxrs:inInterceptors>
            <bean
class="org.apache.cxf.jaxrs.provider.jsonp.JsonpInInterceptor"/>
        </jaxrs:inInterceptors>
        <jaxrs:outInterceptors>
            <bean
class="org.apache.cxf.jaxrs.provider.jsonp.JsonpPostStreamInterceptor"/>
        </jaxrs:outInterceptors>
    </jaxrs:server>

Notes: JsonpPreStreamInterceptor must include in <jaxrs:providers> rather
than <jaxrs:outInterceptors>.




--
View this message in context: http://cxf.547215.n5.nabble.com/JSONP-is-not-works-tp5739858p5745735.html
Sent from the cxf-user mailing list archive at Nabble.com.

Re: JSONP is not works

Posted by Sergey Beryozkin <sb...@gmail.com>.
Hi
On 03/03/14 09:51, Jack Huang wrote:
> Hi,
>
> It not works. :(
>
> I use 2.7.9 version and set JsonpInInterceptor's acceptType value to
> "application/json".
> The JSON data is presented, but without callback function.
>
> You can try it again. Thanks!
>
I've got it fixed:
http://git-wip-us.apache.org/repos/asf/cxf/commit/aebae33d

Note JsonpPreStreamInterceptor need to register as jaxrs:provider as 
opposed to jaxrs:outInterceptor.

The reason for it is that CXF out pre stream interceptors run before 
JAX-RS message body writers are selected. Having 
JsonpPreStreamInterceptor registered as JAX-RS WriterInterceptor
lets us select Jackson still expecting application/json and then set a 
proper JSONP response type

I suspect it probably never worked with Jackson but now we have a test 
confirming it works...

Thanks, Sergey

> Jack Huang
>
>
>
> --
> View this message in context: http://cxf.547215.n5.nabble.com/JSONP-is-not-works-tp5739858p5740684.html
> Sent from the cxf-user mailing list archive at Nabble.com.
>