jersey+maven构建restful服务
一、新建一个Maven Web项目
a) 新建一个简单的Maven项目



b) 将简单的Maven项目转成Web项目


(若没出现further configuration available……或里面的内容不是context相关设置,将Dynamic Web Module版本调高一些试试就自动出现了)

二、修改pom文件,添加jersey依赖
<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.zql</groupId><artifactId>jersey-restful</artifactId><version>0.0.1-SNAPSHOT</version><packaging>war</packaging><name>jersey-restful</name><description>Jersey构建restful服务入门</description><dependencies><dependency><groupId>org.glassfish.jersey.containers</groupId><artifactId>jersey-container-servlet</artifactId><version>2.17</version></dependency><dependency><groupId>org.glassfish.jersey.core</groupId><artifactId>jersey-client</artifactId><version>2.17</version></dependency></dependencies></project>
三、修改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"xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_3_0.xsd"id="WebApp_ID"version="3.0"><display-name>jersey-restful</display-name><servlet><servlet-name>jersey-restful</servlet-name><servlet-class>org.glassfish.jersey.servlet.ServletContainer</servlet-class><init-param><param-name>jersey.config.server.provider.packages</param-name><param-value>com.zql</param-value></init-param><load-on-startup>1</load-on-startup></servlet><servlet-mapping><servlet-name>jersey-restful</servlet-name><url-pattern>/rest/*</url-pattern></servlet-mapping></web-app>
四、构建RestFul服务
package com.zql.model;public class User {public int age;public String name;public String getName() {return name;}public void setName(String name) {this.name = name;}public int getAge() {return age;}public void setAge(int age) {this.age = age;}}
package com.zql.controller;import javax.ws.rs.GET;import javax.ws.rs.Path;import javax.ws.rs.PathParam;import javax.ws.rs.Produces;import javax.ws.rs.QueryParam;import javax.ws.rs.core.MediaType;import com.zql.model.User;@Path("/restful")public class RestfulTest {@GET@Produces(MediaType.TEXT_PLAIN)public String sayHello() {return "hello world!";}@GET@Path("/{param}")@Produces("text/plain;charset=UTF-8")public String sayHello2UserByText(@PathParam("param") String username) {return "Hello " + username;}@GET@Path("/get")@Produces(MediaType.APPLICATION_JSON)public User sayHelloToUserByJson(@QueryParam("username") String username) {User user = new User();user.setAge(11);user.setName(username);return user;}}
发布服务:

测试服务:
http://localhost:8080/jersey-restful/rest/restfulhttp://localhost:8080/jersey-restful/rest/restful/zqlhttp://localhost:8080/jersey-restful/rest/restful/get?username=zql
类似下面图解。

jersey-restful为web.xml文件中<display-name>节点值;
rest为web.xml文件中<url-pattern>节点值;
restful为服务类@Path定义的名称;
zql为服务类方法的@Path定义的方法名;
测试结果:
hello world!Hello zql{"age": 11,"name": "zql"}
参考:
https://blog.csdn.net/mole/article/details/50214971
https://blog.csdn.net/liuchuanhong1/article/details/52880598
jersey+maven构建restful服务的更多相关文章
- 用Jersey构建RESTful服务7--Jersey+SQLServer+Hibernate4.3+Spring3.2
一.整体说明 本例执行演示了用 Jersey 构建 RESTful 服务中.怎样集成 Spring3 二.环境 1.上文的项目RestDemo 2.Spring及其它相关的jar ,导入项目 三.配置 ...
- Springboot & Mybatis 构建restful 服务二
Springboot & Mybatis 构建restful 服务二 1 前置条件 成功执行完Springboot & Mybatis 构建restful 服务一 2 restful ...
- Springboot & Mybatis 构建restful 服务
Springboot & Mybatis 构建restful 服务一 1 前置条件 jdk测试:java -version maven测试:命令行之行mvn -v eclipse及maven插 ...
- Springboot & Mybatis 构建restful 服务五
Springboot & Mybatis 构建restful 服务五 1 前置条件 成功执行完Springboot & Mybatis 构建restful 服务四 2 restful ...
- Springboot & Mybatis 构建restful 服务四
Springboot & Mybatis 构建restful 服务四 1 前置条件 成功执行完Springboot & Mybatis 构建restful 服务三 2 restful ...
- Springboot & Mybatis 构建restful 服务三
Springboot & Mybatis 构建restful 服务三 1 前置条件 成功执行完Springboot & Mybatis 构建restful 服务二 2 restful ...
- Dubbo-使用Maven构建Dubbo服务的可执行jar包
一.为什么要构建Dubbo服务的可执行jar包? 1.1 Dubbo服务运行方式比较 ✎使用Servlet容器运行(Tomcat.Jetty等) ---不可取 --缺点:增加复杂性(多了容器的端口) ...
- CXF+Spring+JAXB+Json构建Restful服务
话不多说,先看详细的样例: 文件文件夹结构: web.xml <?xml version="1.0" encoding="UTF-8"? > < ...
- 使用spring mvc或者resteasy构建restful服务
看到最近一个项目里用resteasy来构建restful接口,有点不明白,不少Spring mvc4.0以后也可以很方面的实现restful服务吗,为啥还要在Spring MVC的项目里还引入rest ...
随机推荐
- try.dot.net 的正确使用姿势
[简介] 微软官方前不久发布了 try.dot.net 这个有趣的网址,开始只是图个新鲜看了一下,后面通过自身实践过后,发现这着实算是个“有趣”的站点! 首先我们大概地列举一下这个站点能给我们带来什么 ...
- mybatis 中 foreach collection的三种用法
foreach的主要用在构建in条件中,它可以在SQL语句中进行迭代一个集合. foreach元素的属性主要有 item,index,collection,open,separator,close. ...
- Python的GIL机制与多线程编程
GIL 全称global interpreter lock 全局解释锁 gil使得python同一个时刻只有一个线程在一个cpu上执行字节码,并且无法将多个线程映射到多个cpu上,即不能发挥多个cpu ...
- @deprecated 的方法处理
因为需要用到poi,偷懒不太想看官方文档,同时自己的github账号忘记密码了.所以直接在别人博客那拷贝一段代码来模仿修改创建HSSF的xsl文件. 虽然能运行,但发现代码太多横线,可以知道方法被标注 ...
- vue.js实战——splice使用
Vue在检测到数组变化时,并不是直接重新渲染整个列表,而是最大化地复用DOM元素.替换的数组中含有相同元素的项不会被重新渲染,因此可以大胆地用新数组来替换就数组,不用担心性能问题. 需要注意的是,以下 ...
- Python基础知识5-递归函数、生成器
函数执行流程* 递归Recursion 递归的性能 递归总结 递归练习: def fac(n): if n==1: return n return n*fac(n-1) def fac1(n, f= ...
- python基础----特性(property)、静态方法(staticmethod)、类方法(classmethod)、__str__的用法
http://www.cnblogs.com/wangyongsong/p/6750454.html#_label0
- OOM实例
1. 使用Executors.newFixedThreadPool()方法,当不断创建新任务,而任务执行速度比创建速度慢时,任务对象就会在任务队列里面排队,堆内存得不到释放,导致OOM: 2. 使用P ...
- spring boot简单的小demo(适合于初学者)
import com.example.demo2.com.example.dao.ShopDao; import com.example.demo2.com.example.entity.Shops; ...
- BZOJ 3613: [Heoi2014]南园满地堆轻絮(二分)
题面: https://www.lydsy.com/JudgeOnline/problem.php?id=3613 题解: 考虑前面的数越小答案越优秀,于是我们二分答案,判断时让前面的数达到所能达到的 ...