Searching...
Friday, 19 July 2013

JAX-RS Web Service + JAXB Example

11:44
JAX-RS Web Service + JAXB Example

JAXB API helps to map java object to xml and xml to java objects, in this example we are using JAXB to convert java objects to xml  
 
In this example we will see how the object to xml mapping is working using JAXB.
 

1. Create a maven project using the quick start archetype and add below dependencies in your pom.xml file
<dependency>
<groupId>org.eclipse.jetty</groupId>
<artifactId>jetty-server</artifactId>
<version>9.2.3.v20140905</version>
</dependency>
<dependency>
<groupId>org.eclipse.jetty</groupId>
<artifactId>jetty-servlet</artifactId>
<version>9.2.3.v20140905</version>
</dependency>
<dependency>
<groupId>org.glassfish.jersey.core</groupId>
<artifactId>jersey-server</artifactId>
<version>2.7</version>
</dependency>
<dependency>
<groupId>org.glassfish.jersey.containers</groupId>
<artifactId>jersey-container-servlet-core</artifactId>
<version>2.7</version>
</dependency>
<dependency>
<groupId>org.glassfish.jersey.containers</groupId>
<artifactId>jersey-container-jetty-http</artifactId>
<version>2.7</version>
</dependency>
<dependency>
<groupId>org.glassfish.jersey.media</groupId>
<artifactId>jersey-media-moxy</artifactId>
<version>2.7</version>
</dependency>

2. Create a simple service 

package com.vinod.vinod_rest_examples;

import javax.ws.rs.GET;
import javax.ws.rs.Path;
import javax.ws.rs.PathParam;
import javax.ws.rs.Produces;
import javax.ws.rs.core.MediaType;
@Path("/state")
public class JaxRsJaxBController {
    //http://localhost:8080/state/KL
    @GET
    @Path("/{param}")
    @Produces(MediaType.APPLICATION_XML)
    public State getStateXML(@PathParam("param") String code) {
        State state = new State();
        state.setCode("KL");
        if (code.equals("KL")) {
            state.setName("KERALA");
        }
        return state;
    }
}
  

3. Create a java class to start jetty server

package com.vinod.vinod_rest_examples;

import org.eclipse.jetty.server.Server;
import org.eclipse.jetty.servlet.ServletContextHandler;
import org.eclipse.jetty.servlet.ServletHolder;

/**
 * Hello world!
 *
 */

public class App {
    public static void main(String[] args) {
        ServletContextHandler context = new ServletContextHandler(ServletContextHandler.SESSIONS);
        context.setContextPath("/");

        Server jettyServer = new Server(8080);
        jettyServer.setHandler(context);

        ServletHolder jerseyServlet = context.addServlet(org.glassfish.jersey.servlet.ServletContainer.class, "/*");
        jerseyServlet.setInitOrder(0);

        // Setting pacakge name over here to load services
        jerseyServlet.setInitParameter("jersey.config.server.provider.packages", "com.vinod.vinod_rest_examples");
        try {
            jettyServer.start();
            jettyServer.join();
        } catch (Exception e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        } finally {
            jettyServer.destroy();
        }
    }
}
  

4. Hit the urls for testing

 
 
 
 

0 comments: