RESTEasy, JBoss project, implementation of the JAX-RS specification. In this tutorial, we show you how to use RESTEasy framework to create a simple REST style web application.

Technologies and Tools used in this article:

  1. RESTEasy 2.2.1.GA
  2. JDK 1.6
  3. Maven 3.0.3
  4. Eclipse 3.6
What’s REST?
Read this, this and this to understand what’s REST.

1. Directory Structure

Review final directory structure of this tutorial. Just a standard web project structure.

RESTEasy hello world example_第1张图片
2. Standard Web Project

Create a standard Maven web project structure.
 

   
   
   
   
  1. mvn archetype:create -DgroupId=com.mkyong.common -DartifactId=RESTfulExample   
  2.     -DarchetypeArtifactId=maven-archetype-webapp -DinteractiveMode=false 
Convert to Eclipse web project.
 
   
   
   
   
  1. mvn eclipse:eclipse -Dwtpversion=3.2.5 
3. Project Dependencies

Declares JBoss public Maven repository and “resteasy-jaxrs” in your Maven pom.xml file. That’s all you need to use RESTEasy.

File : pom.xml

   
   
   
   
  1. <project ..."> 
  2.    
  3.     <repositories> 
  4.        <repository> 
  5.           <id>JBoss repositoryid> 
  6.           <url>https://repository.jboss.org/nexus/content/groups/public-jboss/url> 
  7.        repository> 
  8.     repositories> 
  9.    
  10.     <dependencies> 
  11.    
  12.         <dependency> 
  13.             <groupId>org.jboss.resteasygroupId> 
  14.             <artifactId>resteasy-jaxrsartifactId> 
  15.             <version>2.2.1.GAversion> 
  16.         dependency> 
  17.    
  18.     dependencies> 
  19. project> 

4. REST Service

A simple REST service. See demo at the end of the article, it should be self-explanatory.
 

   
   
   
   
  1. package com.mkyong.rest;  
  2.    
  3. import javax.ws.rs.GET;  
  4. import javax.ws.rs.Path;  
  5. import javax.ws.rs.PathParam;  
  6. import javax.ws.rs.core.Response;  
  7.    
  8. @Path("/message")  
  9. public class MessageRestService {  
  10.    
  11.     @GET 
  12.     @Path("/{param}")  
  13.     public Response printMessage(@PathParam("param") String msg) {  
  14.    
  15.         String result = "Restful example : " + msg;  
  16.    
  17.         return Response.status(200).entity(result).build();  
  18.    
  19.     }  
  20.    

5. web.xml

Now, configure listener and servlet to support RESTEasy. Read this JBoss documentation for detail explanation.

File : web.xml

   
   
   
   
  1. <web-app id="WebApp_ID" version="2.4" 
  2.     xmlns="http://java.sun.com/xml/ns/j2ee"   
  3.     xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" 
  4.     xsi:schemaLocation="http://java.sun.com/xml/ns/j2ee   
  5.     http://java.sun.com/xml/ns/j2ee/web-app_2_4.xsd"> 
  6.     <display-name>Restful Web Applicationdisplay-name> 
  7.    
  8.      
  9.     <context-param> 
  10.         <param-name>resteasy.scanparam-name> 
  11.         <param-value>trueparam-value> 
  12.     context-param> 
  13.    
  14.      
  15.     <context-param> 
  16.         <param-name>resteasy.servlet.mapping.prefixparam-name> 
  17.         <param-value>/restparam-value> 
  18.     context-param> 
  19.    
  20.     <listener> 
  21.         <listener-class> 
  22.             org.jboss.resteasy.plugins.server.servlet.ResteasyBootstrap  
  23.         listener-class> 
  24.     listener> 
  25.    
  26.     <servlet> 
  27.         <servlet-name>resteasy-servletservlet-name> 
  28.         <servlet-class> 
  29.             org.jboss.resteasy.plugins.server.servlet.HttpServletDispatcher  
  30.         servlet-class> 
  31.     servlet> 
  32.    
  33.     <servlet-mapping> 
  34.         <servlet-name>resteasy-servletservlet-name> 
  35.         <url-pattern>/rest/*url-pattern> 
  36.     servlet-mapping> 
  37.    
  38. web-app> 

 Note
You need to set the “resteasy.servlet.mapping.prefix” if your servlet-mapping for the resteasy servlet has a url-pattern other than “/*“.

 

In above example, the resteasy servlet url-pattern is “/rest/*“, so you have to set the “resteasy.servlet.mapping.prefix” to “/rest” as well, otherwise, you will hit resource not found error message


 

 Note
Remember to set “resteasy.scan” to true, so that RESTEasy will find and register your REST service automatically.

 

6. Demo

In this example, web request from “projectURL/rest/message/” will match to “MessageRestService“, and “projectURL/rest/message/{any values}” will match to @PathParam parameter.

Test 1 : http://localhost:8080/RESTfulExample/rest/message/mkyong

RESTEasy hello world example_第2张图片

Test 2 : http://localhost:8080/RESTfulExample/rest/message/hello%20world

RESTEasy hello world example_第3张图片

Alternative REST Service Registration

In above example, you are register REST service via “ResteasyBootstrap” listener. Here i show you another way.

Create a class and extends javax.ws.rs.core.Application, and add your REST service manually.

   
   
   
   
  1. package com.mkyong.app;  
  2.    
  3. import java.util.HashSet;  
  4. import java.util.Set;  
  5. import javax.ws.rs.core.Application;  
  6. import com.mkyong.rest.MessageRestService;  
  7.    
  8. public class MessageApplication extends Application {  
  9.     private Set singletons = new HashSet();  
  10.    
  11.     public MessageApplication() {  
  12.         singletons.add(new MessageRestService());  
  13.     }  
  14.    
  15.     @Override 
  16.     public Set getSingletons() {  
  17.         return singletons;  
  18.     }  
  19. File : web.xml , no more listener, configure your application class like below :
     

       
       
       
       
    1. <web-app id="WebApp_ID" version="2.4" 
    2.     xmlns="http://java.sun.com/xml/ns/j2ee"   
    3.         xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" 
    4.     xsi:schemaLocation="http://java.sun.com/xml/ns/j2ee   
    5.     http://java.sun.com/xml/ns/j2ee/web-app_2_4.xsd"> 
    6.     <display-name>Restful Web Applicationdisplay-name> 
    7.    
    8.     <context-param> 
    9.         <param-name>resteasy.servlet.mapping.prefixparam-name> 
    10.         <param-value>/restparam-value> 
    11.     context-param> 
    12.    
    13.     <servlet> 
    14.         <servlet-name>resteasy-servletservlet-name> 
    15.         <servlet-class> 
    16.             org.jboss.resteasy.plugins.server.servlet.HttpServletDispatcher  
    17.                 servlet-class> 
    18.         <init-param> 
    19.             <param-name>javax.ws.rs.Applicationparam-name> 
    20.             <param-value>com.mkyong.app.MessageApplicationparam-value> 
    21.         init-param> 
    22.     servlet> 
    23.    
    24.     <servlet-mapping> 
    25.         <servlet-name>resteasy-servletservlet-name> 
    26.         <url-pattern>/rest/*url-pattern> 
    27.     servlet-mapping> 
    28.    
    29. web-app> 

    Done.

    Download Source Code

    Download it – RESTEasy-Hello-World-Example.zip (7 KB)

    注:转自http://www.mkyong.com/webservices/jax-rs/resteasy-hello-world-example/

    你可能感兴趣的:(RESTEasy,example,Java开发)