So far in this blog, we have been learning about building RESTful webservices which are server side components. In this post, we will learn to build a RESTful client for consuming the webservices written in previous posts.
I will be re-using the code base written for RESTEasy + JAXB xml example. I will build a pure java API client without using any third party tools.
1) Build the RESTful webservice API
Follow the steps given in RESTEasy + JAXB xml example
For reference, service and model classes are:
UserManagementModule.java
package com.howtodoinjava.service; 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.Response; import com.howtodoinjava.model.User; @Path("/user-management") public class UserManagementModule { @GET @Path("/users/{id}") @Produces("application/xml") public Response getUserById(@PathParam("id") Integer id) { User user = new User(); user.setId(id); user.setFirstName("Lokesh"); user.setLastName("Gupta"); return Response.status(200).entity(user).build(); } }
User.java
package com.howtodoinjava.model; import java.io.Serializable; import javax.xml.bind.annotation.XmlAccessType; import javax.xml.bind.annotation.XmlAccessorType; import javax.xml.bind.annotation.XmlAttribute; import javax.xml.bind.annotation.XmlElement; import javax.xml.bind.annotation.XmlRootElement; @XmlAccessorType(XmlAccessType.NONE) @XmlRootElement(name = "user") public class User implements Serializable { private static final long serialVersionUID = 1L; @XmlAttribute(name = "id") private int id; @XmlElement(name = "firstName") private String firstName; @XmlElement(name = "lastName") private String lastName; public int getId() { return id; } public void setId(int id) { this.id = id; } public String getFirstName() { return firstName; } public void setFirstName(String firstName) { this.firstName = firstName; } public String getLastName() { return lastName; } public void setLastName(String lastName) { this.lastName = lastName; } }
2) Create the RESTFul webservices consumer client
Our java client is based on java.net package APIs. I am doing two steps here:
- Capture the output in string format
- Unmarshal the model object from xml response
package test; import java.io.BufferedReader; import java.io.IOException; import java.io.InputStreamReader; import java.io.StringReader; import java.net.HttpURLConnection; import java.net.MalformedURLException; import java.net.URL; import javax.xml.bind.JAXBContext; import javax.xml.bind.JAXBException; import javax.xml.bind.Unmarshaller; import com.howtodoinjava.model.User; public class PureJavaClient { public static void main(String[] args) { try { URL url = new URL("http://localhost:8080/RESTfulDemoApplication/user-management/users/10"); HttpURLConnection conn = (HttpURLConnection) url.openConnection(); conn.setRequestMethod("GET"); conn.setRequestProperty("Accept", "application/xml"); if (conn.getResponseCode() != 200) { throw new RuntimeException("Failed : HTTP error code : " + conn.getResponseCode()); } BufferedReader br = new BufferedReader(new InputStreamReader((conn.getInputStream()))); String apiOutput = br.readLine(); System.out.println(apiOutput); conn.disconnect(); JAXBContext jaxbContext = JAXBContext.newInstance(User.class); Unmarshaller jaxbUnmarshaller = jaxbContext.createUnmarshaller(); User user = (User) jaxbUnmarshaller.unmarshal(new StringReader(apiOutput)); System.out.println(user.getId()); System.out.println(user.getFirstName()); System.out.println(user.getLastName()); } catch (MalformedURLException e) { e.printStackTrace(); } catch (IOException e) { e.printStackTrace(); } catch (JAXBException e) { e.printStackTrace(); } } } Output: <?xml version="1.0" encoding="UTF-8" standalone="yes"?><user id="10"><firstName>Lokesh</firstName><lastName>Gupta</lastName></user> 10 Lokesh Gupta
Follow the below link for downloading the source code of this example.
Source code download
Happy Learning !!