這篇文章主要從以下幾個方面來介紹。簡單介紹下jersey,springboot,重點介紹如何整合springboot與jersey。
- 什么是jersey
- 什么是springboot
- 為什么要使用springboot+jersey
- 如何整合springboot與jersey
什么是jersey
閱讀官方文檔請點擊:jsersey。RESTful Web Services in Java即java中的一種restful框架。jersey使用了JAX-RS規范來約束API的開發。既然jersey是基于restful風格的框架,那么什么是restful呢,主要有以下幾點:
- 在rest認為,一切都可以被稱為資源。
- 每個資源都由uri標識。要訪問這個資源,必須通過對應的uri去訪問。
- 訪問資源使用POST,GET,PUT,DELETE。POST為新增接口,GET為獲取接口,PUT為修改接口,DELETE為刪除接口。
- 通過XML/JSON去通信
- 每次請求都是獨立的。
什么是springboot
簡單介紹一下,Springboot是由spring衍生的一個框架,boot是輕量的意思,即輕量級的spring。Springboot繼承了spring的特性,但是呢,覺得spring太繁瑣,于是springboot就簡化了spring的配置,不需要寫復雜的配置文件就可以實現spring原有的功能特點。只需要在pom.xml中引入依賴就能實現各種模塊和技術的整合。
為什么要使用springboot+jersey
如果要實現rest,jersey是一個很不錯的選擇。springboot是java中一個輕量級的框架,能簡化配置,不復雜且功能齊全,因此結合起來使用,也是一個不錯的選擇。
如何整合springboot與jersey
1.創建maven項目
2.添加springboot配置。
(1)在pom.xml中添加springboot父依賴
1
2
3
4
5
6
|
<!-- Spring Boot 父依賴 --> < parent > < groupId >org.springframework.boot</ groupId > < artifactId >spring-boot-starter-parent</ artifactId > < version >1.5.1.RELEASE</ version > </ parent > |
(2)在pom.xml中添加springbootweb依賴和junit單元測試依賴(如不使用單元測試,可不加),引入依賴后在控制臺執行命令 mvn clean install
1
2
3
4
5
6
7
8
9
10
11
12
13
14
|
< dependencies > <!-- Spring Boot web依賴 --> < dependency > < groupId >org.springframework.boot</ groupId > < artifactId >spring-boot-starter-web</ artifactId > </ dependency > <!-- Junit --> < dependency > < groupId >junit</ groupId > < artifactId >junit</ artifactId > < version >4.12</ version > </ dependency > </ dependencies > |
(3)創建Springboot入口:Application.java,此時一個springboot的maven項目已經創建成功,執行main函數就可以啟動項目。(是不是確實很輕量級..?)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
|
package com.demo; import org.springframework.boot.SpringApplication; import org.springframework.boot.autoconfigure.SpringBootApplication; /** * Created by Angela on 2017/4/20. */ @SpringBootApplication public class Application { public static void main(String[] args){ //springboot 入口 SpringApplication.run(Application. class ,args); } } |
(4)添加jersey依賴,在pom.xml中添加依賴,在控制臺執行命令mvn install
1
2
3
4
|
< dependency > < groupId >org.springframework.boot</ groupId > < artifactId >spring-boot-starter-jersey</ artifactId > </ dependency > |
(5)創建jersey配置文件
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
|
package com.demo.config.jersey; import org.glassfish.jersey.server.ResourceConfig; import org.springframework.stereotype.Component; /** * Created by Angela on 2017/4/20. */ @Component public class JerseyConfig extends ResourceConfig { public JerseyConfig() { //構造函數,在這里注冊需要使用的內容,(過濾器,攔截器,API等) } } |
此時,基于jersey的springboot項目已經搭建成功。我們寫demo來驗證一下。
(6)基于jersey的api使用
配置文件:
創建項目的配置文件application.yml,指定name為local,端口號為8081,如下:
1
2
3
4
|
spring: name: local server: port: 8081 |
資源,即API,這里以get方法為例:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
|
package com.demo.web; import com.demo.model.City; import org.springframework.stereotype.Component; import javax.ws.rs.GET; import javax.ws.rs.Path; import javax.ws.rs.Produces; import javax.ws.rs.core.MediaType; /** * Created by Angela on 2017/4/20. */ @Component @Path ( "/demo" ) public class Demo { //path注解指定路徑,get注解指定訪問方式,produces注解指定了返回值類型,這里返回JSON @Path ( "/city" ) @GET @Produces (MediaType.APPLICATION_JSON) public City get(){ City city = new City(); city.setId(1L); city.setCityName( "beijing" ); city.setCityCode( "001" ); System.out.println(city.toString()); return city; } } |
jersey配置(有兩種注冊方式,注冊類,注冊包):
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
|
package com.demo.config.jersey; import com.demo.web.Demo; import org.glassfish.jersey.server.ResourceConfig; import org.springframework.stereotype.Component; /** * Created by Angela on 2017/4/20. */ @Component public class JerseyConfig extends ResourceConfig { public JerseyConfig() { //注冊類的方式 // register(Demo.class); //注冊包的方式 packages( "com.demo.web" ); } } |
這里有個小坑。項目打為jar包啟動時,不能使用包注冊的方式,否則會報FileNotFound異常。
此時,demo已經完成,我們可以通過瀏覽器或其他工具訪問接口,訪問路徑:http://localhost:8081/demo/city,返回JSON字符串:{“id”:1,”cityName”:”beijing”,”cityCode”:”001”}。
項目代碼地址:https://github.com/fengqing0216/learning.git
以上就是本文的全部內容,希望對大家的學習有所幫助,也希望大家多多支持服務器之家。
原文鏈接:http://blog.csdn.net/github_38395241/article/details/70265379