REST - Using Apache Wink (Part 01) [Starting JAX-RS Web Services - 01]
If you want to use REST over SOAP, you can get working with your HTTP methods by using Apache Wink along with your Java projects.
Join the DZone community and get the full member experience.
Join For FreeGitHub Link (Code Samples from the Article & More)
https://github.com/sumithpuri/skp-code-marathon-hochiminh
I built this Product Web Service using Apache Wink. It can add, update, delete and retrieve products in memory, and I intend to explain the basics of REST using Apache Wink and this service. REST stands for Representational State Transfer and is based on the concepts of Roy Fielding’s dissertation work as part of his thesis. It works on HTTP and has the following differences when compared to SOAP:
- Works on HTTP protocol, as compared to SOAP, which has its own protocol.
- Uses HTTP GET, PUT, POST, and DELETE as compared to SOAP which uses only POST.
- It uses the HTTP infrastructure, whereas SOAP is transport neutral.
- The producer and consumer are aware of the content being exchanged.
- It does not support many non-functional requirements or any WS-* standards.
Installations Required
Eclipse 4.2.0.
Apache Wink 1.4.0.
Apache Tomcat 8.0.9.
JDK 1.7.0/JRE 1.7.0.
Server/Web Service
The steps to build the web service using Apache Wink are given below:
Create a Dynamic Web Project in Eclipse.
Build a Core Application that performs CRUD Operations.
- 791
/**
2*
3*/
4package me.sumithpuri.rest.persistence;
5
6
7import java.util.ArrayList;
8import java.util.List;
9
10import me.sumithpuri.rest.vo.Product;
11
12/**
13* @author sumith_puri
14*
15*/
16public class ProductPersistenceManager {
17
18private List<Product> productDatabase = new ArrayList<Product>();
19private static ProductPersistenceManager persistenceManager;
20private static int id=0;
21
22private ProductPersistenceManager() {
23
24}
25
26public void add(Product product) {
27
28System.out.println("database: added one product");
29
30// atomic id creation
31id++;
32product.setId(id);
33productDatabase.add(product);
34}
35
36public List<Product> get() {
37System.out.println("database: retrieved all products");
38return productDatabase;
39}
40
41public void update(long productId, String productName) {
42System.out.println("database: modified one product");
43
44for(int i=0;i<productDatabase.size();i++) {
45
46Product product = productDatabase.get(i);
47if(product.getId()==productId) {
48product.setName(productName);
49productDatabase.remove(i);
50productDatabase.add(i,product);
51}
52}
53return;
54}
55
56public void delete(long productId) {
57System.out.println("database: deleted one product");
58
59for(int i=0;i<productDatabase.size();i++) {
60
61Product product = productDatabase.get(i);
62if(product.getId()==productId) productDatabase.remove(i);
63}
64return;
65}
66
67public static ProductPersistenceManager getInstance() {
68
69if(persistenceManager==null) {
70synchronized(ProductPersistenceManager.class) {
71if(persistenceManager==null) {
72persistenceManager = new ProductPersistenceManager();
73}
74}
75}
76return persistenceManager;
77}
78}
79
Add the Supporting JAR Files in the Build Path.
Create the REST Web Service using Apache Wink (GET, POST, DELETE, PUT).
- x1
package me.sumithpuri.rest.webservice;
2
3
4import java.util.List;
5
6import javax.ws.rs.DELETE;
7import javax.ws.rs.GET;
8import javax.ws.rs.POST;
9import javax.ws.rs.PUT;
10import javax.ws.rs.Path;
11import javax.ws.rs.PathParam;
12import javax.ws.rs.Produces;
13import javax.ws.rs.core.MediaType;
14
15import me.sumithpuri.rest.persistence.ProductPersistenceManager;
16import me.sumithpuri.rest.vo.Product;
17
18/**
19* @author sumith_puri
20*
21*/
22"product") (
23public class ProductWebService {
24
25ProductPersistenceManager persistenceManager = ProductPersistenceManager.getInstance();
26
2728MediaType.TEXT_PLAIN) (
29public String getProducts() {
30
31List<Product> products = persistenceManager.get();
32String productList = new String();
33
34for(Product producti: products) {
35productList+=producti.toString() + "\n";
36}
37
38// return as plain text - other types include xml, json
39return productList;
40}
41
4243public String addProducts(String productStr) {
44
45Product product = new Product();
46product.setName(productStr);
47persistenceManager.add(product);
48
49return productStr;
50}
51
5253"/{id}") (
54public void deleteProduct( (value="id") long id) {
55
56persistenceManager.delete(id);
57return;
58}
59
6061"/{id}") (
62public void modifyProduct( (value="id") long id, String productName) {
63
64persistenceManager.update(id, productName);
65return;
66}
67}
68
Configure the application for Apache Wink
To allow Apache Wink to locate this service as a REST web service, you can either define an additional class or configure an application file. We are using an application file to mention all our web services.
You can place this under WEB-INF/and name the file simply ‘application’ (without any extension).
11me.sumithpuri.rest.webservice.ProductWebService
Next, you need to specify the servlet-related configuration for allowing Apache Wink REST Servlet to locate this application configuration. You do that by specifying the location of ‘application’ as the parameter ‘applicationConfigLocation’ in WEB-INF/web.xml1612<web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns="http://java.sun.com/xml/ns/javaee" xmlns:web="http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd" xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_3_0.xsd" id="WebApp_ID" version="3.0">
3<display-name>products</display-name>
4<servlet>
5<servlet-name>restService</servlet-name>
6<servlet-class>org.apache.wink.server.internal.servlet.RestServlet</servlet-class>
7<init-param>
8<param-name>applicationConfigLocation</param-name>
9<param-value>/WEB-INF/application</param-value>
10</init-param>
11</servlet>
12<servlet-mapping>
13<servlet-name>restService</servlet-name>
14<url-pattern>/rest/*</url-pattern>
15</servlet-mapping>
16</web-app>
Deploy as an Apache Tomcat Web App, Directly:
Client/Web Service Client
The steps to test the web service or write a REST client are as follows:
Create a Java project in Eclipse.
Include the Support Client JAR files in the Classpath.
Build the Client to Test or Access the GET method.
Build the Client to Test or Access the POST method.
Build the Client to Test or Access the DELETE method.
Build the Client to Test or Access the PUT method.
1281package me.sumithpuri.rest.client;
2
3import javax.ws.rs.core.MediaType;
4
5import org.apache.wink.client.ClientConfig;
6import org.apache.wink.client.Resource;
7import org.apache.wink.client.RestClient;
8
9
10/**
11* @author sumith_puri
12*
13*/
14class ProductRESTClient {
15
16static String REST_WEB_SERVICE="http://localhost:8080/products/rest/product";
17static ClientConfig clientConfig = new ClientConfig();
18
19/**
20* @param args
21*/
22public static void main(String[] args) throws Exception {
23
24try {
25
26ProductRESTClient restClient = new ProductRESTClient();
27System.out.println("Apache Wink Based REST Client");
28System.out.println("Sumith Kumar Puri (c) 2015");
29System.out.println("=============================");
30
31restClient.configureClient();
32System.out.println();
33
34restClient.invokeGET();
35System.out.println();
36
37String product="Sumith Puri" + (int) (Math.random()*9999);
38restClient.invokePOST(product);
39
40System.out.println();
41product="Sumith Puri" + (int) (Math.random()*9999);
42restClient.invokePOST(product);
43
44System.out.println();
45product="Sumith Puri" + (int) (Math.random()*9999);
46restClient.invokePOST(product);
47
48System.out.println();
49product="Sumith Puri" + (int) (Math.random()*9999);
50restClient.invokePOST(product);
51
52System.out.println();
53restClient.invokeGET();
54
55System.out.println();
56restClient.invokeDELETE(2L);
57
58System.out.println();
59restClient.invokeGET();
60
61System.out.println();
62product="Sumith Puri" + (int) (Math.random()*9999);
63restClient.invokePOST(product);
64
65System.out.println();
66product="Sumith Puri" + (int) (Math.random()*9999);
67restClient.invokePOST(product);
68
69System.out.println();
70restClient.invokeDELETE(4L);
71
72System.out.println();
73restClient.invokeGET();
74
75System.out.println();
76restClient.invokePUT(3L,"Sumith Puri");
77
78System.out.println();
79restClient.invokeGET();
80} catch (Exception e) {
81
82e.printStackTrace();
83}
84}
85
86
87public void configureClient() {
88
89}
90
91public void invokeGET() {
92
93System.out.println("Testing GET command....");
94RestClient restClient = new RestClient(clientConfig);
95Resource resource = restClient.resource(REST_WEB_SERVICE);
96String response = resource.accept("text/plain").get(String.class);
97System.out.printf(response);
98System.out.println("...GET command is successful");
99}
100
101public void invokePOST(String product) {
102
103System.out.println("Testing POST command...");
104RestClient restClient = new RestClient(clientConfig);
105Resource resource = restClient.resource(REST_WEB_SERVICE);
106resource.contentType(MediaType.TEXT_PLAIN).accept(MediaType.TEXT_PLAIN).post(String.class,product);
107System.out.println("...POST command is successful");
108}
109
110
111public void invokePUT(Long id, String productName) {
112
113System.out.println("Testing PUT command...");
114RestClient restClient = new RestClient(clientConfig);
115Resource resource = restClient.resource(REST_WEB_SERVICE+"/"+id);
116resource.contentType(MediaType.TEXT_PLAIN).accept(MediaType.TEXT_PLAIN).put(String.class, productName);
117System.out.println("...PUT command is successful");
118}
119
120public void invokeDELETE(Long id) {
121
122System.out.println("Testing DELETE command...");
123RestClient restClient = new RestClient(clientConfig);
124Resource resource = restClient.resource(REST_WEB_SERVICE+"/"+id);
125resource.contentType(MediaType.TEXT_PLAIN).accept(MediaType.TEXT_PLAIN).delete();
126System.out.println("...DELETE command is successful");
127}
128}
Output (First Access)
Downloads
You may download the products.war (including source) web service as well as the products-client.jar (including source).
Conclusion
In this blog, you have seen how to build REST web services using Apache Wink for basic data types. Next, I will blog on how to include Jackson or Jettison as the stream reader or stream writer JSON libraries so that we can read and write complex or application object types. It will be titled "JAX-RS (REST Web Services) using Apache Wink - 02" [REST Using Apache Wink and Jackson/Jettison].
Published at DZone with permission of Sumith Puri. See the original article here.
Opinions expressed by DZone contributors are their own.
Comments