一直想体验一下REST风格的Web服务,最近关注了一下Jersey这个Java的REST实现,记录一下备忘。
这里有一篇不错的文章:http://www.vogella.com/articles/REST/article.html
REST的Resource类实现:
package wh.rest.jersey.first;
import javax.ws.rs.GET;
import javax.ws.rs.Path;
import javax.ws.rs.Produces;
import javax.ws.rs.core.MediaType;
/**
* @Author:WangHuan
*
* @Date:2012-5-29
*
* @TODO:
*/
@Path("/hello")
public class HelloResource {
// This method is called if TEXT_PLAIN is request
@GET
@Produces(MediaType.TEXT_PLAIN)
public String sayPlainTextHello() {
return "Hello Jersey";
}
// This method is called if XML is request
@GET
@Produces(MediaType.TEXT_XML)
public String sayXMLHello() {
return "<?xml version=\"1.0\"?>" + "<hello> Hello Jersey" + "</hello>";
}
// This method is called if HTML is request
@GET
@Produces(MediaType.TEXT_HTML)
public String sayHtmlHello() {
return "<html> " + "<title>" + "Hello Jersey" + "</title>"
+ "<body><h1>" + "Hello Jersey" + "</body></h1>" + "</html> ";
}
}
web.xml
<?xml version="1.0" encoding="UTF-8"?>
<web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns="http://java.sun.com/xml/ns/javaee" xmlns:web="http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd"
xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd"
id="WebApp_ID" version="2.5">
<display-name>wh.rest.jersey.first</display-name>
<servlet>
<servlet-name>Jersey REST Service</servlet-name>
<servlet-class>com.sun.jersey.spi.container.servlet.ServletContainer</servlet-class>
<init-param>
<param-name>com.sun.jersey.config.property.packages</param-name>
<param-value>wh.rest.jersey.first</param-value>
</init-param>
<load-on-startup>1</load-on-startup>
</servlet>
<servlet-mapping>
<servlet-name>Jersey REST Service</servlet-name>
<url-pattern>/rest/*</url-pattern>
</servlet-mapping>
</web-app>
客户端调用:
package wh.rest.jersey.first;
import java.net.URI;
import javax.ws.rs.core.MediaType;
import javax.ws.rs.core.UriBuilder;
import com.sun.jersey.api.client.Client;
import com.sun.jersey.api.client.ClientResponse;
import com.sun.jersey.api.client.WebResource;
import com.sun.jersey.api.client.config.ClientConfig;
import com.sun.jersey.api.client.config.DefaultClientConfig;
/**
* @Author:WangHuan
*
* @Date:2012-5-29
*
* @TODO:
*/
public class HelloClient {
public static void main(String[] args) {
ClientConfig config = new DefaultClientConfig();
Client client = Client.create(config);
WebResource service = client.resource(getBaseURI());
// Fluent interfaces
System.out.println(service.path("rest").path("hello").accept(
MediaType.TEXT_PLAIN).get(ClientResponse.class).toString());
// Get plain text
System.out.println(service.path("rest").path("hello").accept(
MediaType.TEXT_PLAIN).get(String.class));
// Get XML
System.out.println(service.path("rest").path("hello").accept(
MediaType.TEXT_XML).get(String.class));
// The HTML
System.out.println(service.path("rest").path("hello").accept(
MediaType.TEXT_HTML).get(String.class));
}
private static URI getBaseURI() {
return UriBuilder.fromUri(
"http://localhost:8080/jersey").build();
}
}
最终的访问URL是:http://localhost:8080/jersey/rest/hello
其中hello对应的实现类中的
@Path("/hello")
分享到:
相关推荐
使用Jersey框架搭建Rest服务
JAVA+Spring+Hibernate使用Jersey开发Rest_WEB_SERVICES 祝大家学习快乐
rest jersey 所必须的jar包,初学开发jersey所必须的jar
jersey实现简单的rest接口 返回json数据
jersey构建rest服务,通过mybatis访问数据库
Jersey Java规范下REST风格Web Service开发框架
Jersey-rest服务的Jar包1.8.2版本,含源代码
JAVA+Spring+Hibernate使用Jersey开发Rest WEB SERVICES
Jersey Rest Web Service 例子源码
搭建,搭建,搭建,搭建,搭建,搭建,搭建,搭建,搭建,搭建,搭建,搭建,搭建,搭建,搭建,搭建,搭建,搭建,搭建,搭建,搭建,搭建,搭建……
本文档用于描Spring+Jersey框架的搭建Rest服务的过程及其注意事项。主要介绍环境配置、Spring框架配置、Jersey与Spring的集成配置,以及前端Ajax的调用Demo。
generate -DarchetypeGroupId=org.glassfish.jersey.archetypes -DarchetypeArtifactId=jersey-quickstart-webapp -DarchetypeVersion=2.21 -DgroupId=com.group-xyz -DartifactId=spring-mvc-jersey-rest -...
java项目REST方式使用Jersey技术实现webservice远程调用,内附使用说明文档和Jersey包,方便学习和查阅,最重要的是我的资源优势就是物美价廉哟~~
rest包jersey-1.19.1,包含全部的jar包,下载留评论。开发jersey api接口用到的jar包。是一个软件开发框架,Jersey不仅仅是一个JAX-RS的参考实现,Jersey提供自己的API,其API继承自JAX-RS,提供更多的特性和功能以...
基于rest的jersey的webservice 包含mvc设计模式 单例模式 工厂模式
自己找了资料整合了一份 jersey+spring rest webservice web maven服务器架构,希望有需要的人可以下载使用;
使用 swagger-ui 将 Jersey 用于 REST API + 端点文档 安装 git clone https://github.com/salvitas/JerseySwagger.git cd JerseySwagger mvn clean install mvn jetty:run 网址 示例方法测试: Swagger-ui 文档...
NULL 博文链接:https://tianlinzx.iteye.com/blog/851884