RESTEasy is JBOSS provided implementation of JAX-RS specification for building RESTful Web Services and RESTful Java applications. Though this is not limited to be used in JBOSS only, and you can use with other servers also. In this post, I am building such a hello world application in JBOSS AS7 server.

Environment used:

  1. RESTEasy 2.3.1.GA
  2. Jboss AS7
  3. JDK 1.6

Follow below steps to build a demo application.

1) Create a maven project and convert to eclipse web project

mvn archetype:generate -DgroupId=com.howtodoinjava -DartifactId=RESTfulDemoApplication
-DarchetypeArtifactId=maven-archetype-webapp -DinteractiveMode=false mvn eclipse:eclipse -Dwtpversion=2.0

2) Update runtime dependencies in pom.xml

You need to define only compile time dependencies. Even better, if you can identify and include the jars from jboss distribution package.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
  <modelVersion>4.0.0</modelVersion>
  <groupId>com.howtodoinjava</groupId>
  <artifactId>RESTfulDemoApplication</artifactId>
  <packaging>war</packaging>
  <version>1.0-SNAPSHOT</version>
  <name>RESTfulDemoApplication Maven Webapp</name>
    <repositories>
        <repository>
          <id>jboss</id>
          <url>http://repository.jboss.org/maven2</url>
        </repository>
    </repositories>
  <dependencies>
    <dependency>
      <groupId>junit</groupId>
      <artifactId>junit</artifactId>
      <version>3.8.1</version>
      <scope>test</scope>
    </dependency>
    <!-- core library -->
    <dependency>
        <groupId>org.jboss.resteasy</groupId>
         <artifactId>resteasy-jaxrs</artifactId>
        <version>2.3.1.GA</version>
        <scope>compile</scope>
    </dependency>
   <!-- JAXB support -->
   <dependency>
      <groupId>org.jboss.resteasy</groupId>
        <artifactId>resteasy-jaxb-provider</artifactId>
      <version>2.3.1.GA</version>
      <scope>compile</scope>
   </dependency>
   <!-- multipart/form-data and multipart/mixed support -->
   <dependency>
      <groupId>org.jboss.resteasy</groupId>
        <artifactId>resteasy-multipart-provider</artifactId>
      <version>2.3.1.GA</version>
      <scope>compile</scope>
   </dependency>
   <dependency>
        <groupId>net.sf.scannotation</groupId>
        <artifactId>scannotation</artifactId>
        <version>1.0.2</version>
        <scope>compile</scope>
    </dependency>
  </dependencies>
  <build>
    <finalName>RESTfulDemoApplication</finalName>
  </build>
</project>

3) Create a blank web.xml file

JBOSS’s inbuilt support RESTeasy makes it a perfect combo for RESTFul web application. The minimum configuration to build such application is none. Yes, a blank web.xml file.

1
2
3
4
5
6
7
<!DOCTYPE web-app PUBLIC
 "-//Sun Microsystems, Inc.//DTD Web Application 2.3//EN"
 
<web-app>
  <display-name>Restful Web Application</display-name>
</web-app>

5) Register the application path

You will need to extend javax.ws.rs.core.Application class and provide @ApplicationPath annotation.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
<img src="http://howtodoinjava.com/wp-content/uploads/jboss+resteasy1.png" alt="JBOSS 7+ RESTEasy demo application" width="453" height="225" class="size-full wp-image-2033" /> JBOSS 7+ RESTEasy demo applicationpackage com.howtodoinjava;
 
import java.util.Arrays;
import java.util.HashSet;
import java.util.Set;
 
import javax.ws.rs.ApplicationPath;
import javax.ws.rs.core.Application;
 
import com.howtodoinjava.service.UserService;
 
@ApplicationPath("/")
public class ApplicationConfig extends Application {
    @SuppressWarnings("unchecked")
    public Set<Class<?>> getClasses() {
        return new HashSet<Class<?>>(Arrays.asList(UserService.class));
    }
}

4) Write a service class having @Path annotations

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
package com.howtodoinjava.service;
 
import java.net.URI;
import java.net.URISyntaxException;
import java.util.ArrayList;
 
