Saturday, October 5, 2019

JAX-RS JAXB JSON - convert POJO to JSON

 Guide to JAXB

https://www.baeldung.com/jaxb

Sample code
 https://github.com/eugenp/tutorials/tree/master/jaxb

Microservice sample
https://www.baeldung.com/eclipse-microprofile

In previous versions before Java EE 7 to  serialize a POJO into JSON using JAXB you  had use a custom JAXB runtime like e.g. EclipseLink MOXy:
http://www.adam-bien.com/roller/abien/entry/jaxb_json_pojo_serialization_example

 Weblogic
http://blog.bdoughan.com/2013/07/oracle-weblogic-1212-now-with.html

 https://stackoverflow.com/questions/46813309/weblogic-12-2-1-3-from-moxy-to-jackson

Java EE 6
 https://docs.oracle.com/javaee/6/tutorial/doc/gkknj.html

Java EE 7
https://docs.oracle.com/javaee/7/tutorial/jaxrs-advanced007.htm

 


Using JSON with JAX-RS and JAXB

JAX-RS can automatically read and write XML using JAXB, but it can also work with JSON data. JSON is a simple text-based format for data exchange derived from JavaScript.

For this data
 
 1
  Mattress
  Queen size mattress
  500
 
The equivalent JSON representation is:
{
    "id":"1",
    "name":"Mattress",
    "description":"Queen size mattress",
    "price":500
}
 
You can add the format 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
 
The resource methods can also accept JSON data for JAXB annotated classes:
 
@POST
@Path("/create")
@Consumes({"application/xml","application/json"})
public Response createProduct(Product prod) { ... }
 
The client should include the following header when submitting JSON data with a POST request:

Content-Type: application/json
 
 

Weblogic 12.2.1.2 sample


https://docs.oracle.com/middleware/1221/wls/RESTF/develop-restful-service.htm#RESTF217
 
import javax.ws.rs.GET;
import javax.ws.rs.Produces;
import javax.ws.rs.Path;
...
public class BookmarkResource {
...
    @GET
    @Produces(MediaType.APPLICATION_JSON)
    public JSONObject getBookmark() {
        return asJson();
    }
...
    public JSONObject asJson() {
        try {
            return new JSONObject()
                    .put("userid", bookmarkEntity.getBookmarkEntityPK().getUserid())
                    .put("sdesc", bookmarkEntity.getSdesc())
                    .put("ldesc", bookmarkEntity.getLdesc())
                    .put("uri", bookmarkEntity.getUri());
        } catch (JSONException je){
            return null;
        }
    }
} 
 

Convert Java POJO to JSON- using Response

https://www.baeldung.com/jax-rs-response

@GET
@Path("/pojo")
public Response getPojoResponse() {
    Person person = new Person("Abhinayak", "Nepal");
    return Response
      .status(Response.Status.OK)
      .entity(person)
      .build();
}

https://stackoverflow.com/questions/28079195/how-to-get-json-representation-of-java-objects-in-jax-rs-layer-in-java-ee-7 

@GET
@Path("/books")
@Produces(MediaType.APPLICATION_JSON)
public Response getBooks()  {
    List<Book> listOfBooks = getMiscService().getbooks();
    BooksWrapper result = new BooksWrapper(listOfBooks);
    return Response.status(Response.Status.OK).entity(result).build();
}
 

Convert POJO to JSON using MessageWriter

 

Use MessageBodyWriter - see here https://www.baeldung.com/eclipse-microprofile
 
https://github.com/eugenp/tutorials/blob/master/microprofile/src/main/java/com/baeldung/microprofile/providers/BookMessageBodyWriter.java 


@Provider
@Produces(MediaType.APPLICATION_JSON)
public class BookMessageBodyWriter
  implements MessageBodyWriter {
 

No comments:

Post a Comment