一、选择Jersey+jetty原因
之前做的项目大部分是PC的项目,没有做的完全的前后端分离,后端使用的是MVC框架 像SpringMVC、Sturts2。最近开发移动webapp项目,因为对SpringMVC框架比较熟悉所以就选择了用SpringMVC框架为前端提供接口,后来发现了一些新的RestFull框架如Jersey、RestEeasy等RestFull框架。对比后觉得使用SpringMVC框架为app提供接口有点“大材小用了”,主要原因:
1. webApp所要的数据主要有HTML、CSS、JS、图片、JSON数据,首先HTML直接放在nginx下面,因为nginx处理静态资源比较快。CSS/JS/图片 放入第三放的CDN云空间中,这样可以省去对图片等静态资源管理的工作,并且可以利用CDN加速。唯一需要后端提供的就是JSON数据,所以后端没必要像PC网站一样在项目中存放js/css/html等资料,只需要处理一个http请求,然后返回JSON数据就可以了;
2. SpringMVC 不仅可以处理http请求返回JSON数据,还可以返回视图如JSP/velocity/freemark等,所以使用SpringMVC 用来作接口显得有点“大材小用了”;
3. 既然只需要处理http返回JSON,项目可以不用打成war包,可以使用jetty这样嵌入式的serlvet容器,它比tomcat更轻量,部署更简单,所以准备使用jersey+jetty 代替SpringMVC+tomcat ,做RestFull接口。
二、项目实战
1、建立Maven项目,配置Pom文件,配置如下所示:
<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/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<groupId>com.zhaochao</groupId>
<artifactId>Jersey_jetty</artifactId>
<version>0.0.1-SNAPSHOT</version>
<dependencies>
<dependency>
<groupId>com.sun.jersey</groupId>
<artifactId>jersey-servlet</artifactId>
<version>1.19</version>
</dependency>
<dependency>
<groupId>com.sun.jersey</groupId>
<artifactId>jersey-client</artifactId>
<version>1.19</version>
</dependency>
<dependency>
<groupId>org.eclipse.jetty.aggregate</groupId>
<artifactId>jetty-all-server</artifactId>
<version>8.1.16.v20140903</version>
</dependency>
</dependencies>
</project>
2、新建Jersey 处理RestFull接口类
package jersey;
import javax.ws.rs.GET;
import javax.ws.rs.Path;
import javax.ws.rs.PathParam;
import javax.ws.rs.Produces;
import javax.ws.rs.core.MediaType;
@Path("/helloworld")
public class JerseyResource {
@GET
@Produces("text/plain")
public String getHello() {
return "hello word...";
}
@GET
@Path("/{id}")
@Produces(MediaType.APPLICATION_JSON)
public String getHelloByid(final @PathParam("id") String id) {
return "hello word..."+id;
}
}
这里是jersey实现的RestFull接口,通过简单的Path注解可以将简单的JAVA类映射成一个接口处理类,@GET表明这个接口只接受HTTP的GET请求,@Path(“{name}”)表明这个name是http路径中这变量,@Produces 表明返回类型,@PathParam(“name”) 会将/hello/name 中的name 注入到hello()参数中的name
3、建立jetty服务并启动
package jersey;
import org.eclipse.jetty.server.Server;
import org.eclipse.jetty.servlet.ServletContextHandler;
import org.eclipse.jetty.servlet.ServletHolder;
import com.sun.jersey.spi.container.servlet.ServletContainer;
public class Main {
/**
* @param args
*/
public static void main(String[] args) throws Exception{
Server server=new Server(82);
ServletHolder servlet = new ServletHolder(ServletContainer.class);
servlet.setInitParameter("com.sun.jersey.config.property.resourceConfigClass", "com.sun.jersey.api.core.PackagesResourceConfig");
servlet.setInitParameter("com.sun.jersey.config.property.packages", "jersey");
ServletContextHandler handler = new ServletContextHandler(ServletContextHandler.SESSIONS);
handler.setContextPath("/");
handler.addServlet(servlet, "/*");
server.setHandler(handler);
server.start();
System.out.println("start...in 82");
}
}
4、访问:
1)在浏览器上输入:http://localhost:82/helloworld
输出:hello world...
2)在浏览器中输入http://localhost:9998/application.wadl, 可以看到
This XML file does not appear to have any style information associated with it. The document tree is shown below.
<application xmlns="http://wadl.dev.java.net/2009/02">
<doc xmlns:jersey="http://jersey.java.net/" jersey:generatedBy="Jersey: 1.19 02/11/2015 03:25 AM"/>
<grammars/>
<resources base="http://localhost:82/">
<resource path="/helloworld">
<method id="getHello" name="GET">
<response>
<representation mediaType="text/plain"/>
</response>
</method>
</resource>
</resources>
</application>
5、使用jersey客户端访问restful服务:
package jersey;
import javax.ws.rs.core.MultivaluedMap;
import com.sun.jersey.api.client.Client;
import com.sun.jersey.api.client.WebResource;
import com.sun.jersey.core.util.MultivaluedMapImpl;
public class JerseyClient {
/**
* @param args
*/
public static void main(String[] args) {
Client c = Client.create();
WebResource r=c.resource("http://localhost:82/helloworld");
/*MultivaluedMap formData = new MultivaluedMapImpl();
formData.add("data", "ST/G001000001612549");
String response = r.type("application/x-www-form-urlencoded")
.post(String.class, formData); */
String string = r.type("text/plain").get(String.class);
System.out.println(string);
}
}
6、linux启动方式:
[root@dev51 jersey]# ls
jerseyDemo.jar
[root@dev51 jersey]# java -jar jerseyDemo.jar
2015-11-30 09:24:50.748:INFO:oejs.Server:jetty-8.y.z-SNAPSHOT
2015-11-30 09:24:50.871:INFO:oejs.AbstractConnector:Started SelectChannelConnector@0.0.0.0:82
start...in 82
6、通过jersey+jetty 的hello项目测试我们可以发现以下几个优点
- 项目更轻量
- 项目代码更简洁
- 项目部署更方便
- 项目并发量更大