Press "Enter" to skip to content

Creating REST web services with Apache Wink on Tomcat

Wink is the JAX-RS implementation from Apache. I have never used it in a real-life project, but I thought it would be a good idea to create a small, dummy example as I did with RESTEasy and Jersey before just to see how easy/hard it is to get started with it.

First of all, let’s import the dependencies. We need to list wink-server in our pom.xml.

<?xml version="1.0" encoding="UTF-8"?>  
<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>net.gazsi.laszlo.sandbox</groupId>
    <artifactId>tomcat-wink-example</artifactId>
    <version>1.0</version>
    <packaging>war</packaging>

    <dependencies>
        <dependency>
            <groupId>org.apache.wink</groupId>
            <artifactId>wink-server</artifactId>
            <version>1.4</version>
        </dependency>
    </dependencies>

    <build>
        <finalName>sandbox</finalName>
    </build>


</project>  

I am using the same RestExample class with the hello method that I have used in the previous examples with RESTEasy and Jersey.

package net.gazsi.laszlo.sandbox.ws;

import javax.ws.rs.GET;  
import javax.ws.rs.Path;  
import javax.ws.rs.PathParam;

@Path("hello")
public class RestExample {

    @GET
    @Path("{name}")
    public String sayHello(@PathParam("name") String name){
        StringBuilder stringBuilder = new StringBuilder("SandBox | Hello ");
        stringBuilder.append(name).append("!");

        return stringBuilder.toString();
    }

}

In the case of Apache Wink we need to extend the javax.ws.rs.core.Application class and override the getClasses method, which should return the set of classes that expose REST methods.

package net.gazsi.laszlo.sandbox.application;

import net.gazsi.laszlo.sandbox.ws.RestExample;

import javax.ws.rs.core.Application;  
import java.util.HashSet;  
import java.util.Set;

public class AppExample extends Application {

    @Override
    public Set<Class<?>> getClasses() {
        Set<Class<?>> classes = new HashSet<Class<?>>();
        classes.add(RestExample.class);

        return classes;
    }
}

The last step in setting up our REST endpoint with Apache Wink is to configure it in the web.xml

<?xml version="1.0" encoding="UTF-8"?>  
<web-app 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_3_0.xsd" version="3.0">
    <display-name>Laszlo Gazsi - Sandbox</display-name>

    <servlet>
        <servlet-name>rest-ws</servlet-name>
        <servlet-class>org.apache.wink.server.internal.servlet.RestServlet</servlet-class>
        <init-param>
            <param-name>javax.ws.rs.Application</param-name>
            <param-value>net.gazsi.laszlo.sandbox.application.AppExample</param-value>
        </init-param>
        <load-on-startup>1</load-on-startup>
    </servlet>
    <servlet-mapping>
        <servlet-name>rest-ws</servlet-name>
        <url-pattern>/rest/*</url-pattern>
    </servlet-mapping>

</web-app>  

Finally, after deploying the application in Tomcat, making a GET request to http://localhost:8080/sandbox/rest/hello/Laszlo should result in the SandBox | Hello Laszlo! message.

Be First to Comment

Leave a Reply

Your email address will not be published. Required fields are marked *