spring和resteasy集成,三种主要的方式

对于和jboss as7的集成不需要做任何工作,jboss默认集成了resteasy,只需要对业务pojo做一些jax-rs的注解标注即可。这里讲的servlet容器是类似jetty、tomcat之类的

开始前,先做一些准备工作,引入jar包,我使用的是maven。

jar包版本,添加到属性文件里去:

<properties>

<jackson-version>2.2.3</jackson-version>
  <resteasy-version>3.0.4.Final</resteasy-version>
      <httpcomp-version>4.2.5</httpcomp-version>

</properties>

下面是依赖jar

<dependency>
<groupId>org.jboss.resteasy</groupId>
<artifactId>resteasy-jaxrs</artifactId>
<version>${resteasy-version}</version>
<exclusions>
<exclusion>
<groupId>org.slf4j</groupId>
<artifactId>slf4j-simple</artifactId>
</exclusion>
<exclusion>
<groupId>javassist</groupId>
<artifactId>javassist</artifactId>
</exclusion>
</exclusions>
</dependency>
<dependency>
<groupId>org.apache.httpcomponents</groupId>
<artifactId>httpclient</artifactId>
<version>${httpcomp-version}</version>
</dependency>
    <dependency>
<groupId>org.apache.httpcomponents</groupId>
<artifactId>httpcore</artifactId>
<version>${httpcomp-version}</version>
</dependency>
<dependency>
<groupId>org.apache.httpcomponents</groupId>
<artifactId>httpcore-nio</artifactId>
<version>${httpcomp-version}</version>
</dependency>

<dependency>
<groupId>org.jboss.resteasy</groupId>
<artifactId>resteasy-spring</artifactId>
<version>${resteasy-version}</version>
<exclusions>
<exclusion>
<groupId>org.jboss.resteasy</groupId>
<artifactId>resteasy-jettison-provider</artifactId>
</exclusion>
</exclusions>
</dependency>
    <dependency>
<groupId>org.jboss.resteasy</groupId>
<artifactId>resteasy-jackson2-provider</artifactId>
<version>${resteasy-version}</version>
</dependency>
    <dependency>
<groupId>org.jboss.resteasy</groupId>
<artifactId>resteasy-jaxb-provider</artifactId>
<version>${resteasy-version}</version>
</dependency>

1、如果servlet容器支持的servlet版本大于等于3.0,那么需要做如下工作:

1、1

首先添加这个依赖,这个是servlet 3支持的初始化器

<dependency>
        <groupId>org.jboss.resteasy</groupId>
        <artifactId>resteasy-servlet-initializer</artifactId>
        <version>${resteasy-version}</version>
    </dependency>

1、2

web.xml文件的配置,增加以下配置

<!-- resteasy启动初始化监听器 -->
<listener>
      <listener-class>org.jboss.resteasy.plugins.server.servlet.ResteasyBootstrap</listener-class> 
    </listener>
    <!-- resteasy和spring整合,有了这个,ContextLoaderListener就不要了  -->
<listener>
    <listener-class>org.jboss.resteasy.plugins.spring.SpringContextLoaderListener</listener-class>  
  </listener>

<!-- <listener> -->
<!-- <listener-class>org.springframework.web.context.ContextLoaderListener</listener-class> -->
<!-- </listener> -->

1、3

编写pojo

package com.vteba.service.rs.user;

import javax.inject.Inject;
import javax.inject.Named;
import javax.ws.rs.GET;
import javax.ws.rs.Path;
import javax.ws.rs.Produces;
import javax.ws.rs.core.MediaType; import com.vteba.user.model.User;
import com.vteba.user.service.spi.UserService;
import com.vteba.util.json.JacksonUtils; @Path("/restUser")
@Named
public class RestUserService {
@Inject
private UserService userServiceImpl; @GET
@Path("/list")
@Produces(value = {MediaType.APPLICATION_JSON})
public String list() {
User user = userServiceImpl.get(4L);
return JacksonUtils.get().toJson(user);
}
}

1、4

在spring bean 配置文件中添加

<!-- Import basic SpringMVC Resteasy integration -->
    <import resource="classpath:springmvc-resteasy.xml"/>

1、5

启动tomcat,访问 http://ip:port/你的项目路径/restUser/list

将返回json数据

2、第二种方式是。如果你的servlet容器支持的servlet小于3

