You are viewing a plain text version of this content. The canonical link for it is here.
Posted to users@cxf.apache.org by "vlad.balan" <vl...@gmail.com> on 2018/06/21 14:14:02 UTC

Generate JSON when using JAXB annotated classes

Hello

thanks whoever reads this post.

I understodd from the JEE 7 tutorial that whenever you use JAXB classes as
parameters or return types
you can also use the them for generating/reading JSON.

It's enough to add "application/json" :

@Produces({"application/xml","application/json"}) or
@Consumes({"application/xml","application/json"})


But i tried this in CXF JAX-RS and it didn't work without adding the Jackson
JSON provider.



I cite the tutorial:

The JEE tutorial says in chapter 31.7.3

"
JAX-RS can automatically read and write XML using JAXB, but it can also work
with
JSON data


.....


You can add the format application/json or MediaType.APPLICATION_JSON to the
@Produces annotation in resource methods to produce responses with JSON
data:


@GET
@Path("/get")
@Produces({"application/xml","application/json"})
public Product getProduct() { ... }


In this example, the default response is XML, but the response is a JSON
object if the
client makes a GET request that includes this header:
Accept: application/json


"


Thanks.





--
Sent from: http://cxf.547215.n5.nabble.com/cxf-user-f547216.html

Re: Generate JSON when using JAXB annotated classes

Posted by "vlad.balan" <vl...@gmail.com>.
Thanks a lot Andy, that was useful.

The question is still another one: in JAVA EE 7 tutorial, in the section
that i indicated, it says that you can use same JAX-B classes and method
signature as if the call was for application/xml and in fact generate (or
read) application/json.

I didn't try that with Jersey i tried with CXF and it didn't generate
without adding the JSON provider.

And i think i just come to understand:

- yes i have to manually add the provider
- but IT DOES generate JSON using the same method signature as for xml, that
is the one with the JAX-B classes

It's my fault, i didn't try after ading the JSON provider to see that 

- simply by adding @Produces application/json and 
- making a GET with accept: application/json

IT WILL work!!

Thanks again.

OK, i got it.


Thanks a lot again.



--
Sent from: http://cxf.547215.n5.nabble.com/cxf-user-f547216.html

Re: Generate JSON when using JAXB annotated classes

Posted by Andy McCright <j....@gmail.com>.
Hi Vlad,

JSON binding providers (like Jackson, JSON-B implementations, etc.) are not
required by the JAX-RS specification.  Section 4.2.4 of the spec lists the
provider media types that must be supported by a compliant JAX-RS
implementation - and most (if not all) of them are types that are built-in
to the JDK - XML, plain text, etc.

Some vendors that package CXF (WebSphere Liberty, for sure - and possibly
TomEE and others) will also package built-in providers that will handle
JSON binding without additional config or libraries.  But if you are using
CXF outside of these vendors, then you will need to package and register
your own provider for JSON (Jackson is probably the best for EE7, but if
you are looking at Java EE8 you may want to consider JSON-B).

As for choosing the response, that must be specified by the client in the
Accept header.  If this header is left empty, then the implementation (CXF)
is free to choose any of the media types from the @Produces annotation.  My
guess is that it will choose the first media type listed in the annotation,
but if you want your app to be portable, you should not rely on that
behavior.  If you only want to respond back with JSON, then I would suggest
removing the "application/xml" media type from the @Produces annotation.
If you want to support both JSON and XML, but always use JSON when the
client has not specified an Accept header, then I would suggest adding a
ContainerRequestFilter to the application that will check for the existence
of the Accept header, and add "Accept: application/json" if the header does
not already exist.

Hope this helps,

Andy

On Thu, Jun 21, 2018 at 9:14 AM vlad.balan <vl...@gmail.com> wrote:

> Hello
>
> thanks whoever reads this post.
>
> I understodd from the JEE 7 tutorial that whenever you use JAXB classes as
> parameters or return types
> you can also use the them for generating/reading JSON.
>
> It's enough to add "application/json" :
>
> @Produces({"application/xml","application/json"}) or
> @Consumes({"application/xml","application/json"})
>
>
> But i tried this in CXF JAX-RS and it didn't work without adding the
> Jackson
> JSON provider.
>
>
>
> I cite the tutorial:
>
> The JEE tutorial says in chapter 31.7.3
>
> "
> JAX-RS can automatically read and write XML using JAXB, but it can also
> work
> with
> JSON data
>
>
> .....
>
>
> You can add the format application/json or MediaType.APPLICATION_JSON to
> the
> @Produces annotation in resource methods to produce responses with JSON
> data:
>
>
> @GET
> @Path("/get")
> @Produces({"application/xml","application/json"})
> public Product getProduct() { ... }
>
>
> In this example, the default response is XML, but the response is a JSON
> object if the
> client makes a GET request that includes this header:
> Accept: application/json
>
>
> "
>
>
> Thanks.
>
>
>
>
>
> --
> Sent from: http://cxf.547215.n5.nabble.com/cxf-user-f547216.html
>