20

Sorry for this blunt question . But many use these 2 terms day in and day out yet I don't know .I did some study on this and knew what it is separately . But don't understand how it is related . I will share what I understood about these two first .

JAXB is XML-to-Java binding technology enabling transformations between schema and Java objects and between XML instance documents and Java object instances. Internally JAXB does all this conversions between xml and java . This is a parser of xml and then it knows what component in xml corresponds to what in java and it breaks . Conversion of this answer from JAXB is done by tools like xjc ( or codgen plugin) . Mapping may be like

xsd:string java.lang.String

xsd:integer java.math.BigInteger

JaxRs is different . This is set of specifications for handling requests . Meaning that it says "GET("/foo") " means handle a get call with url /foo . It only states that . How it is done ? Yes , that is called implementation of this spec . There are number of implementations like restlet , resteasy , jersey , apache cxf etc . This is analogus to logic and way you implement in maths . the algorithm idea is bucket search .This can be implemented in any way . In java terms JaxRs is interface and these 4 restlet , resteasy , jersey , apache cxf are implementations of the interface .

Now please say if my understanding is correct . Then tell how they are related . Please help . If possible a pictorial explanation will be more helpful.

Harish Kayarohanam
  • 3,556
  • 2
  • 23
  • 53

1 Answers1

20

Your understanding is basically correct. JAXB and JAX-RS are both Java Community Process (JCP) standards with multiple implementations.

JAXB - Defines standardized metadata and runtime API for converting Java domain objects to/from XML.

JAX-RS - Defines standardized metadata and runtime API for the creation of RESTful services. By default for the application/xml media type JAX-RS will use JAXB to convert the objects to/from XML.

Example

In the following example when a GET operation is performed the JAX-RS implementation will return a Customer. A JAXB implementation will be used to convert that instance of Customer to the XML that the client will actually receive.

import javax.ws.rs.*;
import javax.ws.rs.core.MediaType;

@Path("/customers")
public class CustomerResource {

    @GET
    @Produces(MediaType.APPLICATION_XML)
    @Path("{id}")
    public Customer read(@PathParam("id") int id) {
        Customer customer = new Customer();
        customer.setId(id);
        customer.setFirstName("Jane");
        customer.setLastName(null);

        PhoneNumber pn = new PhoneNumber();
        pn.setType("work");
        pn.setValue("5551111");
        customer.getPhoneNumbers().add(pn);

        return customer;
     }

}
Koray Tugay
  • 20,438
  • 37
  • 155
  • 276
bdoughan
  • 142,244
  • 22
  • 280
  • 377
  • Thank you very much . But I have 2 questions . 1) what do you mean by the statement "By default for the application/xml media type JAX-RS will use JAXB ". Then what happens for other media types . 2) what do you emphasize by the example you quoted . Please clarify. – Harish Kayarohanam Jul 31 '13 at 21:17
  • Ya second question of mine in comment 1 got cleared . But above the example you said "JAXB impl will be used to convert that instance of Customer to the XML " . Is this code internal or is it in the example code snippet itself? If in example can you tell the line number ? – Harish Kayarohanam Jul 31 '13 at 21:20
  • You can override the default handling of `application/xml` by providing a custom `MessageBodyReader`/`MessageBodyWriter`. For other media types it dependends. The `application/json` media type is popular but JAX-RS does not define what the default binding should be, implementations have come up with there own defaults. Here is an example: http://blog.bdoughan.com/2013/07/oracle-weblogic-1212-now-with.html – bdoughan Jul 31 '13 at 21:23
  • 1
    @HarishKayarohanam - By default all the JAXB interaction is done for you behind the scenes. Even though you don't touch the JAXB code directly any JAXB metadata you provide on the class will be used. – bdoughan Jul 31 '13 at 21:27
  • 1
    Oh ok . Thank you very much . – Harish Kayarohanam Jul 31 '13 at 21:44
  • Can we use JAX-RS to have a service which can accept both SOAP and REST api's? Like you mentioned jaxrs will convert class to xml. But I want to have a application which will support REST and SOAP calls. Is it possible using JAX-RS and JAXB? – smilu May 25 '21 at 08:43