maven jersey mysql_Maven和Jersey Framework开发REST风格Web Service
本文演示環境為eclipse + Maven插件 + Jersey framework。本文只關注Jersey的使用,所以只使用類中定義的靜態數據做演示。請在使用時修改我的代碼。如果你的eclipse中沒有安裝 Maven插件,請關注我的博客,我馬上就會推出Maven+eclipse的開發教程。
1. 在eclipse中創建Maven項目
2.單擊"Next"
3. 選擇Maven項目類型為"maven-archetype-webapp"
4. 輸入項目相關的Maven設置
5. 分別創建src/main下java文件夾以及src下test文件夾
6. 設置src/main/java和src/test/java為source folder
7. 最終設置結果如下:
8. 修改pom.xml,添加Maven相應依賴庫
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/maven-v4_0_0.xsd">
4.0.0
net.jianxi.tutorials.jerseyws
jerseywstest
war
1.0
jerseywstest Maven Webapp
http://maven.apache.org
junit
junit
4.7
test
com.sun.jersey
jersey-core
1.3
com.sun.jersey
jersey-server
1.3
com.sun.jersey
jersey-client
1.3
log4j
log4j
1.2.14
javax.ws.rs
jsr311-api
1.1.1
asm
asm
3.2
jerseywstest
maven-compiler-plugin
1.6
1.6
org.codehaus.mojo
tomcat-maven-plugin
target/jerseywstest.war
9. 添加基本POJO類Student:
1 package net.jianxi.tutorials.jerseyws.metadata;
2
3 ?import javax.xml.bind.annotation.XmlRootElement;
4
5 @XmlRootElement
6 ?public class Student {
7 private int id;
8 private String name;
9 private String dept;
10
11 public int getId() {
12 return id;
13 }
14
15 public Student() {
16 }
17
18 public Student(int id, String name, String dept) {
19 super();
20 this.id = id;
21 this.name = name;
22 this.dept = dept;
23 }
24 public void setId(int id) {
25 this.id = id;
26 }
27 public String getName() {
28 return name;
29 }
30 public void setName(String name) {
31 this.name = name;
32 }
33 public String getDept() {
34 return dept;
35 }
36 public void setDept(String dept) {
37 this.dept = dept;
38 }
39
40 }
41
10. 添加一個REST web服務實現類RestWsDemo:
1 package net.jianxi.tutorials.jerseyws;
2
3 ?import java.util.ArrayList;
4 ?import java.util.HashMap;
5 ?import java.util.List;
6 ?import java.util.Map;
7
8 ?import javax.ws.rs.DELETE;
9 ?import javax.ws.rs.FormParam;
10 ?import javax.ws.rs.GET;
11 ?import javax.ws.rs.POST;
12 ?import javax.ws.rs.PUT;
13 import javax.ws.rs.Path;
14 import javax.ws.rs.PathParam;
15 import javax.ws.rs.Produces;
16 import javax.ws.rs.QueryParam;
17 import javax.ws.rs.core.MediaType;
18
19 import net.jianxi.tutorials.jerseyws.metadata.Student;
20
21 import org.apache.log4j.Logger;
22
23
24 @Path("/students")
25 public class RestWsDemo {
26 private static Logger logger = Logger.getLogger(RestWsDemo.class);
27 private static int index = 1;
28 private static Map studentList = new HashMap();
29
30 public RestWsDemo() {
31 if(studentList.size()==0) {
32 studentList.put(index, new Student(index++, "Frank", "CS"));
33 studentList.put(index, new Student(index++, "Jersey", "Math"));
34 }
35 }
36
37 @GET
38 @Path("{studentid}")
39 @Produces({MediaType.APPLICATION_XML, MediaType.APPLICATION_JSON})
40 public Student getMetadata(@PathParam("studentid") int studentid) {
41 if(studentList.containsKey(studentid))
42 return studentList.get(studentid);
43 else
44 return new Student(0, "Nil", "Nil");
45 }
46
47 @GET
48 @Path("list")
49 @Produces({MediaType.APPLICATION_XML, MediaType.APPLICATION_JSON})
50 public List getAllStudents() {
51 List students = new ArrayList();
52 students.addAll(studentList.values());
53 return students;
54 }
55
56 @POST
57 @Path("add")
58 @Produces("text/plain")
59 public String addStudent(@FormParam("name") String name,
60 @FormParam("dept") String dept) {
61 studentList.put(index, new Student(index++, name, dept));
62 return String.valueOf(index-1);
63 }
64
65 @DELETE
66 @Path("delete/{studentid}")
67 @Produces("text/plain")
68 public String removeStudent(@PathParam("studentid") int studentid) {
69 logger.info("Receieving quest for deleting student: " + studentid);
70
71 Student removed = studentList.remove(studentid);
72 if(removed==null) return "failed!";
73 else return "true";
74 }
75
76 @PUT
77 @Path("put")
78 @Produces("text/plain")
79 public String putStudent(@QueryParam("studentid") int studentid,
80 @QueryParam("name") String name,
81 @QueryParam("dept") String dept
82 ) {
83 logger.info("Receieving quest for putting student: " + studentid);
84 if(!studentList.containsKey(studentid))
85 return "failed!";
86 else
87 studentList.put(studentid, new Student(studentid, name, dept));
88
89 return String.valueOf(studentid);
90 }
91 }
92
11. 修改src/main/webapp/WEB-INF/web.xml文件如下:
/p>
"-//Sun Microsystems, Inc.//DTD Web Application 2.3//EN"
"http://java.sun.com/dtd/web-app_2_3.dtd" >
Archetype Created Web Application
jerseyws
com.sun.jersey.spi.container.servlet.ServletContainer
com.sun.jersey.config.property.resourceConfigClass
com.sun.jersey.api.core.PackagesResourceConfig
com.sun.jersey.config.property.packages
net.jianxi.tutorials.jerseyws
1
jerseyws
/rest/*
12. 運行Maven package任務,構建war文件,部署war應用到你的Web服務器。
13. 測試
我馬上就會推出如何用SoapUI工具測試Jersey Web服務的教程。這里這介紹簡單的測試方法。
13.1) 對于GET,可以直接通過瀏覽器進行測試,在瀏覽器中直接輸入:http://localhost:8080/jerseywstest/rest/students/list, 你應該看到返回的XML數據:
CS
1
Frank
Math
2
Jersey
輸入:http://localhost:8080/jerseywstest/rest/students/1則會返回一個學生的信息。
13.2) 測試POST方法。
添加一個testpost.htm文件
Insert title here提交后你在用list方法就可以看到數據的變化。
13.3) PUT和DELETE方法的測試
添加一個Junit測試類
1 package net.jianxi.tutorials.jerseyws;
2
3
4 ?import javax.ws.rs.core.MultivaluedMap;
5
6 ?import org.junit.Before;
7 ?import org.junit.BeforeClass;
8 ?import org.junit.Test;
9
10 ?import com.sun.jersey.api.client.Client;
11 ?import com.sun.jersey.api.client.ClientResponse;
12 ?import com.sun.jersey.api.client.WebResource;
13 ?import com.sun.jersey.core.util.MultivaluedMapImpl;
14
15 ?public class RestWsDemoTest {
16 private String url = "http://localhost:8080/jerseywstest/rest/students";
17
18 @Test
19 public void testDelete() {
20 Client client = Client.create();
21 WebResource webResource = client.resource(url + "/delete/1");
22 ClientResponse response = webResource.delete(ClientResponse.class);
23
24 System.out.println("Response for delete request: " + response.getStatus());
25 }
26
27 @Test
28 public void testPut() {
29 Client client = Client.create();
30 WebResource webResource = client.resource(url + "/put");
31 MultivaluedMap queryParams = new MultivaluedMapImpl();
32 queryParams.add("studentid", "2");
33 queryParams.add("name", "nametest");
34 queryParams.add("dept", "depttest");
35 ClientResponse response = webResource.queryParams(queryParams).put(ClientResponse.class, "foo:test");
36 System.out.println("Response for put request: " + response.getStatus());
37 }
38 }
39
總結
以上是生活随笔為你收集整理的maven jersey mysql_Maven和Jersey Framework开发REST风格Web Service的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: MYSQL处理数据重复值
- 下一篇: VBoxGuestAdditions.i