Venkat Nandanavanam
Many enterprise applications use Marshalling and unmarshalling i.e. XML to POJO and POJO to XML. This tutorial will try show how to do Marshalling and unmarshalling using JAXB and RESTEasy.
Technologies used
Now let us declare our domain objects to represent this XML
order.java
Now this is how our RESTEasy service class will look like OrderSrvc.java
web.xml
pom.xml
Now biuld, deploy and start the server
After starting the server, try this URL
Now lets try to convert XML to Java object. I used REST client firefox plugin to post XML. Do remember to add request header for content type before posting XML
This brings us to the end of the tutorial. I will post more examples on RestEasy, HornetQ, and JBOSS in the future. If you want any specific tutorial let me know in the comments section.
Dear Readers, kindly like us on facebook to see whats happening on this blog
Many enterprise applications use Marshalling and unmarshalling i.e. XML to POJO and POJO to XML. This tutorial will try show how to do Marshalling and unmarshalling using JAXB and RESTEasy.
Technologies used
- JAVA
- RESTEasy
- JAXB
- JBOSS eap 6.1
- Maven
Mumbai 300 f238 OR908765
Now let us declare our domain objects to represent this XML
order.java
package itsvenkis.blogspot.in.domain; import javax.xml.bind.annotation.XmlElement; import javax.xml.bind.annotation.XmlRootElement; /** * @author itsvenkis * */ @XmlRootElement(name ="order") public class Order { private String flightNumber; private String orderId; private String source; private String destination; private Flight flight; public Flight getFlight() { return flight; } @XmlElement public void setFlight(Flight flight) { this.flight = flight; } public String getFlightNumber() { return flightNumber; } @XmlElement public void setFlightNumber(String flightNumber) { this.flightNumber = flightNumber; } public String getOrderId() { return orderId; } @XmlElement public void setOrderId(String orderId) { this.orderId = orderId; } public String getSource() { return source; } @XmlElement public void setSource(String source) { this.source = source; } public String getDestination() { return destination; } @XmlElement public void setDestination(String destination) { this.destination = destination; } }Flight.java
package itsvenkis.blogspot.in.domain; import javax.xml.bind.annotation.XmlAttribute; import javax.xml.bind.annotation.XmlElement; /** * @author itsvenkis * */ public class Flight { private String flightMaker; private String flightType; private int flightAge; public int getFlightAge() { return flightAge; } @XmlElement public void setFlightAge(int flightAge) { this.flightAge = flightAge; } @XmlAttribute public String getFlightMaker() { return flightMaker; } public void setFlightMaker(String flightMaker) { this.flightMaker = flightMaker; } @XmlAttribute public String getFlightType() { return flightType; } public void setFlightType(String flightType) { this.flightType = flightType; } }
Now this is how our RESTEasy service class will look like OrderSrvc.java
package itsvenkis.blogspot.in.rest.services; import itsvenkis.blogspot.in.domain.Flight; import itsvenkis.blogspot.in.domain.Order; import javax.ws.rs.Consumes; import javax.ws.rs.GET; import javax.ws.rs.POST; import javax.ws.rs.Path; import javax.ws.rs.Produces; import javax.ws.rs.core.MediaType; import javax.ws.rs.core.Response; @Path("/getOrder") public class OrderSrvc { @GET @Produces("application/xml") public Order getOrder(){ Order order = new Order(); order.setDestination("Mumbai"); order.setFlightNumber("f238"); order.setOrderId("OR908765"); Flight flight = new Flight(); flight.setFlightMaker("BOEING"); flight.setFlightType("COMMERCIAL"); flight.setFlightAge(300); order.setFlight(flight); return order; } @POST @Consumes(MediaType.APPLICATION_XML) public Response postOrder(Order order){ //Its crime to use System.out.println . Use loggers instead System.out.println(order.getDestination()); return Response.status(200).entity("Received XML").build(); } }Now lets go ahead and define our web.xml and pom.xml
web.xml
resteasy.resources itsvenkis.blogspot.in.rest.services.OrderSrvc resteasy.servlet.mapping.prefix /resteasy org.jboss.resteasy.plugins.server.servlet.ResteasyBootstrap resteasy org.jboss.resteasy.plugins.server.servlet.HttpServletDispatcher resteasy /resteasy/*
pom.xml
4.0.0 in.itsvenkis.blogspot flight-booking 0.0.1-SNAPSHOT war JBOSS-EXAMPLES Simple application to understand RESTEasy,Hornet Q jboss http://repository.jboss.org/maven2 flightbooking org.jboss.resteasy resteasy-jaxrs 3.0.0.Final org.jboss.resteasy resteasy-jaxb-provider 3.0.0.Final
Now biuld, deploy and start the server
After starting the server, try this URL
Now lets try to convert XML to Java object. I used REST client firefox plugin to post XML. Do remember to add request header for content type before posting XML
This brings us to the end of the tutorial. I will post more examples on RestEasy, HornetQ, and JBOSS in the future. If you want any specific tutorial let me know in the comments section.
Dear Readers, kindly like us on facebook to see whats happening on this blog
No comments:
Post a Comment