Guide to JAXB
https://www.baeldung.com/jaxbSample 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
Convert POJO to JSON
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 {