Jersey框架简单实践(一)
目錄
- 一、RESTful
- 二、Jersey框架
一、RESTful
REST(Representational State Transfer 表現(xiàn)層狀態(tài)轉(zhuǎn)化) 是一組架構(gòu)約束,而不是協(xié)議或標(biāo)準(zhǔn)。通常定義:
- 每一個URI代表一種資源;
- 客戶端和服務(wù)器之間,傳遞這種資源的某種表現(xiàn)層;
- 客戶端通過四個HTTP動詞,對服務(wù)器端資源進行操作,實現(xiàn)"表現(xiàn)層狀態(tài)轉(zhuǎn)化"。
一般實現(xiàn)了RESTful的架構(gòu)更結(jié)構(gòu)清晰、符合標(biāo)準(zhǔn)、易于理解、擴展方便,所以正得到越來越多網(wǎng)站的采用。
二、Jersey框架
Jersey是一個RESTful服務(wù)JAVA框架,與常規(guī)的JAVA編程使用的Struts框架類似,它主要用于處理業(yè)務(wù)邏輯層。Jersey是一個是 webservice框架。
通過簡單的配置,即可打造極簡主義的REST接口。其實就是可以把我們的服務(wù)層開放API供前端調(diào)用。
與SpringMVC的區(qū)別:
Jersey同樣提供DI,是由glassfish hk2實現(xiàn),也就是說,如果想單獨使用Jersey一套,需要另外學(xué)習(xí)Bean容器;
MVC出發(fā)點是WEB,但Jersey出發(fā)點是RESTful,體現(xiàn)點在與接口的設(shè)計方面,如MVC返回復(fù)雜結(jié)構(gòu)需要使用ModelAndView,而Jersey僅僅需要返回一個流或者文件句柄;
Jersey提供一種子資源的概念,這也是RESTful中提倡所有url都是資源;
Jersey直接提供application.wadl資源url說明;
MVC提供Session等狀態(tài)管理,jersey沒有,這個源自RESTFull設(shè)計無狀態(tài)化;
Response方法支持更好返回結(jié)果,方便的返回Status,包括200,303,401,403;
提供超級特別方便的方式訪問RESTful;
以下是demo案例:
maven引用:
<?xml version="1.0" encoding="UTF-8"?> <project xmlns="http://maven.apache.org/POM/4.0.0"xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd"><parent><groupId>org.springframework.boot</groupId><artifactId>spring-boot-starter-parent</artifactId><version>2.3.4.RELEASE</version></parent><modelVersion>4.0.0</modelVersion><artifactId>jersey</artifactId><dependencies><dependency><groupId>org.springframework.boot</groupId><artifactId>spring-boot-starter-jersey</artifactId></dependency><dependency><groupId>org.springframework.boot</groupId><artifactId>spring-boot-starter-web</artifactId></dependency></dependencies> </project>Jersey配置,需要被Spring掃描到:
1、自定義資源配置類繼承類ResourceConfig,通過register方法注冊自己的資源。
2、在bean中新建資源配置類ResourceConfig,并返回此config。
注冊我們的服務(wù)的時候有兩種方式,一種是直接將類注冊進去,一種是通過掃描包,建議是使用注冊類的方式,比較好控制。如果服務(wù)非常多,使用包掃描的方式也可,自行定奪。
package org.self.zhlxf.config;import org.glassfish.jersey.jackson.internal.jackson.jaxrs.json.JacksonJsonProvider; import org.glassfish.jersey.server.ResourceConfig; import org.self.zhlxf.service.impl.UserService; import org.springframework.context.annotation.Configuration;import javax.ws.rs.ApplicationPath;@Configuration @ApplicationPath("/") // 這里指定了路由的根路徑,可自行配置,習(xí)慣用/ public class JerseyConfig extends ResourceConfig {public JerseyConfig() {// 注冊json解析register(JacksonJsonProvider.class);// 通過類名單個注冊register(UserService.class);// 通過掃描包注冊包里面的所有服務(wù)// packages("org.self.zhlxf.service");} }編寫統(tǒng)一的返回對象:
package org.self.zhlxf.pojo;public class ApiResponseBody<T> {private Integer code;private T data;private String message;public ApiResponseBody() {}public ApiResponseBody(Integer code, T data, String message) {this.code = code;this.data = data;this.message = message;}public Integer getCode() {return code;}public void setCode(Integer code) {this.code = code;}public T getData() {return data;}public void setData(T data) {this.data = data;}public String getMessage() {return message;}public void setMessage(String message) {this.message = message;}@Overridepublic String toString() {return "ApiResponseBody{" +"code=" + code +", data=" + data +", message='" + message + '\'' +'}';} }編寫服務(wù)類:
package org.self.zhlxf.service;import org.self.zhlxf.pojo.ApiResponseBody;public interface IUserService {ApiResponseBody allUser();String getUserById(); } package org.self.zhlxf.service.impl;import org.jvnet.hk2.annotations.Service; import org.self.zhlxf.pojo.ApiResponseBody; import org.self.zhlxf.pojo.User; import org.self.zhlxf.service.IUserService;import javax.ws.rs.GET; import javax.ws.rs.Path; import javax.ws.rs.Produces; import javax.ws.rs.core.MediaType; import java.util.ArrayList; import java.util.List;@Service @Path("/user") public class UserService implements IUserService {@GET@Override@Produces(MediaType.APPLICATION_JSON + ";charset=utf-8")public ApiResponseBody allUser() {List<User> ant = new ArrayList<>();for (int i = 0; i < 10; i++) {ant.add(new User("小明" + i + "號", 5 + i));}return new ApiResponseBody(200, ant, "success");}@GET@Path("/{id}")@Produces(MediaType.APPLICATION_JSON + ";charset=utf-8")@Overridepublic String getUserById() {return "小明";} }瀏覽器調(diào)用:localhost:8080/user
至此,一個基于jersey框架的RESTful簡單demo就完畢了,更多的服務(wù)自行注冊即可。
總結(jié)
以上是生活随笔為你收集整理的Jersey框架简单实践(一)的全部內(nèi)容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: 后端开发:数据持久化框架为什么放弃Hib
- 下一篇: 磁盘阵列服务器Intel C610系列,