import javax.ws.rs.Consumes;
import javax.ws.rs.DELETE;
import javax.ws.rs.DefaultValue;
import javax.ws.rs.GET;
import javax.ws.rs.POST;
import javax.ws.rs.PUT;
import javax.ws.rs.Path;
import javax.ws.rs.PathParam;
import javax.ws.rs.Produces;
import javax.ws.rs.QueryParam;
import javax.ws.rs.core.Response;
import javax.xml.bind.annotation.XmlAccessType;
import javax.xml.bind.annotation.XmlAccessorType;
import javax.xml.bind.annotation.XmlElement;
import javax.xml.bind.annotation.XmlRootElement;
 
import com.howtodoinjava.model.User;
import com.howtodoinjava.model.Users;
 
@XmlAccessorType(XmlAccessType.NONE)
@XmlRootElement(name = "user-management")
@Path("/user-management")
public class UserService {
    @XmlElement(name = "users")
    private String uri1 = "/user-management/users";
 
    @XmlElement(name = "report")
    private String uri2 = "/user-managemet/generate-report";
 
    public String getUri1() {
        return uri1;
    }
 
    public void setUri1(String uri1) {
        this.uri1 = uri1;
    }
 
    public String getUri2() {
        return uri2;
    }
 
    public void setUri2(String uri2) {
        this.uri2 = uri2;
    }
 
    @GET
    @Path("/")
    @Produces("application/vnd.com.demo.user-management+xml;charset=UTF-8;version=1")
    public UserService getServiceInfo() {
        return new UserService();
    }
 
    @GET
    @Path("/users")
    @Produces("application/vnd.com.demo.user-management.users+xml;charset=UTF-8;version=1")
    public Users getAllUsers() {
        User user1 = new User();
        user1.setId(1);
        user1.setFirstName("demo");
        user1.setLastName("user");
        user1.setUri("/user-management/users/1");
 
        User user2 = new User();
        user2.setId(2);
        user2.setFirstName("demo");
        user2.setLastName("user");
        user2.setUri("/user-management/users/2");
 
        Users users = new Users();
        users.setUsers(new ArrayList<User>());
        users.getUsers().add(user1);
        users.getUsers().add(user2);
 
        return users;
    }
 
