Jersey构建Restful风格的webservices
最近一直在搞老项目的开发工作,很少写博文了。听了两位阿里巴巴大牛的讨论,决定试试用restful风格的webservices看看。
这里用的是Jersey这个框架,刚开始弄,有点麻烦,只能到处查资料。网上的资料也比较零碎,这里整理一下。
一.helloworld
需要三个包,这里从maven获取。
<dependencies>
<dependency>
<groupId>com.sun.jersey</groupId>
<artifactId>jersey-core</artifactId>
<version>1.18</version>
</dependency>
<dependency>
<groupId>com.sun.jersey</groupId>
<artifactId>jersey-json</artifactId>
<version>1.18</version>
</dependency>
<dependency>
<groupId>com.sun.jersey</groupId>
<artifactId>jersey-servlet</artifactId>
<version>1.18</version>
</dependency>
</dependencies>
然后再web.xml中代码:
<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>hello.resource</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>
写一个资源类:
@Path("hello")
public class HelloResource {
@GET
@Produces(MediaType.TEXT_PLAIN)
public String greed(){
return "hello world";
}
}
这样,访问:http://localhost:8080/rest/hello ,可以看到页面会输出:hello world
二.传递参数
在HelloResource这个类中,新加一个方法:
@GET
@Produces(MediaType.TEXT_PLAIN)
@Path("{id}")
public String sayHello(@PathParam("id")int id){
return "hello jersey "+id;
}
此时,访问:http://localhost:8080/rest/hello/123 ,页面会输出:
hello jersey 123
后面的123即是参数id的值。试了一下,如果再写一个一样的方法,把参数类型改成String类型,当输入参数类型不同的时候,会自动识别不同的方法。不过中文不可以直接跟在后面,会有乱码的情况。
我们一般传递参数的话,是采用?符来进行传参的。上面的方式看上去有点奇怪,不符合我们经常用的方式。
这里还有一种方式,适合我们的方式,不采用PathParam这个属性,采用QueryParam这个。
再写一个方法:
@GET
@Path("/param")
@Produces("text/plain")
public String sayHis(@QueryParam("param")String param){
return "nice "+param;
}
此时,访问:http://localhost:8080/rest/hello/param?param=work ,页面会输出:
nice work
这些算是一个基本入门,关于客户端使用等相关知识,在之后的博文中再写。
这里有一篇博文写的也不错:
http://my.oschina.net/mlongbo/blog/152548#OSC_h5_14
另附glassfish配置方式:
上面是用com.sun的包,还有一种是org.glassfish的包。
配置都差不多。
web.xml:
<?xml version="1.0" encoding="UTF-8"?>
<web-app xmlns="http://java.sun.com/xml/ns/javaee"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://java.sun.com/xml/ns/javaee
http://java.sun.com/xml/ns/javaee/web-app_3_0.xsd"
version="3.0">
<servlet>
<servlet-name>Jersey REST Service</servlet-name>
<servlet-class>
org.glassfish.jersey.servlet.ServletContainer
</servlet-class>
<init-param>
<param-name>jersey.config.server.provider.packages</param-name>
<param-value>hello.resource</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>
目前接触到的区别在于,用glassfish方式,配置后的json,不需要下载,浏览器即可显示出json内容。用sun的话,直接用json格式输出,需要下载打开才可以看到。不知道是不是也需要一些类似的配置。
需要的包:
<dependencies>
<dependency>
<groupId>org.glassfish.jersey.containers</groupId>
<!-- if your container implements Servlet API older than 3.0, use "jersey-container-servlet-core" -->
<artifactId>jersey-container-servlet</artifactId>
<version>2.13</version>
</dependency>
<!-- Required only when you are using JAX-RS Client -->
<dependency>
<groupId>org.glassfish.jersey.core</groupId>
<artifactId>jersey-client</artifactId>
<version>2.13</version>
</dependency>
</dependencies>
Jersey构建Restful风格的webservices的更多相关文章
- Jersey构建Restful风格的Webserivces(三)
一.总体说明 通过jersey-client接口,创建客户端程序,来调用Jersey实现的RESTful服务,实现增.删.改.查等操作. 服务端主要是通过内存的方式,来模拟用户的增加.删除.修改.查询 ...
- Jersey构建restful风格的WebSerivices(二)
一. 总体说明 XML和JSON 是最为常用的数据交换格式.本例子演示如何将java对象,转成XML输出. 二.流程 1.在上文的例子中,创建一个包“com.waylau.rest.bean” 2.在 ...
- lucene构建restful风格的简单搜索引擎服务
来自于本人博客: lucene构建restful风格的简单搜索引擎服务 本人的博客如今也要改成使用lucene进行全文检索的功能,因此在这里把代码贴出来与大家分享 一,文件夹结构: 二,配置文件: 总 ...
- 构建RESTful风格的WCF服务
构建RESTful风格的WCF服务 RESTful Wcf是一种基于Http协议的服务架构风格. 相较 WCF.WebService 使用 SOAP.WSDL.WS-* 而言,几乎所有的语言和网络平台 ...
- SpringMVC 构建Restful风格 及问题处理
基本的请求URL: /person/{id} GET 得到id的person /person POST 新增person /person/{id} PUT 更新id的person / ...
- 用Jersey构建RESTful服务7--Jersey+SQLServer+Hibernate4.3+Spring3.2
一.整体说明 本例执行演示了用 Jersey 构建 RESTful 服务中.怎样集成 Spring3 二.环境 1.上文的项目RestDemo 2.Spring及其它相关的jar ,导入项目 三.配置 ...
- Spring Boot构建 RESTful 风格应用
Spring Boot构建 RESTful 风格应用 1.Spring Boot构建 RESTful 风格应用 1.1 实战 1.1.1 创建工程 1.1.2 构建实体类 1.1.4 查询定制 1.1 ...
- SpringBoot实战(一)之构建RestFul风格
RestFul风格是一种非常流行的架构风格,相关实战可以参考我的这篇博客:SSM框架之RestFul示例 论文可参考:https://www.ics.uci.edu/~fielding/pubs/di ...
- springMVC+json构建restful风格的服务
首先.要知道什么是rest服务,什么是rest服务呢? REST(英文:Representational State Transfer,简称REST)描写叙述了一个架构样式的网络系统.比方 web 应 ...
随机推荐
- mac安装protobuf2.4.1时报错./include/gtest/internal/gtest-port.h:428:10: fatal error: 'tr1/tuple' file not found和google/protobuf/message.cc:175:16: error: implicit instantiation of undefined template
通过网上下载的protobuf2.4.1的压缩文件,然后进行安装,./configure和make时遇到了两个问题. 正常的安装步骤如下: ./configure make make check m ...
- maven向本地库添加jar包
mvn install:install-file -DgroupId=com.lowagie -DartifactId=itextasian -Dversion=1.0 -Dpackaging=jar ...
- SQL Server多条件查询的实现
SQL Server多条件查询的实现 SQL Server多条件查询我们经常会用到,下面就教您如何使用存储过程实现SQL Server多条件查询,希望对您学习SQL Server多条件查询方面有所帮助 ...
- mysql的外键知识
外键的作用 1.用来约束两张表中的字段 2.外键也可以用来实现一对多 我们先举一个这样的例子,让大家对外键有一个基本的认识 当前我们有一个需求就是,需要创建一张表,这张表要包括“姓名”,“年龄”,“工 ...
- 利用Python和webhook实现自动提交代码
最近在为公司书写项目的api文档,计划利用码云的wiki管理整个项目,公司自有git作为项目内容依托,这样全员都可参与,而我定期向码云推送就可以了. 问题 根据需求遇见了这样一个问题:我每次从git上 ...
- adf常用方法总结
1.使用clientAttribute传值.获取值 或组件上面放客户端属性 <af:selectBooleanCheckbox text="" label="&qu ...
- metasploit渗透测试指南概要整理
一.名词解释 exploit 测试者利用它来攻击一个系统,程序,或服务,以获得开发者意料之外的结果.常见的 有内存溢出,网站程序漏洞利用,配置错误exploit. payload 我们想让被攻击系统执 ...
- 移动端input验证只允许有数字 在safari浏览器一直不成功解决
<input class="lineHeight-30" type="text" onkeyup="value=value.replace(/[ ...
- 基于TCP的套接字
tcp服务端 1 ss = socket() #创建服务器套接字 2 ss.bind() #把地址绑定到套接字 3 ss.listen() #监听链接 4 inf_loop: #服务器无限循环 5 c ...
- PAT 1015 德才论 (25)(代码+思路)
1015 德才论 (25)(25 分)提问 宋代史学家司马光在<资治通鉴>中有一段著名的"德才论":"是故才德全尽谓之圣人,才德兼亡谓之愚人,德胜才谓之君子, ...