resteasy_RESTEasy教程第2部分:Spring集成

resteasy

RESTEasy提供了对Spring集成的支持,这使我们能够将Spring bean作为RESTful WebServices公开。

步骤#1:使用Maven配置RESTEasy + Spring依赖项。
<project xmlns='http:maven.apache.orgPOM4.0.0' xmlns:xsi='http:www.w3.org2001XMLSchema-instance'xsi:schemaLocation='http:maven.apache.orgPOM4.0.0 http:maven.apache.orgmaven-v4_0_0.xsd'><modelVersion>4.0.0<modelVersion><groupId>com.sivalabs<groupId><artifactId>resteasy-demo<artifactId><version>0.1<version><packaging>war<packaging><properties><project.build.sourceEncoding>UTF-8<project.build.sourceEncoding><org.springframework.version>3.1.0.RELEASE<org.springframework.version><slf4j.version>1.6.1<slf4j.version><java.version>1.6<java.version><junit.version>4.8.2<junit.version><resteasy.version>2.3.2.Final<resteasy.version><properties><build><finalName>resteasy-demo<finalName><build><dependencies><dependency><groupId>junit<groupId><artifactId>junit<artifactId><scope>test<scope><dependency><dependency><groupId>org.slf4j<groupId><artifactId>slf4j-api<artifactId><version>${slf4j.version}<version><dependency><dependency><groupId>org.slf4j<groupId><artifactId>jcl-over-slf4j<artifactId><version>${slf4j.version}<version><scope>runtime<scope><dependency><dependency><groupId>org.slf4j<groupId><artifactId>slf4j-log4j12<artifactId><version>${slf4j.version}<version><scope>runtime<scope><dependency><dependency><groupId>log4j<groupId><artifactId>log4j<artifactId><version>1.2.16<version><scope>runtime<scope><dependency><dependency><groupId>org.springframework<groupId><artifactId>spring-context-support<artifactId><version>${org.springframework.version}<version><exclusions><exclusion><artifactId>commons-logging<artifactId><groupId>commons-logging<groupId><exclusion><exclusions><dependency><dependency><groupId>org.springframework<groupId><artifactId>spring-web<artifactId><version>${org.springframework.version}<version><dependency><dependency><groupId>org.jboss.resteasy<groupId><artifactId>resteasy-jaxrs<artifactId><version>${resteasy.version}<version><scope>provided<scope><dependency><dependency><groupId>org.jboss.resteasy<groupId><artifactId>resteasy-jaxb-provider<artifactId><version>${resteasy.version}<version><scope>provided<scope><dependency><dependency><groupId>org.jboss.resteasy<groupId><artifactId>jaxrs-api<artifactId><version>2.3.0.GA<version><scope>provided<scope><dependency><dependency><groupId>org.jboss.resteasy<groupId><artifactId>resteasy-spring<artifactId><version>2.3.0.GA<version><exclusions><exclusion><artifactId>commons-logging<artifactId><groupId>commons-logging<groupId><exclusion><exclusion><artifactId>org.jboss.resteasy<artifactId><groupId>resteasy-jaxb-provider<groupId><exclusion><exclusion><artifactId>jaxb-impl<artifactId><groupId>com.sun.xml.bind<groupId><exclusion><exclusion><artifactId>sjsxp<artifactId><groupId>com.sun.xml.stream<groupId><exclusion><exclusion><artifactId>jsr250-api<artifactId><groupId>javax.annotation<groupId><exclusion><exclusion><artifactId>resteasy-jaxb-provider<artifactId><groupId>org.jboss.resteasy<groupId><exclusion><exclusion><artifactId>activation<artifactId><groupId>javax.activation<groupId><exclusion><exclusions><dependency><dependency><groupId>javax.servlet<groupId><artifactId>servlet-api<artifactId><version>2.5<version><scope>provided<scope><dependency><dependency><groupId>org.apache.httpcomponents<groupId><artifactId>httpclient<artifactId><version>4.1.2<version><dependency><dependencies><project>

