Web Services - SPLessons

Webservices With XML

Home > Lesson > Chapter 15
SPLessons 5 Steps, 3 Clicks
5 Steps - 3 Clicks

Webservices With XML

Webservices With XML

shape Description

As of now SPlessons covered different examples such as downloading and uploading file, this chapter is about how to XML response will generate with RESTfull web services by using jersy, before going to this concept user need to know again what is XML, Jersy, RESfull. Jersey RESTful Web Services system is open source, creation quality, structure for creating RESTful Web Services in Java that offers help for JAX-RS APIs. Jersey system is more than the JAX-RS Reference Implementation. Jersey gives it's own particular API that augment the JAX-RS toolbox with extra elements and utilities to additionally streamline RESTful administration and customer advancement. Jersey additionally uncovered various augmentation SPIs with the goal that engineers may stretch out Jersey to best suit their necessities.

XML

shape Description

The Extensible Markup Language (XML) is a markup dialect that characterizes an arrangement of standards for encoding records in a configuration that is both comprehensible and machine-coherent. The W3C's XML 1.0 Specification and a few other related details—every one of them free open gauges—characterize XML. The plan objectives of XML underline effortlessness and ease of use over the Internet. It is a printed information arrange with solid support through Unicode for various human dialects. Despite the fact that the outline of XML spotlights on archives, the dialect is generally utilized for the representation of subjective information structures, for example, those utilized as a part of web services.

shape Example

To go with the following example user need to download the following libratires along with the JAXB jar also. The following is the structure of an application. pom.xml [xml]<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd"> <modelVersion>4.0.0</modelVersion> <groupId>RestfulXMLExample</groupId> <artifactId>RestfulXMLExample</artifactId> <version>0.0.1-SNAPSHOT</version> <packaging>war</packaging> <repositories> <repository> <id>maven2-repository.java.net</id> <name>Java.net Repository for Maven</name> <url>http://download.java.net/maven/2/</url> <layout>default</layout> </repository> </repositories> <dependencies> <dependency> <groupId>junit</groupId> <artifactId>junit</artifactId> <version>4.8.2</version> <scope>test</scope> </dependency> <dependency> <groupId>com.sun.jersey</groupId> <artifactId>jersey-server</artifactId> <version>1.8</version> </dependency> </dependencies> <build> <finalName>RestfulXMLExample</finalName> <plugins> <plugin> <artifactId>maven-compiler-plugin</artifactId> <configuration> <compilerVersion>1.5</compilerVersion> <source>1.5</source> <target>1.5</target> </configuration> </plugin> </plugins> </build> </project>[/xml] A Project Object Model is the crucial work in Maven. It is a XML record that contains data about the code and design subtle elements utilized by Maven to construct the code. It contains default values for generally extends. Cases for this is the construct catalog, which is focus on; the source registry, that is src/fundamental/java; the test source index, that is src/test/java . Customer.java [java]package com.splessons; import javax.xml.bind.annotation.XmlAttribute; import javax.xml.bind.annotation.XmlElement; import javax.xml.bind.annotation.XmlRootElement; @XmlRootElement(name = "customer") public class Customer { String custName; String custCountry; int custId; @XmlElement public String getCustName() { return custName; } public void setCustName(String custName) { this.custName = custName; } @XmlElement public String getCustCountry() { return custCountry; } public void setCustCountry(String custCountry) { this.custCountry = custCountry; } @XmlAttribute public int getCustId() { return custId; } public void setCustId(int custId) { this.custId = custId; } }[/java] RestfulXMLExample.java [java]package com.splessons; 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("/customers") public class RestfulXMLExample { @GET @Path("/{id}") @Produces(MediaType.APPLICATION_XML) public Customer getCustomerDetails(@PathParam("id") int custId) { Customer cust = new Customer(); cust.setCustName("SPLESSONS"); cust.setCustCountry("USA"); cust.setCustId(custId); return cust; } }[/java] In the above example customer name and country name has given to generate the output in XML file and the functionality of @PathParem to infuse the estimation of URI parameter that characterized in @Path expression, into Java strategy. web.xml [xml]<web-app id="WebApp_ID" version="2.4" xmlns="http://java.sun.com/xml/ns/j2ee" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://java.sun.com/xml/ns/j2ee http://java.sun.com/xml/ns/j2ee/web-app_2_4.xsd"> <display-name>RestPathAnnotationExample</display-name> <servlet> <servlet-name>jersey-serlvet</servlet-name> <servlet-class>com.sun.jersey.spi.container.servlet.ServletContainer</servlet-class> <init-param> <param-name>com.sun.jersey.config.property.packages</param-name> <param-value>com.splessons</param-value> </init-param> <load-on-startup>1</load-on-startup> </servlet> <servlet-mapping> <servlet-name>jersey-serlvet</servlet-name> <url-pattern>/rest/*</url-pattern> </servlet-mapping> </web-app>[/xml] Now compile the code from the server as click on the project and go to Run As and go to Server as follows. When compile the code from the server the XML file will be generated as follows on the server.

Summary

shape Key Points

  • The POM stands for Project Object Model.
  • The POM file will have the data related to project dependencied in the form of XML.
  • The POM file changed from project.xml to POM.xml .