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 ...
随机推荐
- PHP artisan migrate 报错显示 could not find driver ,怎么办?
记录下自己的错误 一. 1.原因是没有pdo扩展导致的, 2.解决办法:打开php.ini,然后去掉以下两行代码之前的分号(:)即可.如下: extension=php_pdo_firebird.dl ...
- coding规约的网站, 从sonar中链接过去
一个coding规约的网站, 从sonar中链接过去的. 挺好. https://wiki.sei.cmu.edu/confluence/display/seccode/SEI+CERT+Coding ...
- Python——网络编程基础
一.TCP/IP 是Internet的基础协议,分四层应用层(HTTP,SMTP),传输层(TCP/UDP),网络层(IP),接口层 二.常用默认端口号 80-TCP-HTTP 23-TCP-TELN ...
- 10.4 Vue 父子传值
简单示例 APP.vue <template> <div> <img :src="imgSrc"> <!-- 父子传值 --> &l ...
- ant在windows及linux环境下安装
ant下载 http://ant.apache.org/ https://ant.apache.org/bindownload.cgi 历史版本 ant在windows下安装 解压到D盘 新建系统变量 ...
- 20165223 《信息安全系统设计基础》 实现mybash
一.了解 mybash 1. 简介 bash 是 Bourne Again Shell 的缩写,是linux默认的标准shell(也是大家常说的系统内核),bash也是Unix/Linux上常见的 ...
- Vue+koa2开发一款全栈小程序(7.图书录入功能)
1.图书录入功能 1.获取图书信息 1.在mydemo/src/until.js中封装工具函数post和get // 工具函数 import config from './config' // htt ...
- 通过crontab调度java -jar任务提示"nohup: failed to run command `java': No such file or directory"
通过crontab无法运行,提示如标题的信息: 但直接在终端控制台执行sh和java -jar都可以: 网上给的提示解决方法,在.sh文件开始上面加上 source /etc/profile 然后cr ...
- POJ 1015 Jury Compromise(双塔dp)
Jury Compromise Time Limit: 1000MS Memory Limit: 65536K Total Submissions: 33737 Accepted: 9109 ...
- (最短路 Floyd) P2910 [USACO08OPEN]寻宝之路Clear And Present Danger 洛谷
题意翻译 题目描述 农夫约翰正驾驶一条小艇在牛勒比海上航行. 海上有N(1≤N≤100)个岛屿,用1到N编号.约翰从1号小岛出发,最后到达N号小岛. 一张藏宝图上说,如果他的路程上经过的小岛依次出现了 ...