步骤#2:在web.xml中配置RESTEasy + Spring

<web-app xmlns:xsi='http:www.w3.org2001XMLSchema-instance' xmlns='http:java.sun.comxmlnsjavaee' xmlns:web='http:java.sun.comxmlnsjavaeeweb-app_2_5.xsd' xsi:schemaLocation='http:java.sun.comxmlnsjavaee http:java.sun.comxmlnsjavaeeweb-app_3_0.xsd' id='WebApp_ID' version='3.0'><listener><listener-class>org.jboss.resteasy.plugins.server.servlet.ResteasyBootstrap<listener-class><listener><listener><listener-class>org.jboss.resteasy.plugins.spring.SpringContextLoaderListener<listener-class><listener><servlet><servlet-name>Resteasy<servlet-name><servlet-class>org.jboss.resteasy.plugins.server.servlet.HttpServletDispatcher<servlet-class><servlet><servlet-mapping><servlet-name>Resteasy<servlet-name><url-pattern>rest*<url-pattern><servlet-mapping><context-param><param-name>contextConfigLocation<param-name><param-value>classpath:applicationContext.xml<param-value><context-param><context-param><param-name>resteasy.servlet.mapping.prefix<param-name><param-value>rest<param-value><context-param><context-param><param-name>resteasy.scan<param-name><param-value>true<param-value><context-param><web-app>

步骤#3:创建Spring Service类UserService并更新UserResource以使用UserService bean。

package com.sivalabs.resteasydemo;import java.util.List;import org.springframework.stereotype.Service;import com.sivalabs.resteasydemo.MockUserTable;@Servicepublic class UserService {public void save(User user){MockUserTable.save(user);}public User getById(Integer id){return MockUserTable.getById(id);}public List<User> getAll(){return MockUserTable.getAll();}public void delete(Integer id){MockUserTable.delete(id);}}package com.sivalabs.resteasydemo;import java.util.List;import javax.ws.rs.Consumes;import javax.ws.rs.DELETE;import javax.ws.rs.GET;import javax.ws.rs.POST;import javax.ws.rs.Path;import javax.ws.rs.PathParam;import javax.ws.rs.Produces;import javax.ws.rs.core.GenericEntity;import javax.ws.rs.core.MediaType;import javax.ws.rs.core.Response;import org.springframework.beans.factory.annotation.Autowired;import org.springframework.stereotype.Component;@Component@Path('users')@Produces(MediaType.APPLICATION_XML)@Consumes(MediaType.APPLICATION_XML)public class UserResource {@Autowiredprivate UserService userService;@Path('')@GETpublic Response getUsersXML() {List<User> users = userService.getAll();GenericEntity<List<User>> ge = new GenericEntity<List<User>>(users){};return Response.ok(ge).build();}@Path('{id}')@GETpublic Response getUserXMLById(@PathParam('id') Integer id) {User user = userService.getById(id);return Response.ok(user).build();}@Path('')@POSTpublic Response saveUser(User user) {userService.save(user);return Response.ok('<status>success<status>').build();}@Path('{id}')@DELETEpublic Response deleteUser(@PathParam('id') Integer id) {userService.delete(id);return Response.ok('<status>success<status>').build();}}package com.sivalabs.resteasydemo;import java.util.List;import org.springframework.stereotype.Service;import com.sivalabs.resteasydemo.MockUserTable;@Servicepublic class UserService {public void save(User user){MockUserTable.save(user);}public User getById(Integer id){return MockUserTable.getById(id);}public List<User> getAll(){return MockUserTable.getAll();}public void delete(Integer id){MockUserTable.delete(id);}}package com.sivalabs.resteasydemo;import java.util.List;import javax.ws.rs.Consumes;import javax.ws.rs.DELETE;import javax.ws.rs.GET;import javax.ws.rs.POST;import javax.ws.rs.Path;import javax.ws.rs.PathParam;import javax.ws.rs.Produces;import javax.ws.rs.core.GenericEntity;import javax.ws.rs.core.MediaType;import javax.ws.rs.core.Response;import org.springframework.beans.factory.annotation.Autowired;import org.springframework.stereotype.Component;@Component@Path('users')@Produces(MediaType.APPLICATION_XML)@Consumes(MediaType.APPLICATION_XML)public class UserResource {@Autowiredprivate UserService userService;@Path('')@GETpublic Response getUsersXML() {List<User> users = userService.getAll();GenericEntity<List<User>> ge = new GenericEntity<List<User>>(users){};return Response.ok(ge).build();}@Path('{id}')@GETpublic Response getUserXMLById(@PathParam('id') Integer id) {User user = userService.getById(id);return Response.ok(user).build();}@Path('')@POSTpublic Response saveUser(User user) {userService.save(user);return Response.ok('<status>success<status>').build();}@Path('{id}')@DELETEpublic Response deleteUser(@PathParam('id') Integer id) {userService.delete(id);return Response.ok('<status>success<status>').build();}}applicationContext.xml<?xml version='1.0' encoding='UTF-8'?><beans xmlns='http:www.springframework.orgschemabeans'xmlns:xsi='http:www.w3.org2001XMLSchema-instance'xmlns:p='http:www.springframework.orgschemap'xmlns:context='http:www.springframework.orgschemacontext'xsi:schemaLocation='http:www.springframework.orgschemabeans http:www.springframework.orgschemabeansspring-beans.xsdhttp:www.springframework.orgschemacontext http:www.springframework.orgschemacontextspring-context.xsd'><context:annotation-config><context:component-scan base-package='com.sivalabs.resteasydemo'><beans>

