Twitter

sâmbătă, 3 octombrie 2015

JAX-RS configure application examples

In this post, you can see how to configure a JAX-RS application

Let's suppose that we have an application named, JaxrsConfigureApplication_EE7, and the following two resources:
import javax.ws.rs.Produces;
import javax.ws.rs.GET;
import javax.ws.rs.Path;

@Path("helloworld")
public class HelloWorldResource {

 @GET
 @Produces("text/plain") // default: */*
 public String helloWorld() {
  return "Hello, World!";
 }
}

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

@Path("byeworld")
public class ByeWorldResource {

 @GET
 @Produces("text/plain") // default: */*
 public String byeWorld() {
  return "Bye, World!";
 }
}
Basically, by configuring a JAX-RS application means to set the base URI from which an application's resources respond to requests. This can be accomplished in two ways:

·         using the @ApplicationPath annotation in a subclass of javax.ws.rs.core.Application packaged within the WAR

For example, if your base URI will be /webresources (or, webresources) then simply add the following class:
import javax.ws.rs.ApplicationPath;
import javax.ws.rs.core.Application;

@ApplicationPath("webresources")
public class ApplicationConfig extends Application {
}
In the preceding example, the base URI is set to /webresources, which means that all resources defined within the application are relative to /webresources. Check out the below URLs:

// this URL will invoke HelloWorldResource#helloWorld()
http://localhost:8080/JaxrsConfigureApplication_EE7/webresources/helloworld

// this URL will invoke ByeWorldResource#byeWorld()
http://localhost:8080/JaxrsConfigureApplication_EE7/webresources/byeworld

So, by default, all the resources in an archive will be processed for resources. We can easily alter this behavior by overriding the getClasses() method to manually register the resource classes in the application with the JAX-RS runtime. For example, we can register the HelloWorldResource in HelloApplicationConfig, as below:
import java.util.HashSet;
import java.util.Set;
import javax.ws.rs.ApplicationPath;
import javax.ws.rs.core.Application;

@ApplicationPath("helloresources")
public class HelloApplicationConfig extends Application {

 @Override
 public Set<Class<?>> getClasses() {
  final Set<Class<?>> classes = new HashSet<>();
  classes.add(HelloWorldResource.class);
  // add more classes
  return classes;
 }
}
And, we can register the ByeWorldResource in ByeApplicationConfig, as below:
import java.util.HashSet;
import java.util.Set;
import javax.ws.rs.ApplicationPath;
import javax.ws.rs.core.Application;

@ApplicationPath("byeresources")
public class ByeApplicationConfig extends Application {

 @Override
 public Set<Class<?>> getClasses() {
  final Set<Class<?>> classes = new HashSet<>();
  classes.add(ByeWorldResource.class);
  return classes;
 }
}
Now, the URLs will become:

// this URL will invoke HelloWorldResource#helloWorld()
http://localhost:8080/JaxrsConfigureApplication_EE7/helloresources/helloworld

// this URL will invoke ByeWorldResource#byeWorld()
http://localhost:8080/JaxrsConfigureApplication_EE7/byeresources/byeworld

The following URLs will not work!

http://localhost:8080/JaxrsConfigureApplication_EE7/helloresources/byeworld
http://localhost:8080/JaxrsConfigureApplication_EE7/byeresources/helloworld

The complete application is available here.

·         using the servlet-mapping tag within the WAR's web.xml deployment descriptor

Let's suppose that we have an application named, JaxrsConfigureApplicationWebXml_EE7, and the same two resources, HelloWorldResource and ByeWorldResource.

This time we will configure the application via the <servlet-mapping> tag in the web.xml deployment descriptor, using the Application class name as the servlet:
<?xml version="1.0" encoding="UTF-8"?>
<web-app version="3.1" xmlns="http://xmlns.jcp.org/xml/ns/javaee" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://xmlns.jcp.org/xml/ns/javaee http://xmlns.jcp.org/xml/ns/javaee/web-app_3_1.xsd">

 <servlet-mapping>
  <servlet-name>javax.ws.rs.core.Application</servlet-name>
  <url-pattern>/webresources/*</url-pattern>
 </servlet-mapping>
</web-app>
Check out the following valid URLs:

// this URL will invoke HelloWorldResource#helloWorld()
http://localhost:8080/JaxrsConfigureApplicationWebXml_EE7/webresources/helloworld

// this URL will invoke ByeWorldResource#byeWorld()
http://localhost:8080/JaxrsConfigureApplicationWebXml_EE7/webresources/byeworld

This setting will also override the path set by @ApplicationPath when using an Application subclass.  For example let's suppose that we have the following Application subclass:
import javax.ws.rs.ApplicationPath;
import javax.ws.rs.core.Application;

@ApplicationPath("webresources")
public class ApplicationConfig extends Application {
}
And, in web.xml we have:
...
<servlet-mapping>
 <servlet-name>ee7.jaxrs.ApplicationConfig</servlet-name>
 <url-pattern>/greetings/*</url-pattern>      
</servlet-mapping>
...
Now, the following URLs will work:

// this URL will invoke HelloWorldResource#helloWorld()
http://localhost:8080/JaxrsConfigureApplicationWebXml_EE7/greetings/helloworld

// this URL will invoke ByeWorldResource#byeWorld()
http://localhost:8080/JaxrsConfigureApplicationWebXml_EE7/greetings/byeworld

While the following two will not work:

http://localhost:8080/JaxrsConfigureApplicationWebXml_EE7/webresources/helloworld
http://localhost:8080/JaxrsConfigureApplicationWebXml_EE7/webresources/byeworld

The complete application is available here.

Niciun comentariu:

Trimiteți un comentariu