XmlSlurper

XmlSlurper in Groovy

This class belongs to the  package groovy.util.XmlSlurper. The XmlSlurper is very useful in groovy to handle XML related operations

 

The constructor XmlSlurper() can be used to create a very loose (non-validating and namespace-aware) instance.

XmlSlurper parsers
The parse method has many flavors. The parse method returns a GPathResult object.

parse(File file) : XML file can be parsed directly
parse(InputSource input) : Parse the content of input source
parse(InputStream input) : Any binary input stream which refers to XML
parse(Reader in): Using a Reader instance
parse(String uri): URI eg: http://www.helicaltech.com/test.xml
parseText(String text): A helper method to parse the given text as XML

GPathResult

This is the key object which helps in navigating the xml object parsed by XmlSlurper

Some important method of the GPathResult
breadthFirst(): Provides an Iterator over all the nodes using a breadth-first traversal.
childNodes(): Returns an iterator over the child nodes
children(): Returns the children of this GPathResult as a GPathResult object.
declareNamespace(Map newNamespaceMapping): Adds the specified map of prefix to namespace mappings to this GPathResult.
depthFirst() :Provides an Iterator over all using depth-first traversal.
find(Closure closure) :Returns the first child matching the condition(s) specified in the passed closure.
name(): Returns the name of element
parent(): Returns as parent nodes
size() : Returns the size of this GPathResult.
text(): Returns the text of this GPathResult as a String.

 

nodetree

 

JAX-RS and Jersey api

JAX-RS and jersey

JSR 311 defines REST support in java. JAX-RS uses annotations to define the REST in java classes.

What is Jersey?

  • Jersey  provieds api/library for JSR 311. We can use it in implementing Restful webservices in a Java servlet container.
  • In server side it helps in scanning predefined classes and identifies RESTful resources. In web.xml we need to register the servlet for web application.
  • It also provies a client library to communicate with a RESTful webservice.

 

How does it works?
Let say the application has the following URL as the request from the client/browser.
http://localhost:8080/web-application/rest/rest_request_path

The servlet maps the required class and method to give correct response to the above request.
The annotations in the classes helps the servlet to pick the right mapping.

Using the internal library Java Architecture for XML Binding (JAXB) we can have the
JAX-RS supports the creation of XML and JSON.
Annotation Description 

Annotation  Description Package Detail
@GET helps in handle the GET request (javax.ws.rs.GET)
@POST helps in handling the POST request ( javax.ws.rs.POST)
@Produces it handles the output type that will be generated ( javax.ws.rs.Produces)
@Path it handles the request mapping for specific url ( javax.ws.rs.Path)
@PathParam helps to  inject values from the URL into a method parameter. (javax.ws.rs.PathParam)
@QueryParam if the request have any query parameters then we can use this to handle using this annotation (javax.ws.rs.QueryParam)
@Consumes it specifies MIME media types a REST resource can consume. (javax.ws.rs.Consumes)
@FormParam the REST resources generally consume XML/JSON. Using this we can read form parameters sent in POST requests. (javax.ws.rs.FormParam)
@PUT handles the PUT method (javax.ws.rs.PUT)
@DELETE handles the DELETE method (javax.ws.rs.DELETE)

 

Maven jersey dependency.
A quick google search will give you maven dependency. Depending upon the project requirement you can find one.

Maven dependency for jersey-servlet

<dependency>
<groupId>com.sun.jersey</groupId>
<artifactId>jersey-servlet</artifactId>
<version>1.19</version>
</dependency>

Maven dependency for client
<dependency>
<groupId>org.glassfish.jersey.core</groupId>
<artifactId>jersey-client</artifactId>
<version>2.22.1</version>
</dependency>

 

Code snippets

/Maps the path for BaseURL/rest/myMapping
package com.helical.restExample

@Path("/myMapping")
public class SampleService {
   @GET
   @Produces(MediaType.TEXT_PLAIN)
   public String plainMessage() {
    return "REST WORLD";
   }

   @GET
   @Produces(MediaType.TEXT_XML)
   public String xmlMessage() {
     return "<?xml version=\"1.0\"?>" + "<message>REST WORLD" + "</message>";
   }


   @GET
   @Produces(MediaType.APPLICATION_JSON)
    public String jsonMessage() {
    return "{\"mesaage\" : \"rest world\"}";
  }


   @GET
   @Produces(MediaType.TEXT_HTML)
   public String htmlMessage() {
    return "<html> " + "<title>" + "REST WORLD" + "</title>"
     + "<body><h1>" + "Welcome to the Rest world" + "</body></h1>" + "</html> ";
   }

}

 

 

Registering the api

In the web.xml we need to configure the jersey servlet to help the mapping

<servlet>
<servlet-name>Jersey REST Service</servlet-name>
<servlet-class>org.glassfish.jersey.servlet.ServletContainer</servlet-class>
<init-param>
<param-name>jersey.config.server.provider.packages</param-name>
<param-value>com.helical.restExample</param-value>
</init-param>
<load-on-startup>1</load-on-startup>
</servlet>
<servlet-mapping>
<servlet-name>Jersey REST Service</servlet-name>
<url-pattern>/rest/*</url-pattern>
</servlet-mapping>

 

This is good to go. We can run the webservice application to get the desired result.

Sources internet