Jersey實(shí)現(xiàn)Restful服務(wù)(實(shí)例講解)
jersey 是基于Java的一個(gè)輕量級(jí)RESTful風(fēng)格的Web Services框架。以下我基于IDEA實(shí)現(xiàn)Restful完整Demo。
1.創(chuàng)建maven-web工程,后面就是正常的maven工程創(chuàng)建流程。

2.添加Jersey框架的maven文件。
<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/maven-v4_0_0.xsd"> <modelVersion>4.0.0</modelVersion> <groupId>com.restful</groupId> <artifactId>jerseyDemo</artifactId> <packaging>war</packaging> <version>1.0-SNAPSHOT</version> <name>jerseyDemo Maven Webapp</name> <url>http://maven.apache.org</url> <dependencies> <dependency> <groupId>junit</groupId> <artifactId>junit</artifactId> <version>3.8.1</version> <scope>test</scope> </dependency> <dependency> <groupId>org.glassfish.jersey.containers</groupId> <artifactId>jersey-container-servlet</artifactId> <version>2.9</version> </dependency> <dependency> <groupId>org.glassfish.jersey.core</groupId> <artifactId>jersey-client</artifactId> <version>2.9</version> </dependency> <dependency> <groupId>org.glassfish.jersey.media</groupId> <artifactId>jersey-media-json-jackson</artifactId> <version>2.9</version> </dependency> <dependency> <groupId>com.sun.jersey</groupId> <artifactId>jersey-client</artifactId> <version>1.19.3</version> </dependency> </dependencies> <build> <finalName>jerseyDemo</finalName> </build> </project>
3.Restful接口定義。
package com.restful.service;
import com.fasterxml.jackson.core.JsonProcessingException;
import com.fasterxml.jackson.databind.ObjectMapper;
import com.restful.entity.PersonEntity;
import javax.ws.rs.*;
import javax.ws.rs.core.MediaType;
import java.io.IOException;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
/**
* Created by XuHui on 2017/8/2.
*/
@Path("/JerseyService")
public class JerseyService {
private static Map<String, PersonEntity> map = new HashMap<String, PersonEntity>();
@GET
@Path("/getAllResource")
@Produces(MediaType.APPLICATION_JSON)
public String getAllResource() throws JsonProcessingException {
List<PersonEntity> list = new ArrayList<PersonEntity>();
for (PersonEntity entity : map.values()) {
list.add(entity);
}
ObjectMapper mapper = new ObjectMapper();
return mapper.writeValueAsString(list);
}
@GET
@Path("/getResourceById/{id}")
@Produces(MediaType.APPLICATION_JSON)
public String getResource(@PathParam("id") String id) throws JsonProcessingException {
ObjectMapper mapper = new ObjectMapper();
return mapper.writeValueAsString(map.get(id));
}
@POST
@Path("/addResource/{person}")
@Consumes({MediaType.APPLICATION_XML, MediaType.APPLICATION_JSON})
@Produces(MediaType.APPLICATION_JSON)
public String addResource(String person) throws IOException {
ObjectMapper mapper = new ObjectMapper();
PersonEntity entity = mapper.readValue(person, PersonEntity.class);
map.put(entity.getId(), entity);
return mapper.writeValueAsString(entity);
}
@GET
@Path("/getRandomResource")
@Produces(MediaType.APPLICATION_JSON)
public String getRandomResource() throws JsonProcessingException {
ObjectMapper mapper = new ObjectMapper();
PersonEntity entity = new PersonEntity("NO1", "Joker", "http:///");
return mapper.writeValueAsString(entity);
}
}
PersonEntity實(shí)體類實(shí)現(xiàn)。
package com.restful.entity;
/**
* Created by XuHui on 2017/8/2.
*/
public class PersonEntity {
private String id;
private String name;
private String addr;
public PersonEntity() {
}
public PersonEntity(String id, String name, String addr) {
this.id = id;
this.name = name;
this.addr = addr;
}
public String getId() {
return id;
}
public void setId(String id) {
this.id = id;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public String getAddr() {
return addr;
}
public void setAddr(String addr) {
this.addr = addr;
}
@Override
public String toString() {
return "PersonEntity{" +
"id='" + id + '\'' +
", name='" + name + '\'' +
", addr='" + addr + '\'' +
'}';
}
}
4.web.xml配置。
<!DOCTYPE web-app PUBLIC "-//Sun Microsystems, Inc.//DTD Web Application 2.3//EN" "http://java.sun.com/dtd/web-app_2_3.dtd" > <web-app> <display-name>Archetype Created Web Application</display-name> <servlet> <servlet-name>Jersey RESTful Application</servlet-name> <servlet-class>org.glassfish.jersey.servlet.ServletContainer</servlet-class> <init-param> <param-name>jersey.config.server.provider.packages</param-name> <param-value>com.restful</param-value> </init-param> </servlet> <servlet-mapping> <servlet-name>Jersey RESTful Application</servlet-name> <url-pattern>/rest/*</url-pattern> </servlet-mapping> </web-app>
5.搭建本地tomcat

6.運(yùn)行結(jié)果、http://localhost:8080/jerseyDemo/rest/application.wadl是所有對(duì)外接口的調(diào)用方法。使用postman來看看這個(gè)接口是怎么調(diào)用的吧。
POST請(qǐng)求

GET請(qǐng)求

以上這篇Jersey實(shí)現(xiàn)Restful服務(wù)(實(shí)例講解)就是小編分享給大家的全部?jī)?nèi)容了,希望能給大家一個(gè)參考,也希望大家多多支持腳本之家。
相關(guān)文章
Java中的FilterOutputStream 簡(jiǎn)介_動(dòng)力節(jié)點(diǎn)Java學(xué)院整理
FilterOutputStream 的作用是用來“封裝其它的輸出流,并為它們提供額外的功能”。它主要包括BufferedOutputStream, DataOutputStream和PrintStream。接下來通過本文給大家簡(jiǎn)單介紹下FilterOutputStream知識(shí),需要的朋友參考下吧2017-05-05
堆排序?qū)嵗?Java數(shù)組實(shí)現(xiàn))
下面小編就為大家分享一篇使用Java數(shù)組實(shí)現(xiàn)堆排序的實(shí)例,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。一起跟隨小編過來看看吧2017-12-12
一個(gè)注解搞定Spring Security基于Oauth2的SSO單點(diǎn)登錄功能
本文主要介紹 同域 和 跨域 兩種不同場(chǎng)景單點(diǎn)登錄的實(shí)現(xiàn)原理,并使用 Spring Security 來實(shí)現(xiàn)一個(gè)最簡(jiǎn)單的跨域 SSO客戶端。對(duì)Spring Security基于Oauth2的SSO單點(diǎn)登錄功能感興趣的朋友一起看看吧2021-09-09
關(guān)于JSONObject.toJSONString出現(xiàn)地址引用問題
這篇文章主要介紹了關(guān)于JSONObject.toJSONString出現(xiàn)地址引用問題,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。如有錯(cuò)誤或未考慮完全的地方,望不吝賜教2022-03-03
spring AOP代理執(zhí)行@EnableAspectJAutoProxy的exposeProxy屬性詳解
這篇文章主要為大家介紹了spring AOP代理執(zhí)行@EnableAspectJAutoProxy的exposeProxy屬性詳解,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進(jìn)步,早日升職加薪2023-09-09
idea創(chuàng)建項(xiàng)目沒有webapp文件夾的解決方法
本文主要介紹了idea創(chuàng)建項(xiàng)目沒有webapp文件夾的解決方法,文中通過示例代碼介紹的非常詳細(xì),具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2022-05-05
淺談java面向?qū)ο?類,封裝,this,構(gòu)造方法)
下面小編就為大家?guī)硪黄獪\談java面向?qū)ο?類,封裝,this,構(gòu)造方法)。小編覺得挺不錯(cuò)的,現(xiàn)在就分享給大家,也給大家做個(gè)參考。一起跟隨小編過來看看吧2017-06-06

