Home RESTEasy example without using a web.xml
Post
Cancel

RESTEasy example without using a web.xml

In this tutorial we will implement a simple web service with RESTEasy implementation -it’s an implementation of the JAX-RS specification by JBoss- without using a web.xml file. I’ve used

  • Eclipse 4.3.0
  • JBoss AS 7.1.1.Final
  • Maven 3.1.1
  • RESTEasy 3.0.6.Final

tools and technologies. The purpose of not using web.xml file is the fact that we don’t have to deal with servlet mappings within a xml file. So, this feature comes with any Servlet 3.x complaint container.

1. Creating a Maven Project

First thing to do is creating a maven project, so enter the following code into command line;

1
mvn archetype:generate -DgroupId=buraktas.com -DartifactId=rest-helloworld

command line will prompt some questions while it is initializing your project like; choosing an archetype version of maven. If you continue to press Enter it will automatically set to default options.

<packaging>jar</packaging> is set on default in pom.xml, however, we will use war as packaging, so please change it to war.

After we created our maven project we have to import initialized project into eclipse by import->Existing Maven Projects

import_maven

Then browse to the directory where you created your maven project, and select the root folder.

Now our directory structure should look like this

directory

2. Dependencies

We are going to use 2 dependencies which junit is optional cause I’m not going to show any test methods in this tutorial

<dependencies>
	<dependency>
		<groupId>junit</groupId>
		<artifactId>junit</artifactId>
		<version>3.8.1</version>
		<scope>test</scope>
	</dependency>

	<dependency>
		<groupId>org.jboss.resteasy</groupId>
		<artifactId>resteasy-jaxrs</artifactId>
		<version>3.0.6.Final</version>
		<scope>provided</scope>
	</dependency>
</dependencies>
  • resteasy-jaxrs is defined with <scope>provided</scope> so that JBoss will provide this dependency in runtime. Also here you can find more information about maven dependency scopes
  • junit is for testing and it comes with default maven project.

Also we have to add 2 maven plugins ;

<build>
	<plugins>
		<plugin>
			<groupId>org.apache.maven.plugins</groupId>
			<artifactId>maven-compiler-plugin</artifactId>
			<configuration>
				<source>1.7</source>
				<target>1.7</target>
			</configuration>
		</plugin>

		<plugin>
			<groupId>org.apache.maven.plugins</groupId>
			<artifactId>maven-war-plugin</artifactId>
			<version>2.3</version>
			<configuration>
				<failOnMissingWebXml>false</failOnMissingWebXml>
				<warName>rest-helloworld</warName>
			</configuration>
		</plugin>
	</plugins>
</build>
  • maven-compiler-plugin is for compiling your Java sources.
  • maven-war-plugin is responsible for handling with dependencies and packaging them into a web application archive.
    • false is the key point to let maven build our web application without using web.xml,otherwise mavens give an error, and it will look like;
    • Failed to execute goal org.apache.maven.plugins:maven-war-plugin:2.3:war (default-war) on project rest-helloworld: Error assembling WAR: webxml attribute is required (or pre-existing WEB-INF/web.xml if executing in update mode)
    • <warName>rest-helloworld</warName> element will define root URI of our web application. For instance, our every resource will located on localhost:8080/rest-helloworld.

3. Developing a REST Service

We have to implement a base/root application which defines the base URI for all other sub-resources. Then, this root class has to extended from Application, with @ApplicationPath(“”) annotation.

package com.buraktas.application;

import java.util.Collections;
import java.util.Set;
import javax.ws.rs.ApplicationPath;
import javax.ws.rs.core.Application;

@ApplicationPath("")
public class BaseApplication extends Application {

    @Override
    public Set<Class<?>> getClasses() {
        return Collections.emptySet();
    }
}
  • @ApplicationPath(“”) annotation is mandatory, however, path URI is optional. It defines the root URI for all other resources. For instance, if I specify it as root-service, my other resources will be reachable from http://mydomain.com/rest-helloworld/root-service. Otherwise, you will face with an error which says;
  • JBAS011203: No Servlet mappings found for JAX-RS application: com.buraktas.application.BaseApplication either annotate it with @ApplicationPath or add a servlet-mapping in web.xml
    
  • Overriding getClasses method is optional, if you are not going to map classes manually.

Finally, we can implement our simplest and first REST service now which is only composed by a GET method and returns a Hello World string.


package com.buraktas.helloworld;

import javax.ws.rs.GET;
import javax.ws.rs.Path;
import javax.ws.rs.core.Response;

@Path("")
public class HelloWorld {

    @GET
    @Path("/helloworld")
    public Response getHelloWorld() {
        String value = "Hello World";
        return Response.status(200).entity(value).build();
    }
}

Now our web-service is ready for getting build by maven with;

1
mvn clean install

After the process is successfully finished, maven will create .war file under target directory. When we deploy it on JBoss, we could use our implemented resource on http://localhost:8080/rest-helloworld/helloworld which will print Hello World

localhost_helloword

Here you can find the original source code.
RESTEasy Hello World

This post is licensed under CC BY 4.0 by the author.

mongoexport query with using date

Jersey Client Example with JSON Support

Comments powered by Disqus.