RestfulWebService实现之一--------------Jetty+Jersey方法一
2018-02-13 10:20
330 查看
通过Jetty+Jersey实现Restful Web Service
一、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/xsd/maven-4.0.0.xsd"> <modelVersion>4.0.0</modelVersion>
<groupId>My.Demo.Restful</groupId>
<artifactId>Jersey-JettyEmbed</artifactId>
<version>0.0.1-SNAPSHOT</version>
<name>mytest</name>
<description>JettyEmbedServer with Jersey</description>
<dependencies>
<dependency>
<groupId>org.eclipse.jetty</groupId>
<artifactId>jetty-servlet</artifactId>
<version>9.4.8.v20171121</version>
</dependency>
<dependency>
<groupId>com.sun.jersey</groupId>
<artifactId>jersey-servlet</artifactId>
<version>1.19.4</version>
</dependency>
</dependencies>
<build>
<plugins>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-compiler-plugin</artifactId>
<version>2.5.1</version>
<inherited>true</inherited>
<configuration>
<source>1.7</source>
<target>1.7</target>
</configuration>
</plugin>
<plugin>
<groupId>org.codehaus.mojo</groupId>
<artifactId>exec-maven-plugin</artifactId>
<version>1.2.1</version>
<executions>
<execution>
<goals>
<goal>java</goal>
</goals>
</execution>
</executions>
<configuration>
<mainClass>my.restful.Main</mainClass>
</configuration>
</plugin>
</plugins>
</build>
</project>
二、Main代码package my.restful;
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 {
public static void main(String[] args) throws Exception {
Server server=new Server(8080);
//////////////////////////ServletHolder////////////////////////////////
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", "my.restful");
/////////////////////////ServletContextHandler//////////////////////////////////////////
ServletContextHandler handler = new ServletContextHandler(ServletContextHandler.SESSIONS);
handler.setContextPath("/");
handler.addServlet(servlet, "/*");
////////////////////////////////////////
server.setHandler(handler);
server.start();
//////////////////////////////////////
System.out.println("JettyStart at 127.0.0.1:8080");
}
}
三、资源类代码package my.restful;
import javax.ws.rs.GET;
import javax.ws.rs.Path;
import javax.ws.rs.Produces;
import javax.ws.rs.core.MediaType;
/**
* Root resource (exposed at "myresource" path)
*/
@Path("myresource")
public class MyResource {
/**
* Method handling HTTP GET requests. The returned object will be sent
* to the client as "text/plain" media type.
*
* @return String that will be returned as a text/plain response.
*/
@GET
@Produces(MediaType.TEXT_PLAIN)
public String getIt() {
return "Got it!";
}
}
一、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/xsd/maven-4.0.0.xsd"> <modelVersion>4.0.0</modelVersion>
<groupId>My.Demo.Restful</groupId>
<artifactId>Jersey-JettyEmbed</artifactId>
<version>0.0.1-SNAPSHOT</version>
<name>mytest</name>
<description>JettyEmbedServer with Jersey</description>
<dependencies>
<dependency>
<groupId>org.eclipse.jetty</groupId>
<artifactId>jetty-servlet</artifactId>
<version>9.4.8.v20171121</version>
</dependency>
<dependency>
<groupId>com.sun.jersey</groupId>
<artifactId>jersey-servlet</artifactId>
<version>1.19.4</version>
</dependency>
</dependencies>
<build>
<plugins>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-compiler-plugin</artifactId>
<version>2.5.1</version>
<inherited>true</inherited>
<configuration>
<source>1.7</source>
<target>1.7</target>
</configuration>
</plugin>
<plugin>
<groupId>org.codehaus.mojo</groupId>
<artifactId>exec-maven-plugin</artifactId>
<version>1.2.1</version>
<executions>
<execution>
<goals>
<goal>java</goal>
</goals>
</execution>
</executions>
<configuration>
<mainClass>my.restful.Main</mainClass>
</configuration>
</plugin>
</plugins>
</build>
</project>
二、Main代码package my.restful;
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 {
public static void main(String[] args) throws Exception {
Server server=new Server(8080);
//////////////////////////ServletHolder////////////////////////////////
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", "my.restful");
/////////////////////////ServletContextHandler//////////////////////////////////////////
ServletContextHandler handler = new ServletContextHandler(ServletContextHandler.SESSIONS);
handler.setContextPath("/");
handler.addServlet(servlet, "/*");
////////////////////////////////////////
server.setHandler(handler);
server.start();
//////////////////////////////////////
System.out.println("JettyStart at 127.0.0.1:8080");
}
}
三、资源类代码package my.restful;
import javax.ws.rs.GET;
import javax.ws.rs.Path;
import javax.ws.rs.Produces;
import javax.ws.rs.core.MediaType;
/**
* Root resource (exposed at "myresource" path)
*/
@Path("myresource")
public class MyResource {
/**
* Method handling HTTP GET requests. The returned object will be sent
* to the client as "text/plain" media type.
*
* @return String that will be returned as a text/plain response.
*/
@GET
@Produces(MediaType.TEXT_PLAIN)
public String getIt() {
return "Got it!";
}
}
相关文章推荐
- 使用Jersey框架实现RESTfulWebService
- Jersey 极致简单的Restful WebService实现
- RESTWebService实现之二 : Jetty+Jersey(WAR方式)
- 【jersey】 spring 整合jersey 实现RESTful webservice
- Jersey实现Restful webservice及Ajax访问
- 一个实例演示jersey框架实现RESTful风格的Webservice,并发布到Tomcat,以及客户端调用示范
- spring4+springMVC+mybatis集成jersey2,一个实现了RESTful的webservice框架。(源码)
- 使用java实现Restful webservice(Jersey)
- java 通过webservice 地址得到其xml文实现方法
- webservice简单详细的实现方法
- 使用 WCF 实现 RESTful 方式的 WebService
- 分享php中四种webservice实现的简单架构方法及实例
- [Jersey]使用jersey实现RESTful web service 之 web.xml配置问题
- 分享php中四种webservice实现的简单架构方法及实例
- js调用webservice中的方法实现思路及代码
- android Webservice实现方法及原理
- 基于spring实现的restful webservice返回json数据
- Java下RESTful WebService的实现框架介绍
- jersey 实现restful经验总结(待续)
- 最简的restful webservice实现,提供get和post服务(提供war包下载)