使用SpringBoot整合Jersey 实现Restful webservice.可以同时使用springmvc。

  • Post author:
  • Post category:其他



1、pow依赖

<?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>cn.edu.jxnu</groupId>

<artifactId>work_2</artifactId>

<version>0.0.1-SNAPSHOT</version>

<packaging>jar</packaging>



<name>work_2</name>

<description>Spring Boot Demo</description>



<parent>

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

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

<version>1.5.9.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.7</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>


2、application.properties 几乎不用设置

server.port=8080



3、测试实体类

package cn.edu.jxnu.system.jersey;



/**

 * <p>

 * Title: City.java</p>

 * <p>

 * Description: </p>

 * <p>

 * Copyright: Copyright (c) 2018</p>

 * <p>

 * School: jxnu</p>

 * 

 * @author Mr.Li

 * @date 2018-2-4

 * @version 1.0

 */

public class City {

private Long id;

private String name;

private String code;



public Long getId() {

return id;

}



public void setId(Long id) {

this.id = id;

}



public String getName() {

return name;

}



public void setName(String name) {

this.name = name;

}



public String getCode() {

return code;

}



public void setCode(String code) {

this.code = code;

}



@Override

public String toString() {

return "City [id=" + id + ", name=" + name + ", code=" + code

+ ", getId()=" + getId() + ", getName()=" + getName()

+ ", getCode()=" + getCode() + ", getClass()=" + getClass()

+ ", hashCode()=" + hashCode() + ", toString()="

+ super.toString() + "]";

}



}


4、jersey配置

package cn.edu.jxnu.system.jersey;



import javax.ws.rs.ApplicationPath;



import org.glassfish.jersey.server.ResourceConfig;



/**

 * @时间 2018年2月4日15:30:16

 * @author Mr.Li

 * 

 */

@ApplicationPath("/restful")  顶级uri 即基本路径

public class JerseyConfig extends ResourceConfig {



public JerseyConfig() {

// 构造函数,在这里注册需要使用的内容,(过滤器,拦截器,API等)

// 注册类的方式

// register(Demo.class);

// register(RequestContextFilter.class);

// 注册包的方式

packages("cn.edu.jxnu.system.jersey");

}

}


5、jersey controller类似spring的controller

package cn.edu.jxnu.system.jersey;



import javax.ws.rs.GET;

import javax.ws.rs.Path;

import javax.ws.rs.Produces;

import javax.ws.rs.core.MediaType;

//这里jersey的bean是由自带容器管理,需要使用spring容器加@Component 测试了都可以

@Path("/jersey")

public class JerseyController {



// path注解指定路径,get注解指定访问方式,produces注解指定了返回值类型,这里返回JSON

@Path("/city")

@GET

@Produces(MediaType.APPLICATION_JSON)

public City get() {

City city = new City();

city.setId(10000L);

city.setName("beijing");

city.setCode("001");

System.out.println(city.toString());

return city;

}



}


6、主启动配置类

package cn.edu.jxnu.system;



import org.glassfish.jersey.servlet.ServletContainer;

import org.glassfish.jersey.servlet.ServletProperties;

import org.springframework.boot.SpringApplication;

import org.springframework.boot.autoconfigure.SpringBootApplication;

import org.springframework.boot.web.servlet.ServletRegistrationBean;

import org.springframework.context.annotation.Bean;



import cn.edu.jxnu.system.jersey.JerseyConfig;



/**

 * @note 如果整合的时候自己加了数据库驱动pow,则由于自动配置,但是却没有使用数据库就会报错,解决办法是使用exclude

 *       排除数据库的自动配置,或者去除pow依赖

 * @author Mr.Li

 * 

 */

@SpringBootApplication

public class Application {

public static void main(String[] args) {

SpringApplication.run(Application.class, args);

}



@Bean

public ServletRegistrationBean jersetServlet() {

ServletRegistrationBean registration = new ServletRegistrationBean(

// 顶级uri 用来区分SpringMVC

new ServletContainer(), "/restful/*");

registration.addInitParameter(

ServletProperties.JAXRS_APPLICATION_CLASS,

JerseyConfig.class.getName());

return registration;

}

}


7、springmvc 测试controller【这个只是测试springboot中是可以同时使用jersey和springmvc的】

package cn.edu.jxnu.system.jersey;



import org.springframework.web.bind.annotation.RequestMapping;

import org.springframework.web.bind.annotation.RestController;



/**

 * <p>

 * Title: SpringController.java</p>

 * <p>

 * Description: </p>

 * <p>

 * Copyright: Copyright (c) 2018</p>

 * <p>

 * School: jxnu</p>

 * 

 * @author Mr.Li

 * @date 2018-2-4

 * @version 1.0

 */

@RestController

@RequestMapping("/spring")

public class SpringController {


@RequestMapping("/city")

public City getCity() {


City city = new City();

city.setId(200000L);

city.setName("nanchang");

city.setCode("030");

System.out.println(city.toString());


return city;

}

}

由于前面jersey的restful url顶级前缀是restful,所以后面访问都需要加restful


如jersey:http://localhost:8080/restful/jersey/city

测试结果:
201802041716b1-6002beac5ec9.png


注:后台springmvc默认由DispatcherServlet处理并且在第一次处理的时候才会初始化DispatcherServlet 这个在日志中,


而restful开头的url才会被jersey的bean拦截


spring还是一样的:http://localhost:8080/spring/city


201802041719fc-08391fa17410.png


使用springboot,可使用springmvc构建restful,也可以使用jersey替代实现restful

两者可共存。如果springmvc顶级前缀是restful,这样不能实现共存,因为优先被jersey拦截,但是却不是jersey资源,将出现404

这里说的顶级前缀的去除ip:port后的最前url

demo地址:http://download.csdn.net/download/qq_34446485/10239386



版权声明:本文为qq_34446485原创文章,遵循 CC 4.0 BY-SA 版权协议,转载请附上原文出处链接和本声明。