jersey+maven构建restful服务

时间:2021-11-20 08:37:04

一、新建一个Maven Web项目

a) 新建一个简单的Maven项目

jersey+maven构建restful服务

jersey+maven构建restful服务

jersey+maven构建restful服务

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

jersey+maven构建restful服务

jersey+maven构建restful服务

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

jersey+maven构建restful服务

二、修改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;
	}

}

发布服务:

jersey+maven构建restful服务

测试服务:

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+maven构建restful服务

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