那么和上面第一种方式有些区别。

去掉这个依赖

<dependency>
        <groupId>org.jboss.resteasy</groupId>
        <artifactId>resteasy-servlet-initializer</artifactId>
        <version>${resteasy-version}</version>
    </dependency>
在web.xml中增加

<!-- 要指定前缀否则和spring mvc的url-pattern冲突。还有一种解决办法就是将spring mvc和reseasy整合在一起 -->
<servlet>
      <servlet-name>resteasy</servlet-name>
      <servlet-class>org.jboss.resteasy.plugins.server.servlet.HttpServletDispatcher</servlet-class>
      <init-param>
            <param-name>resteasy.servlet.mapping.prefix</param-name>
            <param-value>/rs</param-value>
        </init-param>
    </servlet>
  
    <servlet-mapping>
      <servlet-name>resteasy</servlet-name>
      <url-pattern>/rs/*</url-pattern>
    </servlet-mapping>

这样就OK了,访问地址也要加上前缀

3、第三种就是将resteasy和spring mvc整合在一起,通过注解使他们共用spring mvc的控制器。

在spring bean配置文件中增加

<!-- Import basic SpringMVC Resteasy integration -->
    <import resource="classpath:springmvc-resteasy.xml"/>

web.xml中只需要保留你原来的spring mvc的配置。resteasy不需要做任何配置,

<servlet>
<servlet-name>Spring</servlet-name>
<servlet-class>org.springframework.web.servlet.DispatcherServlet;</servlet-class>
</servlet> <servlet-mapping>
<servlet-name>Spring</servlet-name>
<url-pattern>/*</url-pattern>
</servlet-mapping>

编写控制器,类似如下:

@Controller
@Path(ContactsResource.CONTACTS_URL)
public class ContactsResource
{
public static final String CONTACTS_URL = "/contacts";
@Autowired
ContactService service; @GET
@Produces({MediaType.APPLICATION_XML, MediaType.APPLICATION_JSON})
@Path("data")
public Contacts getAll()
{
return service.getAll();
} @PUT
@POST
@Produces({MediaType.APPLICATION_XML, MediaType.APPLICATION_JSON})
@Path("data")
public Response saveContact(@Context UriInfo uri, Contact contact)
throws URISyntaxException
{
service.save(contact);
URI newURI = UriBuilder.fromUri(uri.getPath()).path(contact.getLastName()).build();
return Response.created(newURI).build();
} @GET
@Produces({MediaType.APPLICATION_XML, MediaType.APPLICATION_JSON})
@Path("data/{lastName}")
public Contact get(@PathParam("lastName") String lastName)
{
return service.getContact(lastName);
} @POST
@PUT
@Consumes(MediaType.APPLICATION_FORM_URLENCODED)
@Produces(MediaType.TEXT_HTML)
public ModelAndView saveContactForm(@Form Contact contact)
throws URISyntaxException
{
service.save(contact);
return viewAll();
} @GET
@Produces(MediaType.TEXT_HTML)
public ModelAndView viewAll()
{
// forward to the "contacts" view, with a request attribute named
// "contacts" that has all of the existing contacts
return new ModelAndView("contacts", "contacts", service.getAll());
}
}

这样就可以将spring mvc和resteasy整合在一起了。

项目例子https://github.com/resteasy/Resteasy/tree/3.0.4.Final/jaxrs/examples/resteasy-springMVC

resteasy参考文档http://docs.jboss.org/resteasy/docs/2.2.1.GA/userguide/html/

有很丰富的内容可以自己学习

spring和resteasy 的集成方式的更多相关文章

  1. tomcat 7下spring 4.x mvc集成websocket以及sockjs完全参考指南

    之所以sockjs会存在,说得不好听点,就是因为微软是个流氓,现在使用windows 7的系统仍然有近半,而windows 7默认自带的是ie 8,有些会自动更新到ie 9,但是大部分非IT用户其实都 ...

  2. NoSql存储日志数据之Spring+Logback+Hbase深度集成

    NoSql存储日志数据之Spring+Logback+Hbase深度集成 关键词:nosql, spring logback, logback hbase appender 技术框架:spring-d ...

  3. 【ELK】4.spring boot 2.X集成ES spring-data-ES 进行CRUD操作 完整版+kibana管理ES的index操作

    spring boot 2.X集成ES 进行CRUD操作  完整版 内容包括: ============================================================ ...

  4. tomcat 7下spring 4.x mvc集成websocket以及sockjs完全参考指南(含nginx/https支持)

    之所以sockjs会存在,说得不好听点,就是因为微软是个流氓,现在使用windows 7的系统仍然有近半,而windows 7默认自带的是ie 8,有些会自动更新到ie 9,但是大部分非IT用户其实都 ...

  5. 6、Spring Boot 2.x 集成 MyBatis

    1.6 Spring Boot 2.x 集成 MyBatis 简介 详细介绍如何在Spring Boot中整合MyBatis,并通过注解方式实现映射. 完整源码: 1.6.1 创建 spring-bo ...

  6. spring boot rest 接口集成 spring security(2) - JWT配置

    Spring Boot 集成教程 Spring Boot 介绍 Spring Boot 开发环境搭建(Eclipse) Spring Boot Hello World (restful接口)例子 sp ...

  7. spring boot rest 接口集成 spring security(1) - 最简配置

    Spring Boot 集成教程 Spring Boot 介绍 Spring Boot 开发环境搭建(Eclipse) Spring Boot Hello World (restful接口)例子 sp ...

  8. Spring和MyBatis的集成

    Spring和MyBatis的整合   1. Spring和各个框架的整合 Spring目前是JavaWeb开发中最终的框架,提供一站式服务,可以其他各个框架整合集成 Spring整合方案 1.1.  ...

  9. 9、ssh的集成方式2

    1.在第一种的集成方式中,通过struts2-spring-plugin-2.1.8.1.jar这个插件让spring自动产生对应需要的action类,不需要在对应的spring.xml文件中进行配置 ...

随机推荐

  1. Python——字典dict()详解

    一.字典 字典是Python提供的一种数据类型,用于存放有映射关系的数据,字典相当于两组数据,其中一组是key,是关键数据(程序对字典的操作都是基于key),另一组数据是value,可以通过key来进 ...

  2. C#基于联通短信Sgip协议构建短信网关程序

    此软件基于中国联通Sgip协议程序接口,适合在中国联通申请了短信发送端口的公司使用.短信群发已经成为现在软件系统.网络营销等必不可少的应用工具.可应用在短信验证.信息群发.游戏虚拟商品购买.事件提醒. ...

  3. 【转】PCA for opencv

    对于PCA,一直都是有个概念,没有实际使用过,今天终于实际使用了一把,发现PCA还是挺神奇的. 在OPENCV中使用PCA非常简单,只要几条语句就可以了. 1.初始化数据 //每一行表示一个样本 Cv ...

  4. POJ-1426-Find the multiply

    这题深搜广搜都可以做,深搜的做法就是把每个由1 和 0 组成的数字拓展10倍以及拓展10倍+1,然后压入队列. 这样可以走过所有由10组成的数字,且两个方向平行发展(*10  +0和+1). bfs ...

  5. pandas关联mysql并读写数据库

    1.代码读写mysql,必须安装关联mysql的工具 操作如下命令: sudo apt-get install mysql-server mysql-clientsudo apt-get instal ...

  6. destoon 后台管理左侧新增菜单项

    destoon 后台菜单设置在对应模块的admin/menu.inc.php 例如要在后台会员管理里增加会员承包和股东管理 $menu = array( array('添加会员', '?modulei ...

  7. graph-SCC

    strongly connected component(SCC): 里面的任一对顶点都是互相可达的. 一个有向图,将每个SCC缩成一个点,那么这个图就变成了DAG(有向无环图). 原图进行DFS之后 ...

  8. nrf528xx bootloader 模块介绍(转载)

    转载https://www.cnblogs.com/rfnets/p/8205521.html 1. bootloader 的基本功能: 启动应用 几个应用之间切换 初始化外设 nordic nrf5 ...

  9. Eclipse设置C++自动补全变量名快捷键

    用快捷键:Alt+/ 要是还是有些场合不能提示,按照下列步骤 Window-Preferences-c/c++-Editor-Content Assist-Advanced 将未勾选的全部勾选

  10. re--读书笔记【转】

    原文链接 * 正则表达式入门 1.正则表达式的两种基本用途:搜索和替换. 2.正则表达式是一些用来匹配和处理文本的字符串. 小结:正则表达式是文本处理方面功能最强大的工具之一,正则表达式语言用来构造正 ...