spring boot整合Jersey2.x实现JAX-RS webservice

spring boot整合Jersey2.x实现JAX-RS webservice

jersey常用注解解释:

Annotation 作用 说明
@GET 查询请求 相当于数据库的查询数据操作
@POST 插入请求 相当于数据库的插入数据操作
@PUT 更新请求 相当于数据库的更新数据操作
@DELETE 删除请求 相当于数据的删除数据操作
@Path uri路径 定义资源的访问路径,client通过这个路径访问资源。比如:@Path(“user”)
@Produces 指定返回MIME格式 资源按照那种数据格式返回,可取的值有:MediaType.APPLICATION_XXX。比如:@Produces(MediaType.APPLICATION_XML)
@Consumes 接受指定的MIME格式 只有符合这个参数设置的请求再能访问到这个资源。比如@Consumes(“application/x-www-form-urlencoded”)
@PathParam uri路径参数 写在方法的参数中,获得请求路径参数。比如:@PathParam(“username”) String userName
@QueryParam uri路径请求参数 写在方法的参数中,获得请求路径附带的参数。比如:@QueryParam(“desc”) String desc
@DefaultValue 设置@QueryParam参数的默认值 如果@QueryParam没有接收到值,就使用默认值。比如:@DefaultValue(“description”) @QueryParam(“desc”) String desc
@FormParam form传递的参数 接受form传递过来的参数。比如:@FormParam(“name”) String userName
@BeanParam 通过Bena的形式传递参数 接受client传递的bean类型的参数,同时这个bean可以在属性上配置@FormParam用以解决client的属性名称和bean的属性名称不一致的问题。比如:@BeanParam User user
@Context 获得一些系统环境信息 通过@Context可以获得以下信息:UriInfo、ServletConfig、ServletContext、HttpServletRequest、HttpServletResponse和HttpHeaders等
@XmlRootElement 将bean转换为xml 如果要讲bean以xml或json的格式返回,必须要这个注解。比如:
@XmlRootElement
public class User{…}

项目文件清单

pom.xml

<?xml version="1.0" encoding="UTF-8"?>
<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.note234</groupId>
	<artifactId>demo-webservice-jersey</artifactId>
	<version>0.0.1-SNAPSHOT</version>
	<packaging>jar</packaging>

	<name>demo-webservice-jersey</name>
	<description>demo-webservice-jersey</description>

	<parent>
		<groupId>org.springframework.boot</groupId>
		<artifactId>spring-boot-starter-parent</artifactId>
		<version>1.4.5.RELEASE</version>
		<relativePath/> <!-- lookup parent from repository -->
	</parent>

	<properties>
		<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
		<project.reporting.outputEncoding>UTF-8</project.reporting.outputEncoding>
		<java.version>1.8</java.version>
	</properties>

	<dependencies>
		<dependency>
			<groupId>org.springframework.boot</groupId>
			<artifactId>spring-boot-starter-jersey</artifactId>
		</dependency>
		<dependency>
			<groupId>org.springframework.boot</groupId>
			<artifactId>spring-boot-starter-web</artifactId>
		</dependency>

		<dependency>
			<groupId>org.springframework.boot</groupId>
			<artifactId>spring-boot-starter-test</artifactId>
			<scope>test</scope>
		</dependency>
	</dependencies>

	<build>
		<plugins>
			<plugin>
				<groupId>org.springframework.boot</groupId>
				<artifactId>spring-boot-maven-plugin</artifactId>
			</plugin>
		</plugins>
	</build>


</project>

JerseyResourceConfig.java

package com.note234.config;

import org.glassfish.jersey.server.ResourceConfig;
import org.glassfish.jersey.server.spring.scope.RequestContextFilter;

/**
 * 继承ResourceConfig,并添加一些配置信息
 * 
 * @author note234
 *
 */
public class JerseyResourceConfig extends ResourceConfig {
	public JerseyResourceConfig() {
		register(RequestContextFilter.class);
		// 配置那个包下面的会被Jersey扫描
		packages("com.note234.rest");
	}
}

JerseyConfig.java

package com.note234.config;

import org.glassfish.jersey.servlet.ServletContainer;
import org.glassfish.jersey.servlet.ServletProperties;
import org.springframework.boot.web.servlet.ServletRegistrationBean;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;

/**
 * 配置Jersey
 * 
 * @author note234
 *
 */
@Configuration
public class JerseyConfig {

	@Bean
	public ServletRegistrationBean jerseyServlet() {
		ServletRegistrationBean registration = new ServletRegistrationBean(new ServletContainer(), "/rest/*");
		// our rest resources will be available in the path /rest/*
		registration.addInitParameter(ServletProperties.JAXRS_APPLICATION_CLASS, JerseyResourceConfig.class.getName());
		return registration;
	}
}

RestResource.java(测试资源类)

package com.note234.rest;

import java.util.HashMap;
import java.util.Map;

import javax.ws.rs.GET;
import javax.ws.rs.Path;
import javax.ws.rs.Produces;
import javax.ws.rs.core.MediaType;

@Path("/")
public class RestResource {

	@Path("/hello") // 具体路径
	@GET // 请求方式
	@Produces(MediaType.APPLICATION_JSON) // 返回的格式
	// @Consumes()//接受指定的MIME格式
	public Map<String, Object> hello() {
		Map<String, Object> map = new HashMap<String, Object>();
		map.put("code", "1");
		map.put("codeMsg", "success");
		return map;
	}
}

Application.java

package com.note234;

import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;

@SpringBootApplication
public class Application {

	public static void main(String[] args) {
		SpringApplication.run(Application.class, args);
	}

}

启动项目,访问地址:http://127.0.0.1:8080/rest/hello

显示内容:

{"code":"1","codeMsg":"success"}

项目下载:

GITHUB:https://github.com/note234/demo-webservice-jersey

 

spring boot整合Jersey2.x实现JAX-RS webservice

相关文章:

你感兴趣的文章:

标签云: