SpringBoot + Jersey 整合开发Restful
2018-06-14 本文已影响2人
步闲
1. pom 文件
<dependencies>
<!-- 引入spring-boot父依赖,相当于parent,适合已有父模块的开发项目 -->
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-dependencies</artifactId>
<version>1.5.8.RELEASE</version>
<type>pom</type>
<scope>import</scope>
</dependency>
<!-- Web 依赖 -->
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
<version>1.5.8.RELEASE</version>
</dependency>
<!-- Jersey 依赖 -->
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-jersey</artifactId>
<version>1.5.8.RELEASE</version>
</dependency>
<dependency>
<groupId>org.glassfish.jersey.media</groupId>
<artifactId>jersey-media-multipart</artifactId>
<version>2.25.1</version>
</dependency>
<!-- 测试依赖 -->
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-test</artifactId>
<version>1.5.8.RELEASE</version>
<scope>test</scope>
</dependency>
</dependencies>
说明:
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-dependencies</artifactId>
<version>1.5.8.RELEASE</version>
<type>pom</type>
<scope>import</scope>
</dependency>
上述依赖作用同下,当项目有自己的父项目,可选择第一种方式。
<parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>1.5.1.RELEASE</version>
</parent>
2. Spring 启动类
package com.xcar.hbase.rest;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
/**
* \* Created with IntelliJ IDEA.
* \* User: zhou.pengbo
* \* Date: 2018/6/13
* \* Time: 18:13
* \* To change this template use File | Settings | File Templates.
* \* Description: Spring Boot 应用启动类
* \
*/
@SpringBootApplication
public class RestApplication {
public static void main(String[] args) {
SpringApplication.run(RestApplication.class, args);
}
}
3. Jersey 配置类
package com.xcar.hbase.rest;
import com.fasterxml.jackson.jaxrs.json.JacksonJsonProvider;
import org.glassfish.jersey.media.multipart.MultiPartFeature;
import org.glassfish.jersey.server.ResourceConfig;
import org.springframework.stereotype.Component;
import javax.ws.rs.ApplicationPath;
/**
* \* Created with IntelliJ IDEA.
* \* User: zhou.pengbo
* \* Date: 2018/6/14
* \* Time: 11:01
* \* To change this template use File | Settings | File Templates.
* \* Description:
* \
*/
@Component
@ApplicationPath("/rest/api/v1") // 指定所有Endpoint的基础路径
public class JerseyConfig extends ResourceConfig {
public JerseyConfig() {
// 注册类的方式
// egister(Demo.class);
// 注册包的方式
packages("com.xcar.hbase.rest.resources");
// 注册JSON转换器
register(JacksonJsonProvider.class);
//注册文件上传模块
register(MultiPartFeature.class);
}
}
4. Spring-boot 配置文件 application.yml
spring:
http:
# 指定 Jersey 中文编码
encoding:
force: true
charset: utf-8
server:
# 指定服务端口
port: 1080
session-timeout: 1800
5. Demo 开发测试
package com.xcar.hbase.rest.resources;
import com.xcar.hbase.core.service.impl.HbaseServiceImpl;
import org.springframework.stereotype.Component;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;
import javax.inject.Inject;
import javax.ws.rs.*;
import javax.ws.rs.core.MediaType;
/**
* \* Created with IntelliJ IDEA.
* \* User: zhou.pengbo
* \* Date: 2018/6/13
* \* Time: 18:12
* \* To change this template use File | Settings | File Templates.
* \* Description: 测试一下SpringBoot 是否启动成功
* \
*/
@Component
@Path("/kv")
public class HelloResource {
@GET @Path("/hello")
@Produces({MediaType.TEXT_HTML,MediaType.APPLICATION_JSON})
public String sayHello() {
String value = HbaseServiceImpl.getValue();
return "Hello "+value+" Sir,Welcome to Spring Boot ! 欢迎光临!";
}
}
访问地址:http://localhost:1080/rest/api/v1/kv/hello
访问如下:
与spring-mvc对比:
package com.xcar.hbase.rest.resources;
import com.xcar.hbase.core.service.impl.HbaseServiceImpl;
import org.springframework.stereotype.Component;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;
import javax.inject.Inject;
import javax.ws.rs.*;
import javax.ws.rs.core.MediaType;
/**
* \* Created with IntelliJ IDEA.
* \* User: zhou.pengbo
* \* Date: 2018/6/13
* \* Time: 18:12
* \* To change this template use File | Settings | File Templates.
* \* Description: 测试一下SpringBoot 是否启动成功
* \
*/
@Controller
public class HelloResource {
@RequestMapping(value = "/hello",method = RequestMethod.GET)
@ResponseBody
public String sayHello() {
String value = HbaseServiceImpl.getValue();
return "Hello "+value+" Sir,Welcome to Spring Boot!欢迎光临 ! ";
}
}
访问地址:localhost:1080/hello
访问如下:
总结:
看似 Spring-MVC 开发更似简洁,但真正开发过程中,你会发现开发Rest服务,还是Jersey更具灵活性。因为正统,所以选择!