    @GET
    @Path("/users/{id}")
    @Produces("application/vnd.com.demo.user-management.user+xml;charset=UTF-8;version=1")
    public User getUserById(@PathParam("id"int id) {
        User user = new User();
        user.setId(id);
        user.setFirstName("demo");
        user.setLastName("user");
        user.setUri("/user-management/users/" + id);
        return user;
    }
 
    @POST
    @Path("/users")
    @Consumes("application/vnd.com.demo.user-management.user+xml;charset=UTF-8;version=1")
    public Response createUser(User user,
            @DefaultValue("false"@QueryParam("allow-admin"boolean allowAdmin)
            throws URISyntaxException {
        System.out.println(user.getFirstName());
        System.out.println(user.getLastName());
        return Response.status(201)
                .contentLocation(new URI("/user-management/users/123")).build();
    }
 
    @PUT
    // @Path("/users/{id: [0-9]*}")
    @Path("/users/{id}")
    @Consumes("application/vnd.com.demo.user-management.user+xml;charset=UTF-8;version=1")
    @Produces("application/vnd.com.demo.user-management.user+xml;charset=UTF-8;version=1")
    public User updateUser(@PathParam("id"int id, User user)
            throws URISyntaxException {
        user.setId(id);
        user.setFirstName(user.getFirstName() + "updated");
        return user;
    }
 
    @DELETE
    @Path("/users/{id}")
    public Response deleteUser(@PathParam("id"int id)
            throws URISyntaxException {
        return Response.status(200).build();
    }
}

5) Run the application

When we deploy above built application in jboss and hit the URL: ” http://localhost:8080/RESTfulDemoApplication/user-management/users”, below is the response.

reference from:http://howtodoinjava.com/2013/05/12/resteasy-jboss-7-hello-world-application/

RESTEasy + JBOSS 7 Hello world application---reference的更多相关文章

  1. Hibernate Validator 6.0.9.Final - JSR 380 Reference Implementation: Reference Guide

    Preface Validating data is a common task that occurs throughout all application layers, from the pre ...

  2. 转载---jboss简单使用

    初学Jboss,对于Jboss的基础认识以及配置做一些记录 Jboss基础: JBoss是什么–基于J2EE的应用服务器–开放源代码–JBoss核心服务不包括支持servlet/JSP的WEB容器,一 ...

  3. Jboss基础及简单的应用

    初学Jboss,对于Jboss的基础认识以及配置做一些记录 Jboss基础: JBoss是什么–基于J2EE的应用服务器–开放源代码–JBoss核心服务不包括支持servlet/JSP的WEB容器,一 ...

  4. jboss相关的术语

    1 jboss eap java ee application server.red hat官方版本. 2 jboss as/wildfly java ee application server的社区 ...

  5. zt 比较各JAX-RS实现:CXF,Jersey,RESTEasy,Restlet

    http://news.misuland.com/20080926/1222396399411.html JavaSE/EE执行委员批准了JSR 311 JAX-RS作为支持RESTful web服务 ...

  6. 分布式服务治理框架Dubbo的前世今生及应用实战

    Dubbo的出现背景 Dubbo从开源到现在,已经出现了接近10年时间,在国内各大企业被广泛应用. 它到底有什么魔力值得大家去追捧呢?本篇文章给大家做一个详细的说明. 大规模服务化对于服务治理的要求 ...

  7. RESTful和JAX-RS

    一.简介 Java Web有很多成熟的框架,主要可以分为两类Web Application和Web Services.用于Web Application的框架包括官方的Servlet/JSP, JST ...

  8. Apache CXF 102 CXF with REST

    前言 续上篇Apache CXF 101,摘抄部分REST概念性知识,以运行实例考察CXF对REST的支持. 目录 1 REST简介 2 工具 3 运行实例 内容 本Spike记录中内容,如无特别指出 ...

  9. WebService基础学习(二)—三要素

    一.Java中WebService规范      JAVA 中共有三种WebService 规范,分别是JAX-WS.JAX-RS.JAXM&SAAJ(废弃).   1.JAX-WS规范    ...

随机推荐

  1. 基于jQuery查找dom的多种方式性能问题

    这个问题的产生由于我们前端组每个人的编码习惯的差异,最主要的还是因为代码的维护性问题.在此基础上,我对jQuery源码(1.11.3)查找dom节点相关的内容进行了仔细的查阅,虽然并不能理解的很深入 ...

  2. iframe中在父窗口打开子页面

    我们在做页面框架的时候,通常会采用一个iframe来显示子页面,但有这么种情况,就是session失效时,登录页面就会显示在iframe中,这不符合常理,一般应该显示在顶部才对. 下面的js代码可以解 ...

  3. 用VS2005写一个 C 的类库和用 C#来调用的示例

    一.用VS2005写一个 C 的类库的步骤: (1).建立一个空的Visual C++项目 (2).这时候在项目中可以看见 三个空目录 选中 "源文件" 目录,然后点鼠标右键,在弹 ...

  4. springMVC整合jedis+redis,以注解形式使用

    前两天写过 springMVC+memcached 的整合,我从这个基础上改造一下,把redis和springmvc整合到一起. 和memcached一样,redis也有java专用的客户端,官网推荐 ...

  5. dedecms 文章内容文章名字和文章网址的调用

    文章标题: <a href="{dede:field name='arcurl'/}">{dede:field.title/}</a> 本文章网址: < ...

  6. Js设置所有连接是触发/swt/的代码

    Js设置所有连接是触发/swt/的代码 代码为: <script> var doca=document.getElementsByTagName('a'); for(var i=0;i&l ...

  7. phpexcel 一些基本的设置 (表格的基本属性)

    网址是:http://www.thinkphp.cn/code/1893.html

  8. 接受POST表单传过来的信息 可以用foreach循环进行遍历操作

    if(isset($_POST['Goods'])){                     foreach($_POST['Goods'] as $_k =>$_v){            ...

  9. CPU与外设传送数据方式

    7.2 CPU与外设之间数据传送的方式 在微型计算机系统中,CPU与外设之间的数据传送方式主要有程序传送方式.中断传送方式和直接存储器存取(DMA)传送方式,分别介绍如下.     7.2.1 程序传 ...

  10. div+css知识点

    前端书写规范: 1.所有书写均在英文半角状态下的小写: 2.id,class必须以字母开头: 3.所有标签必须闭合: 4.html标签用tab键缩进: 5.属性值必须带引号: 6.<!-- ht ...