看到群里有人在问如何集成jersey,博主在别的项目也用过jersey,在这里就给大家简单介绍下springboot中怎么使用jersey。本章大纲如下:

1)新建工程spring-boot-jersey;

2)在pom.xml中添加相关依赖;

3)编写启动文件和注册ServletContainer

4)编写Jerseyconfig文件;

5)编写一个测试文件;

6)运行测试;

 

1)新建工程spring-boot-jersey;

       新建一个工程,取名为spring-boot-jersey

2)在pom.xml中添加相关依赖;

       pom.xml文件中添加相关的依赖,主要是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.0http://maven.apache.org/xsd/maven-4.0.0.xsd">

  <modelVersion>4.0.0</modelVersion>

 

  <groupId>com.kfit</groupId>

  <artifactId>spring-boot-jersey</artifactId>

  <version>0.0.1-SNAPSHOT</version>

  <packaging>jar</packaging>

 

  <name>spring-boot-jersey</name>

  <url>http://maven.apache.org</url>

 

  <properties>

    <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>

    <!-- jdk版本号,Angel在这里使用1.8,大家修改为大家本地配置的jdk版本号即可 -->

    <java.version>1.8</java.version>

  </properties>

 

 

   <!--

       spring boot 父节点依赖,

       引入这个之后相关的引入就不需要添加version配置,

       spring boot会自动选择最合适的版本进行添加。

     -->

    <parent>

       <groupId>org.springframework.boot</groupId>

       <artifactId>spring-boot-starter-parent</artifactId>

       <version>1.4.0.RELEASE</version>

    </parent>

 

  <dependencies>

    <dependency>

      <groupId>junit</groupId>

      <artifactId>junit</artifactId>

      <scope>test</scope>

    </dependency>

   

    <!-- jersey -->

    <dependency>

       <groupId>org.springframework.boot</groupId>

       <artifactId>spring-boot-starter-jersey</artifactId>

    </dependency>

   

  </dependencies>

</project>

 

3)编写启动文件和注册ServletContainer

       编写一个App.java启动文件,在此代码中使用ServletRegistrationBean注册ServletContainer,具体代码如下:

package com.kfit;

 

importorg.glassfish.jersey.servlet.ServletContainer;

importorg.glassfish.jersey.servlet.ServletProperties;

import org.springframework.boot.SpringApplication;

importorg.springframework.boot.autoconfigure.SpringBootApplication;

importorg.springframework.boot.web.servlet.ServletRegistrationBean;

import org.springframework.context.annotation.Bean;

import com.kfit.config.jersey.JerseyConfig;

 

/**

 *

 * @author Angel --守护天使

 * @version v.0.1

 * @date 2016116

 */

@SpringBootApplication

public class App {

   

    @Bean

    public ServletRegistrationBeanjerseyServlet() {

      ServletRegistrationBean registration = newServletRegistrationBean(newServletContainer(),"/rest/*");

       // our rest resources will be available in the path /rest/*

      registration.addInitParameter(ServletProperties.JAXRS_APPLICATION_CLASS, JerseyConfig.class.getName());

       return registration;

    }

   

    public static void main(String[] args) {

       SpringApplication.run(App.classargs);

    }

}

 

4)编写Jerseyconfig文件;

       编写jersey的配置文件,主要是指定扫描的包packages("com.kfit.rest");具体代码如下:

package com.kfit.config.jersey;

 

import org.glassfish.jersey.server.ResourceConfig;

importorg.glassfish.jersey.server.spring.scope.RequestContextFilter;

 

public class JerseyConfig extends ResourceConfig {

    public JerseyConfig() {

       register(RequestContextFilter.class);

       //配置restfulpackage.

       packages("com.kfit.rest");

    }

}

 

5)编写一个测试文件;

       编写一个测试文件,这个有jersey基础的就很简单了,使用@Path进行指定访问路径,使用@GET标注是get请求,使用@Produces指定的返回的数据类型。具体代码如下:

package com.kfit.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;

 

import org.springframework.stereotype.Component;

 

@Path("/")

@Component

public class RestResource {

    @GET

    @Produces(MediaType.APPLICATION_JSON)

    @Path("/hello")

    public Map<String,Object>hello() {

       Map<String,Object> map = newHashMap<String,Object>();

       map.put("code","1");

       map.put("codeMsg""success");

       return map;

    }

}

 

6)运行测试;

       剩下的就是运行App.java进行测试了,访问测试地址:

http://127.0.0.1:8080/rest/hello ,好了这就是最基本的一个例子,jersey还是有很多地方需要研究的,剩下的就靠你自己了。