初学resteasy框架
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解压缩。
构建环境
- RESTEasy 2.3.1.GA
- Tomcat 7.0.55
- jdk1.6.0_45
- maven3
- 编辑器IntelliJ IDEA
maven3是用来下载依赖包的可以忽略
部署
创建一个maven web项目
项目结构:
-
src
- com.test
- com.rest WEB-INF
- classes
- lib
- web.xml
更新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>com.rest</groupId>
<artifactId>RESTEasyDemo</artifactId>
<packaging>war</packaging>
<version>0.0.1-SNAPSHOT</version>
<name>RESTfulDemoApplication Maven Webapp</name>
<url>http://maven.apache.org</url>
<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>
</dependency>
<!-- https://mvnrepository.com/artifact/org.scannotation/scannotation -->
<dependency>
<groupId>org.scannotation</groupId>
<artifactId>scannotation</artifactId>
<version>1.0.3</version>
</dependency>
</dependencies>
<build>
<finalName>resteasyDemo</finalName>
</build>
</project>
更新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>
<display-name>create easyrest</display-name>
<!-- Auto scan REST service -->
<context-param>
<param-name>resteasy.scan</param-name>
<param-value>true</param-value>
</context-param>
<servlet>
<servlet-name>resteasy-servlet</servlet-name>
<servlet-class>
org.jboss.resteasy.plugins.server.servlet.HttpServletDispatcher
</servlet-class>
</servlet>
<servlet-mapping>
<servlet-name>resteasy-servlet</servlet-name>
<url-pattern>/*</url-pattern>
</servlet-mapping>
</web-app>
写服务类
package com.test;
import javax.ws.rs.GET;
import javax.ws.rs.Path;
import javax.ws.rs.core.Response;
/**
* Created by Administrator on 2017/6/15 0015.
*/
@Path("/test")
public class Test {
@GET
@Path("/one")
public Response getAllUsers()
{
String result = "<h1>RESTful Demo Application</h1>In real world application, a collection of users will be returned !!";
return Response.status(200).entity(result).build();
}
}
运行及报错
将Test类打包放入bin下
然后把整个项目放入tomcat的webapp下
启动tomcat
访问localhost:8080/test/one
报错 java.lang.ClassNotFoundException: javassist.bytecode.ClassFile
这是因为需要的javassist需要在运行中使用,所以找到这个包并放入目录中的bin文件下
Success:
RESTful Demo Application
In real world application, a collection of users will be returned !!