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)文章
SpringBoot3整合Nacos?V2.3.2的詳細(xì)過(guò)程
本文介紹了如何在?Spring?Boot?3.2.x?項(xiàng)目中整合?Nacos?2.3.2,包括依賴配置、Nacos?服務(wù)發(fā)現(xiàn)與動(dòng)態(tài)配置的配置方法,通過(guò)整合?Nacos,Spring?Boot?應(yīng)用可以實(shí)現(xiàn)高效的服務(wù)發(fā)現(xiàn)、動(dòng)態(tài)配置管理以及分布式系統(tǒng)中的靈活擴(kuò)展,感興趣的朋友跟隨小編一起看看吧2024-11-11
啟動(dòng) Eclipse 彈出 Failed to load the JNI shared library jvm.dll
這篇文章主要介紹了有時(shí)候,新電腦上回碰到打開(kāi)Eclipse時(shí),彈出提示“Failed to load the JNI shared library jvm.dll”錯(cuò)誤,這里給大家分享解決方案2016-08-08
Mybatis不啟動(dòng)項(xiàng)目直接測(cè)試Mapper的實(shí)現(xiàn)方法
在項(xiàng)目開(kāi)發(fā)中,測(cè)試單個(gè)Mybatis Mapper方法通常需要啟動(dòng)整個(gè)SpringBoot項(xiàng)目,消耗大量時(shí)間,本文介紹通過(guò)Main方法和Mybatis配置類,快速測(cè)試Mapper功能,無(wú)需啟動(dòng)整個(gè)項(xiàng)目,這方法使用AnnotationConfigApplicationContext容器2024-09-09
Spring的@Scheduled 如何動(dòng)態(tài)更新cron表達(dá)式
這篇文章主要介紹了Spring的@Scheduled 如何動(dòng)態(tài)更新cron表達(dá)式的操作,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。如有錯(cuò)誤或未考慮完全的地方,望不吝賜教2021-07-07
RestTemplate設(shè)置超時(shí)時(shí)間及返回狀態(tài)碼非200處理
這篇文章主要為大家介紹了RestTemplate設(shè)置超時(shí)時(shí)間及返回狀態(tài)碼非200處理,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進(jìn)步,早日升職加薪2022-06-06
java通過(guò)cglib動(dòng)態(tài)生成實(shí)體bean的操作
這篇文章主要介紹了java通過(guò)cglib動(dòng)態(tài)生成實(shí)體bean的操作,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。一起跟隨小編過(guò)來(lái)看看吧2021-02-02

