1. pom 文件
<dependencies>
<!-- 引入spring-boot父依賴,相當(dāng)于parent浩蓉,適合已有父模塊的開發(fā)項(xiàng)目 -->
<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>
上述依賴作用同下怖竭,當(dāng)項(xiàng)目有自己的父項(xiàng)目毙替,可選擇第一種方式。
<parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>1.5.1.RELEASE</version>
</parent>
2. Spring 啟動(dòng)類
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 應(yīng)用啟動(dòng)類
* \
*/
@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的基礎(chǔ)路徑
public class JerseyConfig extends ResourceConfig {
public JerseyConfig() {
// 注冊類的方式
// egister(Demo.class);
// 注冊包的方式
packages("com.xcar.hbase.rest.resources");
// 注冊JSON轉(zhuǎn)換器
register(JacksonJsonProvider.class);
//注冊文件上傳模塊
register(MultiPartFeature.class);
}
}
4. Spring-boot 配置文件 application.yml
spring:
http:
# 指定 Jersey 中文編碼
encoding:
force: true
charset: utf-8
server:
# 指定服務(wù)端口
port: 1080
session-timeout: 1800
5. Demo 開發(fā)測試
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 是否啟動(dòng)成功
* \
*/
@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 是否啟動(dòng)成功
* \
*/
@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
訪問如下:
總結(jié):
看似 Spring-MVC 開發(fā)更似簡潔,但真正開發(fā)過程中纸淮,你會(huì)發(fā)現(xiàn)開發(fā)Rest服務(wù)平斩,還是Jersey更具靈活性。因?yàn)檎y(tǒng)萎馅,所以選擇双戳!