一区二区三区在线-一区二区三区亚洲视频-一区二区三区亚洲-一区二区三区午夜-一区二区三区四区在线视频-一区二区三区四区在线免费观看

服務(wù)器之家:專注于服務(wù)器技術(shù)及軟件下載分享
分類導(dǎo)航

PHP教程|ASP.NET教程|Java教程|ASP教程|編程技術(shù)|正則表達(dá)式|C/C++|IOS|C#|Swift|Android|VB|R語言|JavaScript|易語言|vb.net|

服務(wù)器之家 - 編程語言 - Java教程 - Jersey實(shí)現(xiàn)Restful服務(wù)(實(shí)例講解)

Jersey實(shí)現(xiàn)Restful服務(wù)(實(shí)例講解)

2020-12-12 16:03Java教程網(wǎng) Java教程

下面小編就為大家?guī)硪黄狫ersey實(shí)現(xiàn)Restful服務(wù)(實(shí)例講解)。小編覺得挺不錯(cuò)的,現(xiàn)在就分享給大家,也給大家做個(gè)參考。一起跟隨小編過來看看吧

jersey 是基于java的一個(gè)輕量級(jí)restful風(fēng)格的web services框架。以下我基于idea實(shí)現(xiàn)restful完整demo。

1.創(chuàng)建maven-web工程,后面就是正常的maven工程創(chuàng)建流程。

Jersey實(shí)現(xiàn)Restful服務(wù)(實(shí)例講解)

2.添加jersey框架的maven文件。

 
?
1
 
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
<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接口定義。

 
?
1
 
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
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)。

 
?
1
 
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
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配置。

 
?
1
 
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
<!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

Jersey實(shí)現(xiàn)Restful服務(wù)(實(shí)例講解)

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

post請(qǐng)求

Jersey實(shí)現(xiàn)Restful服務(wù)(實(shí)例講解)

get請(qǐng)求

Jersey實(shí)現(xiàn)Restful服務(wù)(實(shí)例講解)

以上這篇jersey實(shí)現(xiàn)restful服務(wù)(實(shí)例講解)就是小編分享給大家的全部內(nèi)容了,希望能給大家一個(gè)參考,也希望大家多多支持服務(wù)器之家。

延伸 · 閱讀

精彩推薦
主站蜘蛛池模板: 777午夜精品免费播放 | 亚洲精品福利在线 | 国产成人精品免费大全 | 韩国一大片a毛片女同 | 亚洲美女爱爱 | 国产v日韩v欧美v精品专区 | 日本激情网站 | 久久精视频 | 精品手机在线1卡二卡3卡四卡 | 国产欧美日韩综合 | 精品久久久久久午夜 | jiuse视频| 激情综 | 情缘1完整版在线观看 | 色帝国亚洲欧美在线蜜汁tv | 欧美怡红院视频一区二区三区 | 成人在线视频观看 | 日本九九热| 1024亚洲精品国产 | 无敌秦墨漫画免费阅读 | 国产91网站在线观看 | 国产片自拍 | 亚洲欧美日韩另类在线一 | 国产大乳美女挤奶视频 | 欧美久久天天综合香蕉伊 | 香蕉tv国产在线永久播放 | 三上悠亚久久国产 | 激情自拍网 | 亚洲天堂一区二区在线观看 | xxx美国| 日本漫画工囗全彩内番e绅 日本伦理动漫在线观看 | 好大用力深一点 | 秀逼逼| 激情文学综合网 | 精品午夜久久福利大片免费 | 成人一区二区免费中文字幕 | 亚洲国产日韩成人综合天堂 | 嗯好爽视频 | 欧美一级精品 | h玉足嫩脚嗯啊白丝 | 国产五月天在线 |