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風格的框架,那么什么是restful呢,主要有以下幾點:
- 在rest認為,一切都可以被稱為資源。
- 每個資源都由uri標識。要訪問這個資源,必須通過對應的uri去訪問。
- 訪問資源使用POST,GET,PUT,DELETE。POST為新增接口,GET為獲取接口,PUT為修改接口,DELETE為刪除接口。
- 通過XML/JSON去通信
- 每次請求都是獨立的。
什么是springboot
簡單介紹一下,Springboot是由spring衍生的一個框架,boot是輕量的意思,即輕量級的spring。Springboot繼承了spring的特性,但是呢,覺得spring太繁瑣,于是springboot就簡化了spring的配置,不需要寫復雜的配置文件就可以實現(xiàn)spring原有的功能特點。只需要在pom.xml中引入依賴就能實現(xiàn)各種模塊和技術的整合。
為什么要使用springboot+jersey
如果要實現(xiàn)rest,jersey是一個很不錯的選擇。springboot是java中一個輕量級的框架,能簡化配置,不復雜且功能齊全,因此結合起來使用,也是一個不錯的選擇。
如何整合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() {
//構造函數(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)容,希望對大家的學習有所幫助,也希望大家多多支持腳本之家。
相關文章
SpringBoot3整合Nacos?V2.3.2的詳細過程
本文介紹了如何在?Spring?Boot?3.2.x?項目中整合?Nacos?2.3.2,包括依賴配置、Nacos?服務發(fā)現(xiàn)與動態(tài)配置的配置方法,通過整合?Nacos,Spring?Boot?應用可以實現(xiàn)高效的服務發(fā)現(xiàn)、動態(tài)配置管理以及分布式系統(tǒng)中的靈活擴展,感興趣的朋友跟隨小編一起看看吧2024-11-11
啟動 Eclipse 彈出 Failed to load the JNI shared library jvm.dll
這篇文章主要介紹了有時候,新電腦上回碰到打開Eclipse時,彈出提示“Failed to load the JNI shared library jvm.dll”錯誤,這里給大家分享解決方案2016-08-08
Mybatis不啟動項目直接測試Mapper的實現(xiàn)方法
在項目開發(fā)中,測試單個Mybatis Mapper方法通常需要啟動整個SpringBoot項目,消耗大量時間,本文介紹通過Main方法和Mybatis配置類,快速測試Mapper功能,無需啟動整個項目,這方法使用AnnotationConfigApplicationContext容器2024-09-09
Spring的@Scheduled 如何動態(tài)更新cron表達式
這篇文章主要介紹了Spring的@Scheduled 如何動態(tài)更新cron表達式的操作,具有很好的參考價值,希望對大家有所幫助。如有錯誤或未考慮完全的地方,望不吝賜教2021-07-07
RestTemplate設置超時時間及返回狀態(tài)碼非200處理
這篇文章主要為大家介紹了RestTemplate設置超時時間及返回狀態(tài)碼非200處理,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進步,早日升職加薪2022-06-06

