一、新建一个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/restful
http://localhost:8080/jersey-restful/rest/restful/zql
http://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服务的更多相关文章

  1. 用Jersey构建RESTful服务7--Jersey+SQLServer+Hibernate4.3+Spring3.2

    一.整体说明 本例执行演示了用 Jersey 构建 RESTful 服务中.怎样集成 Spring3 二.环境 1.上文的项目RestDemo 2.Spring及其它相关的jar ,导入项目 三.配置 ...

  2. Springboot & Mybatis 构建restful 服务二

    Springboot & Mybatis 构建restful 服务二 1 前置条件 成功执行完Springboot & Mybatis 构建restful 服务一 2 restful ...

  3. Springboot & Mybatis 构建restful 服务

    Springboot & Mybatis 构建restful 服务一 1 前置条件 jdk测试:java -version maven测试:命令行之行mvn -v eclipse及maven插 ...

  4. Springboot & Mybatis 构建restful 服务五

    Springboot & Mybatis 构建restful 服务五 1 前置条件 成功执行完Springboot & Mybatis 构建restful 服务四 2 restful ...

  5. Springboot & Mybatis 构建restful 服务四

    Springboot & Mybatis 构建restful 服务四 1 前置条件 成功执行完Springboot & Mybatis 构建restful 服务三 2 restful ...

  6. Springboot & Mybatis 构建restful 服务三

    Springboot & Mybatis 构建restful 服务三 1 前置条件 成功执行完Springboot & Mybatis 构建restful 服务二 2 restful ...

  7. Dubbo-使用Maven构建Dubbo服务的可执行jar包

    一.为什么要构建Dubbo服务的可执行jar包? 1.1 Dubbo服务运行方式比较 ✎使用Servlet容器运行(Tomcat.Jetty等)  ---不可取 --缺点:增加复杂性(多了容器的端口) ...

  8. CXF+Spring+JAXB+Json构建Restful服务

    话不多说,先看详细的样例: 文件文件夹结构: web.xml <?xml version="1.0" encoding="UTF-8"? > < ...

  9. 使用spring mvc或者resteasy构建restful服务

    看到最近一个项目里用resteasy来构建restful接口,有点不明白,不少Spring mvc4.0以后也可以很方面的实现restful服务吗,为啥还要在Spring MVC的项目里还引入rest ...

随机推荐

  1. vi/vim 使用

    1.  vim一共有4个模式:(linux下最好用的编辑器) 正常模式 (Normal-mode) 插入模式 (Insert-mode) 命令模式 (Command-mode) 可视模式 (Visua ...

  2. MySQL索引原理及慢查询优化(转自:美团tech)

    背景 MySQL凭借着出色的性能.低廉的成本.丰富的资源,已经成为绝大多数互联网公司的首选关系型数据库.虽然性能出色,但所谓“好马配好鞍”,如何能够更好的使用它,已经成为开发工程师的必修课,我们经常会 ...

  3. 使用offsetof对结构体指针偏移操作

    题目来自于COMP20003 Tutorial 2: Program m ing Challenge 2.2 The technology stack at Hidebound Inc. uses a ...

  4. Python——控件事件

    鼠标 键盘 窗口 按钮

  5. Linux 学习 (六) 关机与重启命令

    Linux达人养成计划 I 学习笔记 shutdown [选项] 时间 -c:取消前一个关机命令 -h:关机 -r:重启 shutdown命令会在关机或重启时自动保存系统中正在运行的服务,最安全的关机 ...

  6. 17.kubernete的dashboard

    部署dashboard kubectl apply -f https://raw.githubusercontent.com/kubernetes/dashboard/v1.10.1/src/depl ...

  7. Unity3d中如何查找一个脚本被挂在那些预设上面?

    用一个脚本函数可以获取到选择的脚本文件被哪些预设和场景引用 [MenuItem("Assets/Tool/GetReference")] static void GetRefere ...

  8. zookeeper的单实例和伪集群部署

    原文链接: http://gudaoyufu.com/?p=1395 zookeeper工作方式 ZooKeeper 是一个开源的分布式协调服务,由雅虎创建,是 Google Chubby 的开源实现 ...

  9. 消息框MessageBox+遍历控件

    消息对话框:主要用来显示信息,也可以警告.用户确认取消等. MessageBox.Show("展示内容","标题",MessageBoxButtons.按钮种类 ...

  10. 利用DOS命令窗口进行Mail通信(二)

    一:SMTP协议(对邮件进行发送) <SP>代表空格,<CRLF>代表回车和换行 SMTP命令格式 说明 ehlo<SP><domain><CRL ...