You are viewing a plain text version of this content. The canonical link for it is here.
Posted to users@cxf.apache.org by Timo Hosters <th...@orga-systems.com> on 2014/06/26 10:23:20 UTC
Question: Getting XML instead of JSON - wrong JAXB-/JSON-provider
setup?
The context
I've written a RESTful web service with CXF which has to process and
returns either XML or JSON.
My configuration-file contains JAXB- and a JSON-provider to add e. g.
validation against a schema, dropping/adding namespaces and so on.
The class is annotated with the according "@consumes", "@produces" and
content-types ("{MediaType.APPLICATION_XML,MediaType.APPLICATION_JSON}").
The problem:
There is a strange behavior I fail to fix: If I test it with a JSON message
(and "content-type: application/JSON"), CXF will process it but return an
XML-message (together with "content-type: application/XML").
(I also managed to have it the other way round - request with XML, but
response was JSON. Apparently it can be provoked by changing orders of the
content-types in the annotations - and/or the order of the providers in the
configuration-file - I cannot confirm that.)
My guess...
... is that the JSON provider does not receive the request (or passes it
on). The request eventually goes on to the XML provider, which processes it
instead (or additionally). Hence there is only the XML-payload +
"content-type: application/XML" in the response.
However I cannot see why; searching on the forum (like this post
<http://cxf.547215.n5.nabble.com/Getting-JSON-instead-of-XML-at-marshalling-step-td565312.html>
) and the web did not help.
*What could be the issue?* I'm running out of ideas for this, so *I
appreciate any hints and pointers.*
My current configuration/setup
I'm using
CXF 2.7.11 (3.0.0 lead to the same results)
JDK 1.7
Tomcat-7.0.53
SoapUI 5.0.0 for testing
Here are the according sections of the source- and configuration-files:
AccountApiV1.java:
beans.xml:
--
View this message in context: http://cxf.547215.n5.nabble.com/Question-Getting-XML-instead-of-JSON-wrong-JAXB-JSON-provider-setup-tp5745600.html
Sent from the cxf-user mailing list archive at Nabble.com.
Re: Question: Getting XML instead of JSON - wrong
JAXB-/JSON-provider setup?
Posted by Timo Hosters <th...@orga-systems.com>.
Hi again.
Thanks for the explanation - and the additional info on solving it with a
filter.
That's good to know in case a use case for it comes up in the future.
Best regards,Timo
--
View this message in context: http://cxf.547215.n5.nabble.com/Question-Getting-XML-instead-of-JSON-wrong-JAXB-JSON-provider-setup-tp5745600p5745698.html
Sent from the cxf-user mailing list archive at Nabble.com.
Re: Question: Getting XML instead of JSON - wrong JAXB-/JSON-provider
setup?
Posted by Sergey Beryozkin <sb...@gmail.com>.
Hi
On 26/06/14 14:02, Timo Hosters wrote:
> Hello and thanks for your quick reply.
> If I manually set the the Accept-header to "application/json" (resp.
> "application/xml") in Soap UI, everything works as expected. So that is the
> *most obvious solution* to that issue.
> Thank you, Mr. Beryozkin!
> I wonder: Is there a way CXF can determine the acceptable content-type
> without an Accept-header, maybe based on the payload or a different
> configuration of JSON-/JAXB-Providers?
> This issue kept me on my toes for quite some time, even looking for other
> culprits in different directions (like missing "propOrder" in the
> annotations; cf. below). So as lessons-learned from that, here are *some
> other remarks/hints that might help people* with future (Soap UI) issues:
> As just proven today: Regardless how you set the media-type in Soap UI, it
> will NOT automatically set an according Accept-header. So keep you eyes
> peeled for that.
> Furthermore: Soap UI can and will show you the JSON-aquivalent of XML if you
> are in the JSON-tab. Thus you might think everything works. The clue that it
> is not is the unexpected content-type in the header of the response.
> The same applies if you expect XML but get JSON: Soap UI will show you XML.
> But the elements are not in the proper order as specified by your XSD -> do
> not blame CXF or a annotation specifying a "propOrder"; it's Soap UI's
> doing.
>
No, CXF can't make the decision on what exactly the user expects based
on the input content type if Accept is missing. But a custom filter can
easily set HTTP Accept if preferred
Cheers, Sergey
>
>
>
> --
> View this message in context: http://cxf.547215.n5.nabble.com/Question-Getting-XML-instead-of-JSON-wrong-JAXB-JSON-provider-setup-tp5745600p5745627.html
> Sent from the cxf-user mailing list archive at Nabble.com.
>
Re: Question: Getting XML instead of JSON - wrong
JAXB-/JSON-provider setup?
Posted by Timo Hosters <th...@orga-systems.com>.
Hello and thanks for your quick reply.
If I manually set the the Accept-header to "application/json" (resp.
"application/xml") in Soap UI, everything works as expected. So that is the
*most obvious solution* to that issue.
Thank you, Mr. Beryozkin!
I wonder: Is there a way CXF can determine the acceptable content-type
without an Accept-header, maybe based on the payload or a different
configuration of JSON-/JAXB-Providers?
This issue kept me on my toes for quite some time, even looking for other
culprits in different directions (like missing "propOrder" in the
annotations; cf. below). So as lessons-learned from that, here are *some
other remarks/hints that might help people* with future (Soap UI) issues:
As just proven today: Regardless how you set the media-type in Soap UI, it
will NOT automatically set an according Accept-header. So keep you eyes
peeled for that.
Furthermore: Soap UI can and will show you the JSON-aquivalent of XML if you
are in the JSON-tab. Thus you might think everything works. The clue that it
is not is the unexpected content-type in the header of the response.
The same applies if you expect XML but get JSON: Soap UI will show you XML.
But the elements are not in the proper order as specified by your XSD -> do
not blame CXF or a annotation specifying a "propOrder"; it's Soap UI's
doing.
--
View this message in context: http://cxf.547215.n5.nabble.com/Question-Getting-XML-instead-of-JSON-wrong-JAXB-JSON-provider-setup-tp5745600p5745627.html
Sent from the cxf-user mailing list archive at Nabble.com.
Re: Question: Getting XML instead of JSON - wrong JAXB-/JSON-provider
setup?
Posted by Sergey Beryozkin <sb...@gmail.com>.
Hi,
Can you please check what is HTTP Accept header set to ?
Or try the browser and add to the URI:
?_type=json
this should set Accept: application/json
and then
?_type=xml
and see what you get
Cheers, Sergey
On 26/06/14 09:23, Timo Hosters wrote:
> The context
> I've written a RESTful web service with CXF which has to process and
> returns either XML or JSON.
> My configuration-file contains JAXB- and a JSON-provider to add e. g.
> validation against a schema, dropping/adding namespaces and so on.
>
> The class is annotated with the according "@consumes", "@produces" and
> content-types ("{MediaType.APPLICATION_XML,MediaType.APPLICATION_JSON}").
> The problem:
> There is a strange behavior I fail to fix: If I test it with a JSON message
> (and "content-type: application/JSON"), CXF will process it but return an
> XML-message (together with "content-type: application/XML").
>
> (I also managed to have it the other way round - request with XML, but
> response was JSON. Apparently it can be provoked by changing orders of the
> content-types in the annotations - and/or the order of the providers in the
> configuration-file - I cannot confirm that.)
> My guess...
> ... is that the JSON provider does not receive the request (or passes it
> on). The request eventually goes on to the XML provider, which processes it
> instead (or additionally). Hence there is only the XML-payload +
> "content-type: application/XML" in the response.
> However I cannot see why; searching on the forum (like this post
> <http://cxf.547215.n5.nabble.com/Getting-JSON-instead-of-XML-at-marshalling-step-td565312.html>
> ) and the web did not help.
>
> *What could be the issue?* I'm running out of ideas for this, so *I
> appreciate any hints and pointers.*
> My current configuration/setup
> I'm using
>
> CXF 2.7.11 (3.0.0 lead to the same results)
> JDK 1.7
> Tomcat-7.0.53
> SoapUI 5.0.0 for testing
>
> Here are the according sections of the source- and configuration-files:
> AccountApiV1.java:
>
> beans.xml:
>
>
>
>
>
> --
> View this message in context: http://cxf.547215.n5.nabble.com/Question-Getting-XML-instead-of-JSON-wrong-JAXB-JSON-provider-setup-tp5745600.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