SpringBoot整合jersey的示例代碼
這篇文章主要從以下幾個方面來介紹。簡單介紹下jersey,springboot,重點介紹如何整合springboot與jersey。
- 什么是jersey
- 什么是springboot
- 為什么要使用springboot+jersey
- 如何整合springboot與jersey
什么是jersey
閱讀官方文檔請點擊:jsersey。RESTful Web Services in Java即java中的一種restful框架。jersey使用了JAX-RS規(guī)范來約束API的開發(fā)。既然jersey是基于restful風(fēng)格的框架,那么什么是restful呢,主要有以下幾點:
- 在rest認(rèn)為,一切都可以被稱為資源。
- 每個資源都由uri標(biāo)識。要訪問這個資源,必須通過對應(yīng)的uri去訪問。
- 訪問資源使用POST,GET,PUT,DELETE。POST為新增接口,GET為獲取接口,PUT為修改接口,DELETE為刪除接口。
- 通過XML/JSON去通信
- 每次請求都是獨立的。
什么是springboot
簡單介紹一下,Springboot是由spring衍生的一個框架,boot是輕量的意思,即輕量級的spring。Springboot繼承了spring的特性,但是呢,覺得spring太繁瑣,于是springboot就簡化了spring的配置,不需要寫復(fù)雜的配置文件就可以實現(xiàn)spring原有的功能特點。只需要在pom.xml中引入依賴就能實現(xiàn)各種模塊和技術(shù)的整合。
為什么要使用springboot+jersey
如果要實現(xiàn)rest,jersey是一個很不錯的選擇。springboot是java中一個輕量級的框架,能簡化配置,不復(fù)雜且功能齊全,因此結(jié)合起來使用,也是一個不錯的選擇。
如何整合springboot與jersey
1.創(chuàng)建maven項目
2.添加springboot配置。
(1)在pom.xml中添加springboot父依賴
<!-- 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單元測試依賴(如不使用單元測試,可不加),引入依賴后在控制臺執(zhí)行命令 mvn clean install
<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)創(chuàng)建Springboot入口:Application.java,此時一個springboot的maven項目已經(jīng)創(chuàng)建成功,執(zhí)行main函數(shù)就可以啟動項目。(是不是確實很輕量級..?)
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中添加依賴,在控制臺執(zhí)行命令mvn install
<dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-jersey</artifactId> </dependency>
(5)創(chuàng)建jersey配置文件
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() {
//構(gòu)造函數(shù),在這里注冊需要使用的內(nèi)容,(過濾器,攔截器,API等)
}
}
此時,基于jersey的springboot項目已經(jīng)搭建成功。我們寫demo來驗證一下。
(6)基于jersey的api使用
配置文件:
創(chuàng)建項目的配置文件application.yml,指定name為local,端口號為8081,如下:
spring: name: local server: port: 8081
資源,即API,這里以get方法為例:
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配置(有兩種注冊方式,注冊類,注冊包):
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已經(jīng)完成,我們可以通過瀏覽器或其他工具訪問接口,訪問路徑:http://localhost:8081/demo/city,返回JSON字符串:{“id”:1,”cityName”:”beijing”,”cityCode”:”001”}。
項目代碼地址:https://github.com/fengqing0216/learning.git
以上就是本文的全部內(nèi)容,希望對大家的學(xué)習(xí)有所幫助,也希望大家多多支持腳本之家。
相關(guān)文章
Spring security密碼加密實現(xiàn)代碼實例
這篇文章主要介紹了Spring security密碼加密實現(xiàn)代碼實例,文中通過示例代碼介紹的非常詳細(xì),對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價值,需要的朋友可以參考下2020-04-04
Java中ThreadLocal?導(dǎo)致內(nèi)存?OOM?的原因分析
這篇文章主要介紹了Java中ThreadLocal導(dǎo)致內(nèi)存OOM的原因分析,文章基于Java的相關(guān)內(nèi)容展開ThreadLocal導(dǎo)致內(nèi)存OOM的原因分析,需要的小伙v阿布可以參考一下2022-05-05
Java SpringBoot集成ChatGPT實現(xiàn)AI聊天
ChatGPT已經(jīng)組件放開了,現(xiàn)在都可以基于它寫插件了,也許可以用它結(jié)合文字語音開發(fā)一個老人小孩需要的智能的說話陪伴啥的,這篇文章就介紹SpringBoot結(jié)合ChatGPT實現(xiàn)AI聊天感興趣的同學(xué)可以借鑒一下2023-04-04
解讀SpringBoot接收List<Bean>參數(shù)問題(POST請求方式)
這篇文章主要介紹了解讀SpringBoot接收List<Bean>參數(shù)問題(POST請求方式),具有很好的參考價值,希望對大家有所幫助。如有錯誤或未考慮完全的地方,望不吝賜教2022-09-09