步骤#4:使用相同的JUnit TestCase来测试第1部分中描述的REST Web服务。

package com.sivalabs.resteasydemo;import java.util.List;import org.jboss.resteasy.client.ClientRequest;import org.jboss.resteasy.client.ClientResponse;import org.jboss.resteasy.util.GenericType;import org.junit.Assert;import org.junit.Test;import com.sivalabs.resteasydemo.User;public class UserResourceTest {static final String ROOT_URL = 'http:localhost:8080resteasy-demorest';@Testpublic void testGetUsers() throws Exception {ClientRequest request = new ClientRequest(ROOT_URL+'users');ClientResponse<List<User>> response = request.get(new GenericType<List<User>>(){});List<User> users = response.getEntity();Assert.assertNotNull(users);}@Testpublic void testGetUserById() throws Exception {ClientRequest request = new ClientRequest(ROOT_URL+'users1');ClientResponse<User> response = request.get(User.class);User user = response.getEntity();Assert.assertNotNull(user);}@Testpublic void testSaveUser() throws Exception {User user = new User();user.setId(3);user.setName('User3');user.setEmail('user3@gmail.com');ClientRequest request = new ClientRequest(ROOT_URL+'users');request.body('applicationxml', user);ClientResponse<String> response = request.post(String.class);String statusXML = response.getEntity();Assert.assertNotNull(statusXML);}@Testpublic void testDeleteUser() throws Exception {ClientRequest request = new ClientRequest(ROOT_URL+'users2');ClientResponse<String> response = request.delete(String.class);String statusXML = response.getEntity();Assert.assertNotNull(statusXML);}}

重要注意事项:

1.应先注册org.jboss.resteasy.plugins.server.servlet.ResteasyBootstrap监听器。
2.如果HttpServletDispatcher Servlet URL模式不是/ *,则应配置resteasy.servlet.mapping.prefix @lt; context-param>
3.您应该通过使用@Component或@Service进行注释将REST Resource注册为Spring bean。

