In this lab, you will create a simple RESTful service using JAX-RS.
Create a new Dynamic Web project named Lab12Web.
Create a new class named HelloREST in the services package.
Complete the class so it can act as a source of RESTful content:
Write an annotation on the class that assigns a RESTful URL:
@Path("/hellorest")
Write a simple method that returns content:
public String hello(String name)
{
return "Hello, " + name;
}
Annotate the method so that it responds to an HTTP GET and produces a text HTML result:
@GET
@Produces("text/html")
Annotate the name parameter so it's accessible to the service as an HTTP GET parameter:
@QueryParam("name")
Import types from the javax.ws.rs package.
Create another class in the services package named HelloApplication that extends javax.ws.rs.core.Application.
Complete the class:
Annotate the class to provide a prefix for the RESTful URL:
@ApplicationPath("/rest/")
Use the Source - Override/Implement Methods menu to override the getClasses method.
You can delete the generated call to the superclass.
Add code to the method so that it creates a Set containing all of your RESTful sources (only one in this case):
Set<Class<?>> s = new HashSet<Class<?>>();
s.add(HelloREST.class);
return s;
Deploy the application by adding it to the TomEE server.
Test the RESTful service by opening a Web browser and entering this URL:
http://localhost:8080/Lab12Web/rest/hellorest?name=xxxx
Substitute your name for the xxxx. You should see a simple Web page saying "Hello" to your name.