jersey 是基于java的一个轻量级restful风格的web services框架。以下我基于idea实现restful完整demo。
1.创建maven-web工程,后面就是正常的maven工程创建流程。
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实体类实现。
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
6.运行结果、http://localhost:8080/jerseydemo/rest/application.wadl是所有对外接口的调用方法。使用postman来看看这个接口是怎么调用的吧。
post请求
get请求
以上这篇jersey实现restful服务(实例讲解)就是小编分享给大家的全部内容了,希望能给大家一个参考,也希望大家多多支持服务器之家。