参考: RESTEasy教程第2部分:我的JCG合作伙伴 Siva Reddy在“ 技术上的我的实验”博客上的 Spring Integration 。


翻译自: https://www.javacodegeeks.com/2012/06/resteasy-tutorial-part-2-spring.html

resteasy

本文来自互联网用户投稿,该文观点仅代表作者本人,不代表本站立场。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如若转载,请注明出处:http://www.mzph.cn/news/354406.shtml

如若内容造成侵权/违法违规/事实不符,请联系多彩编程网进行投诉反馈email:809451989@qq.com,一经查实,立即删除!

相关文章

前端代码加密

虽然浏览器会把加密的代码破解在解析&#xff0c;但是还是可以稍微加密&#xff0c;或者人家就是破解不了呢哈哈哈哈&#xff01;&#xff01;&#xff01; 1、脚本之家 推荐&#xff1a;HTML原代码加、解密脚本 https://www.jb51.net/tools/html_jiami.htm CSS代码格式化和…

原型链继承

转载于:https://www.cnblogs.com/cmblog/p/8079773.html

清洁责任–摆脱均等,compareTo和toString

您是否看过Java中Object类的javadoc&#xff1f; 大概。 您倾向于时不时地到达那里&#xff0c;然后沿着继承树进行挖掘。 您可能已经注意到的一件事是&#xff0c;每个类都必须继承许多方法。 实现自己而不是坚持使用原始方法的最喜欢的方法可能是.toString&#xff08;&#…

密码保护

1.更新User对象&#xff0c;设置对内的_password class User(db.Model): __tablename__ user _password db.Column(db.String(200), nullableFalse) #内部使用 2.编写对外的password from werkzeug.security import generate_password_hash, check_password_hash property d…

linux 安装wdcp控制面板

**wdCP是WDlinux Control Panel的简称,**是一套通过WEB控制和管理服务器的Linux服务器管理系统以及虚拟主机管理系统。 可以查看服务器情况,资源利用率,系统负载,内存使用率,带宽使用率&#xff1b; 可以轻松创建网站,开站点,发布网站,创建FTP,创建mysql数据库&#xff1b; 可以…

9.proc目录下的文件和目录详解

1./proc目录下的文件和目录详解 /proc:虚拟目录.是内存的映射,内核和进程的虚拟文件系统目录,每个进程会生成1个pid&#xff0c;而每个进程都有1个目录. /proc/Version:内核版本 /proc/sys/kernel:系统内核功能 /proc/sys/net/ipv4: /proc/meminfo:系统内存信息,free -m /proc/…

java线程死锁_Java线程死锁–案例研究

java线程死锁本文将描述从在IBM JVM 1.6上运行的Weblogic 11g生产系统中观察到的最新Java死锁问题的完整根本原因分析。 此案例研究还将证明掌握线程转储分析技能的重要性&#xff1b; 包括用于IBM JVM Thread Dump格式。 环境规格 – Java EE服务器&#xff1a;Oracle Weblo…

JS实现禁止浏览器后退返回上一页

