Note that there are some explanatory texts on larger screens.

plurals
  1. POHow to reuse Jersey's JSON/JAXB for serialization?
    primarykey
    data
    text
    <p>I have a JAX-RS REST service implemented using Jersey. One of the cool features of JAX-RS/Jersey is how easily a POJO can be turned into a REST service, simply by sprinkling a few Java annotations... including a trivially easy mechanism for translating POJOs to JSON - using JAXB annotations.</p> <p>Now, I'd like to be able to take advantage of this cool JSON-ifying functionality for non-REST purposes - I'd love to be able to just serialize some of these objects to disk, as JSON text. Here's an example JAXB object that I'd want to serialize:</p> <pre><code>@XmlRootElement(name = "user") public class UserInfoImpl implements UserInfo { public UserInfoImpl() {} public UserInfoImpl(String user, String details) { this.user = user; this.details = details; } public String getUser() { return user; } public void setUser(String user) { this.user = user; } public String getDetails() { return details; } public void setDetails(String details) { this.details = details; } private String user; private String details; } </code></pre> <p>Jersey can turn one of these into json with no additional info. I'm wondering if Jersey has exposed this functionality in the API for needs like mine? I've had no luck finding it so far...</p> <p>Thanks!</p> <p><strong>UPDATE 2009-07-09</strong>: I have learned that I can use the Providers object to <em>almost</em> do exactly what I want: </p> <pre><code> @Context Providers ps; MessageBodyWriter uw = ps.getMessageBodyWriter(UserInfoImpl.class, UserInfoImpl.class, new Annotation[0], MediaType.APPLICATION_JSON_TYPE); uw.writeTo(....) </code></pre> <p>... This writes the object as json to any outputstream, which would be perfect for me, but I can only get at the Providers object using @Context from a @Component object. Does anyone know how to access it from a regular, un-annotated POJO? Thanks!</p>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload