1 什么是resteasy
resteasy是JBoss的一个开源项目,提供各种框架帮助你构建RESTful Web Services和RESTful Java应用程序。它是JAX-RS规范的一个完整实现并通过JCP认证。作为一个JBOSS的项目,它当然能和JBOSS应用服务器很好地集成在一起。 但是,它也能在任何运行JDK5或以上版本的Servlet容器中运行。RESTEasy还提供一个RESTEasy JAX-RS客户端调用框架。能够很方便与EJB、Seam、Guice、Spring和Spring MVC集成使用。支持在客户端与服务器端自动实现GZIP解压缩。
2 构建项目工程
基于resteasy版本:2.2.1.GA。使用maven作为构建和依赖管理工具。
2.1 编写pom.xml
确保Eclipse安装了m2eclipse。
步骤一:创建文件夹并命名resteasy-demo(名字可以自己随便取)。创建文件并命名为pom.xml。内容如下:
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/maven-v4_0_0.xsd"> <modelVersion>4.0.0</modelVersion> <groupId>rest.resteasy</groupId> <artifactId>resteay-demo</artifactId> <packaging>war</packaging> <version>1.0</version> <name>rest-resteay-demo Maven Webapp</name> <url>http://maven.apache.org</url> <repositories> <repository> <id>java.net</id> <url>http://download.java.net/maven/1</url> <layout>legacy</layout> </repository> <repository> <id>maven repo</id> <name>maven repo</name> <url>http://repo1.maven.org/maven2/</url> </repository> <!-- For resteasy --> <repository> <id>jboss</id> <name>jboss repo</name> <url>http://repository.jboss.org/nexus/content/groups/public/</url> </repository> </repositories> <dependencies> <dependency> <groupId>org.jboss.resteasy</groupId> <artifactId>resteasy-jaxrs</artifactId> <version>2.2.1.GA</version> <!-- filter out unwanted jars --> <exclusions> <exclusion> <groupId>commons-httpclient</groupId> <artifactId>commons-httpclient</artifactId> </exclusion> <exclusion> <groupId>javax.servlet</groupId> <artifactId>servlet-api</artifactId> </exclusion> <exclusion> <groupId>javax.xml.bind</groupId> <artifactId>jaxb-api</artifactId> </exclusion> <exclusion> <groupId>com.sun.xml.bind</groupId> <artifactId>jaxb-impl</artifactId> </exclusion> </exclusions> </dependency> <dependency> <groupId>org.jboss.resteasy</groupId> <artifactId>resteasy-jettison-provider</artifactId> <version>2.2.1.GA</version> <exclusions> <exclusion> <groupId>javax.xml.bind</groupId> <artifactId>jaxb-api</artifactId> </exclusion> <exclusion> <groupId>com.sun.xml.bind</groupId> <artifactId>jaxb-impl</artifactId> </exclusion> <exclusion> <groupId>javax.xml.stream</groupId> <artifactId>stax-api</artifactId> </exclusion> </exclusions> </dependency> <dependency> <groupId>junit</groupId> <artifactId>junit</artifactId> <version>3.8.1</version> <scope>test</scope> </dependency> </dependencies> <build> <finalName>rest-resteay-demo</finalName> <plugins> <plugin> <groupId>org.apache.maven.plugins</groupId> <artifactId>maven-compiler-plugin</artifactId> <configuration> <source>1.6</source> <target>1.6</target> </configuration> </plugin> <plugin> <groupId>org.apache.tomcat.maven</groupId> <artifactId>tomcat7-maven-plugin</artifactId> <version>2.0</version> </plugin> </plugins> </build> </project>
步骤二:在Eclipse中导入Maven项目,如下图:
选择刚刚创建pom.xml的文件夹,注意是文件夹,不是pom.xml文件。如下图:
选中该文件夹之后效果如下:
点击Finish之后,在Package Explorer中的效果如下所示:
2.2 编写jax-rs服务类
package resteasy.server; import javax.ws.rs.GET; import javax.ws.rs.Path; import javax.ws.rs.PathParam; @Path(value = "echo") public class Echo { @GET @Path(value = "/{message}") public String echoService(@PathParam("message") String message) { return message + ",hello"; } }
@Path表示开启访问这个资源的路径
@GET表示响应HTTP 的get方法
@PathParam表示引用URI中得参数
2.3 web.xml 的配置
在WEB-INF目录下面创建文件web.xml。内容如下:
<!DOCTYPE web-app PUBLIC "-//Sun Microsystems, Inc.//DTD Web Application 2.3//EN" "http://java.sun.com/dtd/web-app_2_3.dtd" > <web-app> <context-param> <param-name>resteasy.resources</param-name> <param-value>resteasy.server.Echo</param-value> </context-param> <listener> <listener-class> org.jboss.resteasy.plugins.server.servlet.ResteasyBootstrap </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>/*</url-pattern> </servlet-mapping> </web-app>
2.4 发布
配置Maven build。
在Goals中填入tomcat7:run
点击运行。控制台出现以下信息,表示发布成功:
然后访问http://localhost:8080/resteay-demo/echo/world,网页上出现world,hello则成功。
如果您认为此文章有用,请点击底端的【顶】让其他人也了解此文章。
此博客为原创,转载请保留此出处,谢谢。http://free9277.iteye.com/blog/1848848
相关推荐
该资源是java restful的应用框架resteasy---实现了jax-rs规范的一个demo,如有疑问请联系caizhh2009@gmail.com
resteasy入门级文档、如何使用Restful进行WebService的开发简介及相关RestEasy相关资源的链接
jaxrs-招摇带有 RestEasy 的 Swagger 2.0 Hello World 示例。 它使用 swagger-core 1.5.0-M2 版本。 由于 1.5.0 尚未发布,因此您需要自行构建 swagger 核心并将其安装在本地 Maven 存储库中。构建 swagger-core git...
Test http://localhost:8082/resteasy/hello/world 教程 jax-rs规范用法: http://www.vogella.com/tutorials/REST/article.html resteasy 教程: http://code.freedomho.com/9541.html 名次解释 RESTEasy:RESTEasy...
resteasy
Spring整合RestEasy示例工程源码,Spring+Jetty+RestEasy
resteasy讲解
resteasy简单demo使用的jar包
RESTEasy入门经典.docx
详细描述了restEasy在servlet3.0前后的不同使用方法;restEasy 结合httpServer 无需容器开发;restEasy的通用差错拦截;附详细实现源码!
NULL 博文链接:https://aa00aa00.iteye.com/blog/1033250
resteasy不使用tomcat使用netty实现,实现了高性能,也可以不需要tomcat的容器,一个主函数就能执行
使用RESTEasy+guice实现上传下载文件
resteasy-jaxrs-2.3.2框架源码。希望您了解Resteasy框架有所帮助
RestEasyHelloWorld RestEasy Hello世界示例
resteasy资料整理,使技术人员快速入门。快速开发rest接口。
RESTEasy JSON DEMO
resteasy参考文档及jar包
resteasy-reference-guide-en-US.pdf