<script type"text/javascript"> $(function() {//防止页面后退history.pushState(null, null, document.URL);window.addEventListener(popstate, function () {history.pushState(null, null, document.URL);});}) </script>

网页中文乱码--UTF-8和GB2312互转

一、如果你想把utf-8转为GB2312 1、用记事本打开源码&#xff0c;把换成&#xff1b;如果是JS不需要加这句&#xff0c;如果是网页最好加上这句和你页面对应的编码。 2、用记事本打开源码&#xff0c;另存为&#xff0c;编码 哪里选择 ANSI 即可。 二、如果你想把GB2312转为…

将JAR依赖项添加到Eclipse插件Maven Tycho构建

开发OPP项目时&#xff0c;一直困扰着我的是使用硬编码Java库依赖项。 我手动下载了所用库的jar &#xff0c;将其复制到需要它们的插件中的目录中&#xff0c;然后将其添加到MANIFEST.MF文件中。 您可能会问我为什么要这样做。 好吧&#xff0c;Eclipse插件&#xff08;或更正…

如何查看一个网站是否部署了SSL证书?

如何才能确定一个网站是否部署了安全的SSL证书呢&#xff1f; 答&#xff1a;能用https方式访问的站点 如果此网站部署SSL证书&#xff0c;则在需要加密的页面会自动从 http:// 变为 https:// &#xff0c;如果没有变&#xff0c;你认为此页面应该加密&#xff0c;您也可以尝试…

页面加载时模块移入动画---wow

首先官网下载&#xff1a;https://github.com/matthieua/WOW animate.css wow.js 1&#xff0c;在头部引用animate.css <link rel"stylesheet" href"css/animate.css">2&#xff0c;body底部引入wow.js 且初始化一下 <script src"js/wow.…

人工智能的概念和知识构架_概念验证:玩! 构架

人工智能的概念和知识构架我们正在开始一个新项目&#xff0c;我们必须选择Web框架。 我们的默认选择是grails&#xff0c;因为团队已经拥有使用它的经验&#xff0c;但是我决定给Play&#xff01; 和Scala有机会。 玩&#xff01; 有很多很酷的东西&#xff0c;在我的评估中&a…

团队-科学计算器-模块测试过程

项目托管平台地址&#xff1a;https://gitee.com/mamamayun/KeXueJiSuanQia/tree/master/calculator_soul3.5 模块测试:进行加减乘除运算 测试方法:反复进行计算 其他补充说明: 无 转载于:https://www.cnblogs.com/dunianze/p/8092874.html

linux 系统安装mongodb数据库---方法2

我是安装在/home/mongodb 1&#xff0c;进入/home/mongodb wget https://fastdl.mongodb.org/linux/mongodb-linux-x86_64-4.0.7.tgz tar -zxvf mongodb-linux-x86_64-4.0.7.tgz cd mongodb-linux-x86_64-4.0.7/ 创建两个目录 存放数据库和日记 mkdir -p logs mkdir -p dat…

网络排错命令工具

ping tracert nslookup dig netstat 转载于:https://www.cnblogs.com/changha0/p/8111134.html

JLBH示例4 – QuickFix vs ChronicleFix基准化

在这篇文章中&#xff1a; 使用JLBH测试QuickFIX 观察QuickFix延迟如何通过百分位数降低 比较QuickFIX和Chronicle FIX 如JLBH简介中所述&#xff0c;创建JLBH的主要原因是为了测量Chronicle-FIX引擎。 我们使用了JLBH的所有功能&#xff0c;特别是吞吐量杠杆和协调遗漏的…

设计模式----java的单例模式

单例模式&#xff08;Singleton Pattern&#xff09;是一个比较简单的模式&#xff0c;它确保某一个类只有一个实例&#xff0c;而且自行实例化并向整个系统提供这个实例。今天我们就来学习一下单例模式的用法。有生之年&#xff0c;一起去看看这个美丽易碎的世界。凡有等待&am…

linux下安装pm2

提前安装node linux下安装pm2 全局安装 npm install pm2 -g安装完成后可以查看pm2的所在目录 创建软连接----根据上面的安装目录创建 ln -s /home/node/nodejs/lib/node_modules/pm2/bin/pm2 /usr/local/bin/查看进程 pm2 list安装成功&#xff01;&#xff01;&#xff…

主机关机后第二天就无法开机_工控机几种常见的在开机或关机后不能正常使用的故障处理方法汇总...

工控机开机启动时我们经常会碰到各种不能正常使用的问题&#xff0c;下面我们把这类故障现象及处理方法在这里给大家汇总分析一下&#xff0c;希望你在碰到类似的问题时&#xff0c;能给你们提供一定的帮助&#xff01;故障现象一&#xff1a;工控机在开机过程中出现死机故障.故…