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

