http://www.360doc.com/content/12/1020/21/820209_242674322.shtml
本文演示环境为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相应依赖库
<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>net.jianxi.tutorials.jerseyws</groupId> <artifactId>jerseywstest</artifactId> <packaging>war</packaging> <version>1.0</version> <name>jerseywstest Maven Webapp</name> <url>http://maven.apache.org</url> <dependencies> <dependency> <groupId>junit</groupId> <artifactId>junit</artifactId> <version>4.7</version> <scope>test</scope> </dependency> <dependency> <groupId>com.sun.jersey</groupId> <artifactId>jersey-core</artifactId> <version>1.3</version> </dependency> <dependency> <groupId>com.sun.jersey</groupId> <artifactId>jersey-server</artifactId> <version>1.3</version> </dependency> <dependency> <groupId>com.sun.jersey</groupId> <artifactId>jersey-client</artifactId> <version>1.3</version> </dependency> <dependency> <groupId>log4j</groupId> <artifactId>log4j</artifactId> <version>1.2.14</version> </dependency> <dependency> <groupId>javax.ws.rs</groupId> <artifactId>jsr311-api</artifactId> <version>1.1.1</version> </dependency> <dependency> <groupId>asm</groupId> <artifactId>asm</artifactId> <version>3.2</version> </dependency> </dependencies> <build> <finalName>jerseywstest</finalName> <plugins> <plugin> <artifactId>maven-compiler-plugin</artifactId> <configuration> <source>1.6</source> <target>1.6</target> </configuration> </plugin> <plugin> <groupId>org.codehaus.mojo</groupId> <artifactId>tomcat-maven-plugin</artifactId> <configuration> <warFile>target/jerseywstest.war</warFile> </configuration> </plugin> </plugins> </build> </project>
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:
<div class="cnblogs_code"><div class="cnblogs_code_toolbar"><span class="cnblogs_code_copy"><a target=_blank title="复制代码" style="color: rgb(61, 107, 167);"><img alt="复制代码" src="http://image55.360doc.com/DownloadImg/2012/10/2021/27665308_8.gif" style="border: 0px;" /></a></span></div><pre><span style="color: rgb(0, 128, 128);"> 1</span> <span style="color: rgb(0, 0, 255);">package</span><span style="color: rgb(0, 0, 0);"> net.jianxi.tutorials.jerseyws;
</span><span style="color: rgb(0, 128, 128);"> 2</span> <span style="color: rgb(0, 0, 0);">
</span><span style="color: rgb(0, 128, 128);"> 3</span> <span style="color: rgb(0, 0, 0);"> </span><span style="color: rgb(0, 0, 255);">import</span><span style="color: rgb(0, 0, 0);"> java.util.ArrayList;
</span><span style="color: rgb(0, 128, 128);"> 4</span> <span style="color: rgb(0, 0, 0);"> </span><span style="color: rgb(0, 0, 255);">import</span><span style="color: rgb(0, 0, 0);"> java.util.HashMap;
</span><span style="color: rgb(0, 128, 128);"> 5</span> <span style="color: rgb(0, 0, 0);"> </span><span style="color: rgb(0, 0, 255);">import</span><span style="color: rgb(0, 0, 0);"> java.util.List;
</span><span style="color: rgb(0, 128, 128);"> 6</span> <span style="color: rgb(0, 0, 0);"> </span><span style="color: rgb(0, 0, 255);">import</span><span style="color: rgb(0, 0, 0);"> java.util.Map;
</span><span style="color: rgb(0, 128, 128);"> 7</span> <span style="color: rgb(0, 0, 0);">
</span><span style="color: rgb(0, 128, 128);"> 8</span> <span style="color: rgb(0, 0, 0);"> </span><span style="color: rgb(0, 0, 255);">import</span><span style="color: rgb(0, 0, 0);"> javax.ws.rs.DELETE;
</span><span style="color: rgb(0, 128, 128);"> 9</span> <span style="color: rgb(0, 0, 0);"> </span><span style="color: rgb(0, 0, 255);">import</span><span style="color: rgb(0, 0, 0);"> javax.ws.rs.FormParam;
</span><span style="color: rgb(0, 128, 128);">10</span> <span style="color: rgb(0, 0, 0);"> </span><span style="color: rgb(0, 0, 255);">import</span><span style="color: rgb(0, 0, 0);"> javax.ws.rs.GET;
</span><span style="color: rgb(0, 128, 128);">11</span> <span style="color: rgb(0, 0, 0);"> </span><span style="color: rgb(0, 0, 255);">import</span><span style="color: rgb(0, 0, 0);"> javax.ws.rs.POST;
</span><span style="color: rgb(0, 128, 128);">12</span> <span style="color: rgb(0, 0, 0);"> </span><span style="color: rgb(0, 0, 255);">import</span><span style="color: rgb(0, 0, 0);"> javax.ws.rs.PUT;
</span><span style="color: rgb(0, 128, 128);">13</span> <span style="color: rgb(0, 0, 255);">import</span><span style="color: rgb(0, 0, 0);"> javax.ws.rs.Path;
</span><span style="color: rgb(0, 128, 128);">14</span> <span style="color: rgb(0, 0, 255);">import</span><span style="color: rgb(0, 0, 0);"> javax.ws.rs.PathParam;
</span><span style="color: rgb(0, 128, 128);">15</span> <span style="color: rgb(0, 0, 255);">import</span><span style="color: rgb(0, 0, 0);"> javax.ws.rs.Produces;
</span><span style="color: rgb(0, 128, 128);">16</span> <span style="color: rgb(0, 0, 255);">import</span><span style="color: rgb(0, 0, 0);"> javax.ws.rs.QueryParam;
</span><span style="color: rgb(0, 128, 128);">17</span> <span style="color: rgb(0, 0, 255);">import</span><span style="color: rgb(0, 0, 0);"> javax.ws.rs.core.MediaType;
</span><span style="color: rgb(0, 128, 128);">18</span> <span style="color: rgb(0, 0, 0);">
</span><span style="color: rgb(0, 128, 128);">19</span> <span style="color: rgb(0, 0, 255);">import</span><span style="color: rgb(0, 0, 0);"> net.jianxi.tutorials.jerseyws.metadata.Student;
</span><span style="color: rgb(0, 128, 128);">20</span> <span style="color: rgb(0, 0, 0);">
</span><span style="color: rgb(0, 128, 128);">21</span> <span style="color: rgb(0, 0, 255);">import</span><span style="color: rgb(0, 0, 0);"> org.apache.log4j.Logger;
</span><span style="color: rgb(0, 128, 128);">22</span> <span style="color: rgb(0, 0, 0);">
</span><span style="color: rgb(0, 128, 128);">23</span> <span style="color: rgb(0, 0, 0);">
</span><span style="color: rgb(0, 128, 128);">24</span> <span style="color: rgb(0, 0, 0);">@Path(</span><span style="color: rgb(0, 0, 0);">"</span><span style="color: rgb(0, 0, 0);">/students</span><span style="color: rgb(0, 0, 0);">"</span><span style="color: rgb(0, 0, 0);">)
</span><span style="color: rgb(0, 128, 128);">25</span> <span style="color: rgb(0, 0, 255);">public</span><span style="color: rgb(0, 0, 0);"> </span><span style="color: rgb(0, 0, 255);">class</span><span style="color: rgb(0, 0, 0);"> RestWsDemo {
</span><span style="color: rgb(0, 128, 128);">26</span> <span style="color: rgb(0, 0, 0);"> </span><span style="color: rgb(0, 0, 255);">private</span><span style="color: rgb(0, 0, 0);"> </span><span style="color: rgb(0, 0, 255);">static</span><span style="color: rgb(0, 0, 0);"> Logger logger </span><span style="color: rgb(0, 0, 0);">=</span><span style="color: rgb(0, 0, 0);"> Logger.getLogger(RestWsDemo.</span><span style="color: rgb(0, 0, 255);">class</span><span style="color: rgb(0, 0, 0);">);
</span><span style="color: rgb(0, 128, 128);">27</span> <span style="color: rgb(0, 0, 0);"> </span><span style="color: rgb(0, 0, 255);">private</span><span style="color: rgb(0, 0, 0);"> </span><span style="color: rgb(0, 0, 255);">static</span><span style="color: rgb(0, 0, 0);"> </span><span style="color: rgb(0, 0, 255);">int</span><span style="color: rgb(0, 0, 0);"> index </span><span style="color: rgb(0, 0, 0);">=</span><span style="color: rgb(0, 0, 0);"> </span><span style="color: rgb(0, 0, 0);">1</span><span style="color: rgb(0, 0, 0);">;
</span><span style="color: rgb(0, 128, 128);">28</span> <span style="color: rgb(0, 0, 0);"> </span><span style="color: rgb(0, 0, 255);">private</span><span style="color: rgb(0, 0, 0);"> </span><span style="color: rgb(0, 0, 255);">static</span><span style="color: rgb(0, 0, 0);"> Map</span><span style="color: rgb(0, 0, 0);"><</span><span style="color: rgb(0, 0, 0);">Integer,Student</span><span style="color: rgb(0, 0, 0);">></span><span style="color: rgb(0, 0, 0);"> studentList </span><span style="color: rgb(0, 0, 0);">=</span><span style="color: rgb(0, 0, 0);"> </span><span style="color: rgb(0, 0, 255);">new</span><span style="color: rgb(0, 0, 0);"> HashMap</span><span style="color: rgb(0, 0, 0);"><</span><span style="color: rgb(0, 0, 0);">Integer, Student</span><span style="color: rgb(0, 0, 0);">></span><span style="color: rgb(0, 0, 0);">();
</span><span style="color: rgb(0, 128, 128);">29</span> <span style="color: rgb(0, 0, 0);">
</span><span style="color: rgb(0, 128, 128);">30</span> <span style="color: rgb(0, 0, 0);"> </span><span style="color: rgb(0, 0, 255);">public</span><span style="color: rgb(0, 0, 0);"> RestWsDemo() {
</span><span style="color: rgb(0, 128, 128);">31</span> <span style="color: rgb(0, 0, 0);"> </span><span style="color: rgb(0, 0, 255);">if</span><span style="color: rgb(0, 0, 0);">(studentList.size()</span><span style="color: rgb(0, 0, 0);">==</span><span style="color: rgb(0, 0, 0);">0</span><span style="color: rgb(0, 0, 0);">) {
</span><span style="color: rgb(0, 128, 128);">32</span> <span style="color: rgb(0, 0, 0);"> studentList.put(index, </span><span style="color: rgb(0, 0, 255);">new</span><span style="color: rgb(0, 0, 0);"> Student(index</span><span style="color: rgb(0, 0, 0);">++</span><span style="color: rgb(0, 0, 0);">, </span><span style="color: rgb(0, 0, 0);">"</span><span style="color: rgb(0, 0, 0);">Frank</span><span style="color: rgb(0, 0, 0);">"</span><span style="color: rgb(0, 0, 0);">, </span><span style="color: rgb(0, 0, 0);">"</span><span style="color: rgb(0, 0, 0);">CS</span><span style="color: rgb(0, 0, 0);">"</span><span style="color: rgb(0, 0, 0);">));
</span><span style="color: rgb(0, 128, 128);">33</span> <span style="color: rgb(0, 0, 0);"> studentList.put(index, </span><span style="color: rgb(0, 0, 255);">new</span><span style="color: rgb(0, 0, 0);"> Student(index</span><span style="color: rgb(0, 0, 0);">++</span><span style="color: rgb(0, 0, 0);">, </span><span style="color: rgb(0, 0, 0);">"</span><span style="color: rgb(0, 0, 0);">Jersey</span><span style="color: rgb(0, 0, 0);">"</span><span style="color: rgb(0, 0, 0);">, </span><span style="color: rgb(0, 0, 0);">"</span><span style="color: rgb(0, 0, 0);">Math</span><span style="color: rgb(0, 0, 0);">"</span><span style="color: rgb(0, 0, 0);">));
</span><span style="color: rgb(0, 128, 128);">34</span> <span style="color: rgb(0, 0, 0);"> }
</span><span style="color: rgb(0, 128, 128);">35</span> <span style="color: rgb(0, 0, 0);"> }
</span><span style="color: rgb(0, 128, 128);">36</span> <span style="color: rgb(0, 0, 0);">
</span><span style="color: rgb(0, 128, 128);">37</span> <span style="color: rgb(0, 0, 0);"> @GET
</span><span style="color: rgb(0, 128, 128);">38</span> <span style="color: rgb(0, 0, 0);"> @Path(</span><span style="color: rgb(0, 0, 0);">"</span><span style="color: rgb(0, 0, 0);">{studentid}</span><span style="color: rgb(0, 0, 0);">"</span><span style="color: rgb(0, 0, 0);">)
</span><span style="color: rgb(0, 128, 128);">39</span> <span style="color: rgb(0, 0, 0);"> @Produces({MediaType.APPLICATION_XML, MediaType.APPLICATION_JSON})
</span><span style="color: rgb(0, 128, 128);">40</span> <span style="color: rgb(0, 0, 0);"> </span><span style="color: rgb(0, 0, 255);">public</span><span style="color: rgb(0, 0, 0);"> Student getMetadata(@PathParam(</span><span style="color: rgb(0, 0, 0);">"</span><span style="color: rgb(0, 0, 0);">studentid</span><span style="color: rgb(0, 0, 0);">"</span><span style="color: rgb(0, 0, 0);">) </span><span style="color: rgb(0, 0, 255);">int</span><span style="color: rgb(0, 0, 0);"> studentid) {
</span><span style="color: rgb(0, 128, 128);">41</span> <span style="color: rgb(0, 0, 0);"> </span><span style="color: rgb(0, 0, 255);">if</span><span style="color: rgb(0, 0, 0);">(studentList.containsKey(studentid))
</span><span style="color: rgb(0, 128, 128);">42</span> <span style="color: rgb(0, 0, 0);"> </span><span style="color: rgb(0, 0, 255);">return</span><span style="color: rgb(0, 0, 0);"> studentList.get(studentid);
</span><span style="color: rgb(0, 128, 128);">43</span> <span style="color: rgb(0, 0, 0);"> </span><span style="color: rgb(0, 0, 255);">else</span><span style="color: rgb(0, 0, 0);">
</span><span style="color: rgb(0, 128, 128);">44</span> <span style="color: rgb(0, 0, 0);"> </span><span style="color: rgb(0, 0, 255);">return</span><span style="color: rgb(0, 0, 0);"> </span><span style="color: rgb(0, 0, 255);">new</span><span style="color: rgb(0, 0, 0);"> Student(</span><span style="color: rgb(0, 0, 0);">0</span><span style="color: rgb(0, 0, 0);">, </span><span style="color: rgb(0, 0, 0);">"</span><span style="color: rgb(0, 0, 0);">Nil</span><span style="color: rgb(0, 0, 0);">"</span><span style="color: rgb(0, 0, 0);">, </span><span style="color: rgb(0, 0, 0);">"</span><span style="color: rgb(0, 0, 0);">Nil</span><span style="color: rgb(0, 0, 0);">"</span><span style="color: rgb(0, 0, 0);">);
</span><span style="color: rgb(0, 128, 128);">45</span> <span style="color: rgb(0, 0, 0);"> }
</span><span style="color: rgb(0, 128, 128);">46</span> <span style="color: rgb(0, 0, 0);">
</span><span style="color: rgb(0, 128, 128);">47</span> <span style="color: rgb(0, 0, 0);"> @GET
</span><span style="color: rgb(0, 128, 128);">48</span> <span style="color: rgb(0, 0, 0);"> @Path(</span><span style="color: rgb(0, 0, 0);">"</span><span style="color: rgb(0, 0, 0);">list</span><span style="color: rgb(0, 0, 0);">"</span><span style="color: rgb(0, 0, 0);">)
</span><span style="color: rgb(0, 128, 128);">49</span> <span style="color: rgb(0, 0, 0);"> @Produces({MediaType.APPLICATION_XML, MediaType.APPLICATION_JSON})
</span><span style="color: rgb(0, 128, 128);">50</span> <span style="color: rgb(0, 0, 0);"> </span><span style="color: rgb(0, 0, 255);">public</span><span style="color: rgb(0, 0, 0);"> List</span><span style="color: rgb(0, 0, 0);"><</span><span style="color: rgb(0, 0, 0);">Student</span><span style="color: rgb(0, 0, 0);">></span><span style="color: rgb(0, 0, 0);"> getAllStudents() {
</span><span style="color: rgb(0, 128, 128);">51</span> <span style="color: rgb(0, 0, 0);"> List</span><span style="color: rgb(0, 0, 0);"><</span><span style="color: rgb(0, 0, 0);">Student</span><span style="color: rgb(0, 0, 0);">></span><span style="color: rgb(0, 0, 0);"> students </span><span style="color: rgb(0, 0, 0);">=</span><span style="color: rgb(0, 0, 0);"> </span><span style="color: rgb(0, 0, 255);">new</span><span style="color: rgb(0, 0, 0);"> ArrayList</span><span style="color: rgb(0, 0, 0);"><</span><span style="color: rgb(0, 0, 0);">Student</span><span style="color: rgb(0, 0, 0);">></span><span style="color: rgb(0, 0, 0);">();
</span><span style="color: rgb(0, 128, 128);">52</span> <span style="color: rgb(0, 0, 0);"> students.addAll(studentList.values());
</span><span style="color: rgb(0, 128, 128);">53</span> <span style="color: rgb(0, 0, 0);"> </span><span style="color: rgb(0, 0, 255);">return</span><span style="color: rgb(0, 0, 0);"> students;
</span><span style="color: rgb(0, 128, 128);">54</span> <span style="color: rgb(0, 0, 0);"> }
</span><span style="color: rgb(0, 128, 128);">55</span> <span style="color: rgb(0, 0, 0);">
</span><span style="color: rgb(0, 128, 128);">56</span> <span style="color: rgb(0, 0, 0);"> @POST
</span><span style="color: rgb(0, 128, 128);">57</span> <span style="color: rgb(0, 0, 0);"> @Path(</span><span style="color: rgb(0, 0, 0);">"</span><span style="color: rgb(0, 0, 0);">add</span><span style="color: rgb(0, 0, 0);">"</span><span style="color: rgb(0, 0, 0);">)
</span><span style="color: rgb(0, 128, 128);">58</span> <span style="color: rgb(0, 0, 0);"> @Produces(</span><span style="color: rgb(0, 0, 0);">"</span><span style="color: rgb(0, 0, 0);">text/plain</span><span style="color: rgb(0, 0, 0);">"</span><span style="color: rgb(0, 0, 0);">)
</span><span style="color: rgb(0, 128, 128);">59</span> <span style="color: rgb(0, 0, 0);"> </span><span style="color: rgb(0, 0, 255);">public</span><span style="color: rgb(0, 0, 0);"> String addStudent(@FormParam(</span><span style="color: rgb(0, 0, 0);">"</span><span style="color: rgb(0, 0, 0);">name</span><span style="color: rgb(0, 0, 0);">"</span><span style="color: rgb(0, 0, 0);">) String name,
</span><span style="color: rgb(0, 128, 128);">60</span> <span style="color: rgb(0, 0, 0);"> @FormParam(</span><span style="color: rgb(0, 0, 0);">"</span><span style="color: rgb(0, 0, 0);">dept</span><span style="color: rgb(0, 0, 0);">"</span><span style="color: rgb(0, 0, 0);">) String dept) {
</span><span style="color: rgb(0, 128, 128);">61</span> <span style="color: rgb(0, 0, 0);"> studentList.put(index, </span><span style="color: rgb(0, 0, 255);">new</span><span style="color: rgb(0, 0, 0);"> Student(index</span><span style="color: rgb(0, 0, 0);">++</span><span style="color: rgb(0, 0, 0);">, name, dept));
</span><span style="color: rgb(0, 128, 128);">62</span> <span style="color: rgb(0, 0, 0);"> </span><span style="color: rgb(0, 0, 255);">return</span><span style="color: rgb(0, 0, 0);"> String.valueOf(index</span><span style="color: rgb(0, 0, 0);">-</span><span style="color: rgb(0, 0, 0);">1</span><span style="color: rgb(0, 0, 0);">);
</span><span style="color: rgb(0, 128, 128);">63</span> <span style="color: rgb(0, 0, 0);"> }
</span><span style="color: rgb(0, 128, 128);">64</span> <span style="color: rgb(0, 0, 0);">
</span><span style="color: rgb(0, 128, 128);">65</span> <span style="color: rgb(0, 0, 0);"> @DELETE
</span><span style="color: rgb(0, 128, 128);">66</span> <span style="color: rgb(0, 0, 0);"> @Path(</span><span style="color: rgb(0, 0, 0);">"</span><span style="color: rgb(0, 0, 0);">delete/{studentid}</span><span style="color: rgb(0, 0, 0);">"</span><span style="color: rgb(0, 0, 0);">)
</span><span style="color: rgb(0, 128, 128);">67</span> <span style="color: rgb(0, 0, 0);"> @Produces(</span><span style="color: rgb(0, 0, 0);">"</span><span style="color: rgb(0, 0, 0);">text/plain</span><span style="color: rgb(0, 0, 0);">"</span><span style="color: rgb(0, 0, 0);">)
</span><span style="color: rgb(0, 128, 128);">68</span> <span style="color: rgb(0, 0, 0);"> </span><span style="color: rgb(0, 0, 255);">public</span><span style="color: rgb(0, 0, 0);"> String removeStudent(@PathParam(</span><span style="color: rgb(0, 0, 0);">"</span><span style="color: rgb(0, 0, 0);">studentid</span><span style="color: rgb(0, 0, 0);">"</span><span style="color: rgb(0, 0, 0);">) </span><span style="color: rgb(0, 0, 255);">int</span><span style="color: rgb(0, 0, 0);"> studentid) {
</span><span style="color: rgb(0, 128, 128);">69</span> <span style="color: rgb(0, 0, 0);"> logger.info(</span><span style="color: rgb(0, 0, 0);">"</span><span style="color: rgb(0, 0, 0);">Receieving quest for deleting student: </span><span style="color: rgb(0, 0, 0);">"</span><span style="color: rgb(0, 0, 0);"> </span><span style="color: rgb(0, 0, 0);">+</span><span style="color: rgb(0, 0, 0);"> studentid);
</span><span style="color: rgb(0, 128, 128);">70</span> <span style="color: rgb(0, 0, 0);">
</span><span style="color: rgb(0, 128, 128);">71</span> <span style="color: rgb(0, 0, 0);"> Student removed </span><span style="color: rgb(0, 0, 0);">=</span><span style="color: rgb(0, 0, 0);"> studentList.remove(studentid);
</span><span style="color: rgb(0, 128, 128);">72</span> <span style="color: rgb(0, 0, 0);"> </span><span style="color: rgb(0, 0, 255);">if</span><span style="color: rgb(0, 0, 0);">(removed</span><span style="color: rgb(0, 0, 0);">==</span><span style="color: rgb(0, 0, 255);">null</span><span style="color: rgb(0, 0, 0);">) </span><span style="color: rgb(0, 0, 255);">return</span><span style="color: rgb(0, 0, 0);"> </span><span style="color: rgb(0, 0, 0);">"</span><span style="color: rgb(0, 0, 0);">failed!</span><span style="color: rgb(0, 0, 0);">"</span><span style="color: rgb(0, 0, 0);">;
</span><span style="color: rgb(0, 128, 128);">73</span> <span style="color: rgb(0, 0, 0);"> </span><span style="color: rgb(0, 0, 255);">else</span><span style="color: rgb(0, 0, 0);"> </span><span style="color: rgb(0, 0, 255);">return</span><span style="color: rgb(0, 0, 0);"> </span><span style="color: rgb(0, 0, 0);">"</span><span style="color: rgb(0, 0, 0);">true</span><span style="color: rgb(0, 0, 0);">"</span><span style="color: rgb(0, 0, 0);">;
</span><span style="color: rgb(0, 128, 128);">74</span> <span style="color: rgb(0, 0, 0);"> }
</span><span style="color: rgb(0, 128, 128);">75</span> <span style="color: rgb(0, 0, 0);">
</span><span style="color: rgb(0, 128, 128);">76</span> <span style="color: rgb(0, 0, 0);"> @PUT
</span><span style="color: rgb(0, 128, 128);">77</span> <span style="color: rgb(0, 0, 0);"> @Path(</span><span style="color: rgb(0, 0, 0);">"</span><span style="color: rgb(0, 0, 0);">put</span><span style="color: rgb(0, 0, 0);">"</span><span style="color: rgb(0, 0, 0);">)
</span><span style="color: rgb(0, 128, 128);">78</span> <span style="color: rgb(0, 0, 0);"> @Produces(</span><span style="color: rgb(0, 0, 0);">"</span><span style="color: rgb(0, 0, 0);">text/plain</span><span style="color: rgb(0, 0, 0);">"</span><span style="color: rgb(0, 0, 0);">)
</span><span style="color: rgb(0, 128, 128);">79</span> <span style="color: rgb(0, 0, 0);"> </span><span style="color: rgb(0, 0, 255);">public</span><span style="color: rgb(0, 0, 0);"> String putStudent(@QueryParam(</span><span style="color: rgb(0, 0, 0);">"</span><span style="color: rgb(0, 0, 0);">studentid</span><span style="color: rgb(0, 0, 0);">"</span><span style="color: rgb(0, 0, 0);">) </span><span style="color: rgb(0, 0, 255);">int</span><span style="color: rgb(0, 0, 0);"> studentid,
</span><span style="color: rgb(0, 128, 128);">80</span> <span style="color: rgb(0, 0, 0);"> @QueryParam(</span><span style="color: rgb(0, 0, 0);">"</span><span style="color: rgb(0, 0, 0);">name</span><span style="color: rgb(0, 0, 0);">"</span><span style="color: rgb(0, 0, 0);">) String name,
</span><span style="color: rgb(0, 128, 128);">81</span> <span style="color: rgb(0, 0, 0);"> @QueryParam(</span><span style="color: rgb(0, 0, 0);">"</span><span style="color: rgb(0, 0, 0);">dept</span><span style="color: rgb(0, 0, 0);">"</span><span style="color: rgb(0, 0, 0);">) String dept
</span><span style="color: rgb(0, 128, 128);">82</span> <span style="color: rgb(0, 0, 0);"> ) {
</span><span style="color: rgb(0, 128, 128);">83</span> <span style="color: rgb(0, 0, 0);"> logger.info(</span><span style="color: rgb(0, 0, 0);">"</span><span style="color: rgb(0, 0, 0);">Receieving quest for putting student: </span><span style="color: rgb(0, 0, 0);">"</span><span style="color: rgb(0, 0, 0);"> </span><span style="color: rgb(0, 0, 0);">+</span><span style="color: rgb(0, 0, 0);"> studentid);
</span><span style="color: rgb(0, 128, 128);">84</span> <span style="color: rgb(0, 0, 0);"> </span><span style="color: rgb(0, 0, 255);">if</span><span style="color: rgb(0, 0, 0);">(</span><span style="color: rgb(0, 0, 0);">!</span><span style="color: rgb(0, 0, 0);">studentList.containsKey(studentid))
</span><span style="color: rgb(0, 128, 128);">85</span> <span style="color: rgb(0, 0, 0);"> </span><span style="color: rgb(0, 0, 255);">return</span><span style="color: rgb(0, 0, 0);"> </span><span style="color: rgb(0, 0, 0);">"</span><span style="color: rgb(0, 0, 0);">failed!</span><span style="color: rgb(0, 0, 0);">"</span><span style="color: rgb(0, 0, 0);">;
</span><span style="color: rgb(0, 128, 128);">86</span> <span style="color: rgb(0, 0, 0);"> </span><span style="color: rgb(0, 0, 255);">else</span><span style="color: rgb(0, 0, 0);">
</span><span style="color: rgb(0, 128, 128);">87</span> <span style="color: rgb(0, 0, 0);"> studentList.put(studentid, </span><span style="color: rgb(0, 0, 255);">new</span><span style="color: rgb(0, 0, 0);"> Student(studentid, name, dept));
</span><span style="color: rgb(0, 128, 128);">88</span> <span style="color: rgb(0, 0, 0);">
</span><span style="color: rgb(0, 128, 128);">89</span> <span style="color: rgb(0, 0, 0);"> </span><span style="color: rgb(0, 0, 255);">return</span><span style="color: rgb(0, 0, 0);"> String.valueOf(studentid);
</span><span style="color: rgb(0, 128, 128);">90</span> <span style="color: rgb(0, 0, 0);"> }
</span><span style="color: rgb(0, 128, 128);">91</span> <span style="color: rgb(0, 0, 0);">}
</span><span style="color: rgb(0, 128, 128);">92</span>
11. 修改src/main/webapp/WEB-INF/web.xml文件如下:
<div class="cnblogs_code"><div class="cnblogs_code_toolbar"><span class="cnblogs_code_copy"><a target=_blank title="复制代码" style="color: rgb(61, 107, 167);"><img alt="复制代码" src="http://image55.360doc.com/DownloadImg/2012/10/2021/27665308_8.gif" style="border: 0px;" /></a></span></div><pre><span style="color: rgb(0, 0, 255);"><!</span><span style="color: rgb(255, 0, 255);">DOCTYPE web-app PUBLIC "-//Sun Microsystems, Inc.//DTD Web Application 2.3//EN" "http://java.sun.com/dtd/web-app_2_3.dtd" </span><span style="color: rgb(0, 0, 255);">></span><span style="color: rgb(0, 0, 0);"> </span><span style="color: rgb(0, 0, 255);"><</span><span style="color: rgb(128, 0, 0);">web-app</span><span style="color: rgb(0, 0, 255);">></span><span style="color: rgb(0, 0, 0);"> </span><span style="color: rgb(0, 0, 255);"><</span><span style="color: rgb(128, 0, 0);">display-name</span><span style="color: rgb(0, 0, 255);">></span><span style="color: rgb(0, 0, 0);">Archetype Created Web Application</span><span style="color: rgb(0, 0, 255);"></</span><span style="color: rgb(128, 0, 0);">display-name</span><span style="color: rgb(0, 0, 255);">></span><span style="color: rgb(0, 0, 0);"> </span><span style="color: rgb(0, 0, 255);"><</span><span style="color: rgb(128, 0, 0);">servlet</span><span style="color: rgb(0, 0, 255);">></span><span style="color: rgb(0, 0, 0);"> </span><span style="color: rgb(0, 0, 255);"><</span><span style="color: rgb(128, 0, 0);">servlet-name</span><span style="color: rgb(0, 0, 255);">></span><span style="color: rgb(0, 0, 0);">jerseyws</span><span style="color: rgb(0, 0, 255);"></</span><span style="color: rgb(128, 0, 0);">servlet-name</span><span style="color: rgb(0, 0, 255);">></span><span style="color: rgb(0, 0, 0);"> </span><span style="color: rgb(0, 0, 255);"><</span><span style="color: rgb(128, 0, 0);">servlet-class</span><span style="color: rgb(0, 0, 255);">></span><span style="color: rgb(0, 0, 0);">com.sun.jersey.spi.container.servlet.ServletContainer</span><span style="color: rgb(0, 0, 255);"></</span><span style="color: rgb(128, 0, 0);">servlet-class</span><span style="color: rgb(0, 0, 255);">></span><span style="color: rgb(0, 0, 0);"> </span><span style="color: rgb(0, 0, 255);"><</span><span style="color: rgb(128, 0, 0);">init-param</span><span style="color: rgb(0, 0, 255);">></span><span style="color: rgb(0, 0, 0);"> </span><span style="color: rgb(0, 0, 255);"><</span><span style="color: rgb(128, 0, 0);">param-name</span><span style="color: rgb(0, 0, 255);">></span><span style="color: rgb(0, 0, 0);">com.sun.jersey.config.property.resourceConfigClass</span><span style="color: rgb(0, 0, 255);"></</span><span style="color: rgb(128, 0, 0);">param-name</span><span style="color: rgb(0, 0, 255);">></span><span style="color: rgb(0, 0, 0);"> </span><span style="color: rgb(0, 0, 255);"><</span><span style="color: rgb(128, 0, 0);">param-value</span><span style="color: rgb(0, 0, 255);">></span><span style="color: rgb(0, 0, 0);">com.sun.jersey.api.core.PackagesResourceConfig</span><span style="color: rgb(0, 0, 255);"></</span><span style="color: rgb(128, 0, 0);">param-value</span><span style="color: rgb(0, 0, 255);">></span><span style="color: rgb(0, 0, 0);"> </span><span style="color: rgb(0, 0, 255);"></</span><span style="color: rgb(128, 0, 0);">init-param</span><span style="color: rgb(0, 0, 255);">></span><span style="color: rgb(0, 0, 0);"> </span><span style="color: rgb(0, 0, 255);"><</span><span style="color: rgb(128, 0, 0);">init-param</span><span style="color: rgb(0, 0, 255);">></span><span style="color: rgb(0, 0, 0);"> </span><span style="color: rgb(0, 0, 255);"><</span><span style="color: rgb(128, 0, 0);">param-name</span><span style="color: rgb(0, 0, 255);">></span><span style="color: rgb(0, 0, 0);">com.sun.jersey.config.property.packages</span><span style="color: rgb(0, 0, 255);"></</span><span style="color: rgb(128, 0, 0);">param-name</span><span style="color: rgb(0, 0, 255);">></span><span style="color: rgb(0, 0, 0);"> </span><span style="color: rgb(0, 0, 255);"><</span><span style="color: rgb(128, 0, 0);">param-value</span><span style="color: rgb(0, 0, 255);">></span><span style="color: rgb(0, 0, 0);">net.jianxi.tutorials.jerseyws</span><span style="color: rgb(0, 0, 255);"></</span><span style="color: rgb(128, 0, 0);">param-value</span><span style="color: rgb(0, 0, 255);">></span><span style="color: rgb(0, 0, 0);"> </span><span style="color: rgb(0, 0, 255);"></</span><span style="color: rgb(128, 0, 0);">init-param</span><span style="color: rgb(0, 0, 255);">></span><span style="color: rgb(0, 0, 0);"> </span><span style="color: rgb(0, 0, 255);"><</span><span style="color: rgb(128, 0, 0);">load-on-startup</span><span style="color: rgb(0, 0, 255);">></span><span style="color: rgb(0, 0, 0);">1</span><span style="color: rgb(0, 0, 255);"></</span><span style="color: rgb(128, 0, 0);">load-on-startup</span><span style="color: rgb(0, 0, 255);">></span><span style="color: rgb(0, 0, 0);"> </span><span style="color: rgb(0, 0, 255);"></</span><span style="color: rgb(128, 0, 0);">servlet</span><span style="color: rgb(0, 0, 255);">></span><span style="color: rgb(0, 0, 0);"> </span><span style="color: rgb(0, 0, 255);"><</span><span style="color: rgb(128, 0, 0);">servlet-mapping</span><span style="color: rgb(0, 0, 255);">></span><span style="color: rgb(0, 0, 0);"> </span><span style="color: rgb(0, 0, 255);"><</span><span style="color: rgb(128, 0, 0);">servlet-name</span><span style="color: rgb(0, 0, 255);">></span><span style="color: rgb(0, 0, 0);">jerseyws</span><span style="color: rgb(0, 0, 255);"></</span><span style="color: rgb(128, 0, 0);">servlet-name</span><span style="color: rgb(0, 0, 255);">></span><span style="color: rgb(0, 0, 0);"> </span><span style="color: rgb(0, 0, 255);"><</span><span style="color: rgb(128, 0, 0);">url-pattern</span><span style="color: rgb(0, 0, 255);">></span><span style="color: rgb(0, 0, 0);">/rest/*</span><span style="color: rgb(0, 0, 255);"></</span><span style="color: rgb(128, 0, 0);">url-pattern</span><span style="color: rgb(0, 0, 255);">></span><span style="color: rgb(0, 0, 0);"> </span><span style="color: rgb(0, 0, 255);"></</span><span style="color: rgb(128, 0, 0);">servlet-mapping</span><span style="color: rgb(0, 0, 255);">></span><span style="color: rgb(0, 0, 0);"> </span><span style="color: rgb(0, 0, 255);"></</span><span style="color: rgb(128, 0, 0);">web-app</span><span style="color: rgb(0, 0, 255);">></span><span style="color: rgb(0, 0, 0);"> </span>
12. 运行Maven package任务,构建war文件,部署war应用到你的Web服务器。
13. 测试
我马上就会推出如何用SoapUI工具测试Jersey Web服务的教程。这里这介绍简单的测试方法。
13.1) 对于GET,可以直接通过浏览器进行测试,在浏览器中直接输入:http://localhost:8080/jerseywstest/rest/students/list, 你应该看到返回的XML数据:
<div class="cnblogs_code"><div class="cnblogs_code_toolbar"><span class="cnblogs_code_copy"><a target=_blank title="复制代码" style="color: rgb(61, 107, 167);"><img alt="复制代码" src="http://image55.360doc.com/DownloadImg/2012/10/2021/27665308_8.gif" style="border: 0px;" /></a></span></div><pre><span style="color: rgb(0, 0, 255);"><</span><span style="color: rgb(128, 0, 0);">students</span><span style="color: rgb(0, 0, 255);">></span><span style="color: rgb(0, 0, 0);"> </span><span style="color: rgb(0, 0, 255);"><</span><span style="color: rgb(128, 0, 0);">student</span><span style="color: rgb(0, 0, 255);">></span><span style="color: rgb(0, 0, 0);"> </span><span style="color: rgb(0, 0, 255);"><</span><span style="color: rgb(128, 0, 0);">dept</span><span style="color: rgb(0, 0, 255);">></span><span style="color: rgb(0, 0, 0);">CS</span><span style="color: rgb(0, 0, 255);"></</span><span style="color: rgb(128, 0, 0);">dept</span><span style="color: rgb(0, 0, 255);">></span><span style="color: rgb(0, 0, 0);"> </span><span style="color: rgb(0, 0, 255);"><</span><span style="color: rgb(128, 0, 0);">id</span><span style="color: rgb(0, 0, 255);">></span><span style="color: rgb(0, 0, 0);">1</span><span style="color: rgb(0, 0, 255);"></</span><span style="color: rgb(128, 0, 0);">id</span><span style="color: rgb(0, 0, 255);">></span><span style="color: rgb(0, 0, 0);"> </span><span style="color: rgb(0, 0, 255);"><</span><span style="color: rgb(128, 0, 0);">name</span><span style="color: rgb(0, 0, 255);">></span><span style="color: rgb(0, 0, 0);">Frank</span><span style="color: rgb(0, 0, 255);"></</span><span style="color: rgb(128, 0, 0);">name</span><span style="color: rgb(0, 0, 255);">></span><span style="color: rgb(0, 0, 0);"> </span><span style="color: rgb(0, 0, 255);"></</span><span style="color: rgb(128, 0, 0);">student</span><span style="color: rgb(0, 0, 255);">></span><span style="color: rgb(0, 0, 0);"> </span><span style="color: rgb(0, 0, 255);"><</span><span style="color: rgb(128, 0, 0);">student</span><span style="color: rgb(0, 0, 255);">></span><span style="color: rgb(0, 0, 0);"> </span><span style="color: rgb(0, 0, 255);"><</span><span style="color: rgb(128, 0, 0);">dept</span><span style="color: rgb(0, 0, 255);">></span><span style="color: rgb(0, 0, 0);">Math</span><span style="color: rgb(0, 0, 255);"></</span><span style="color: rgb(128, 0, 0);">dept</span><span style="color: rgb(0, 0, 255);">></span><span style="color: rgb(0, 0, 0);"> </span><span style="color: rgb(0, 0, 255);"><</span><span style="color: rgb(128, 0, 0);">id</span><span style="color: rgb(0, 0, 255);">></span><span style="color: rgb(0, 0, 0);">2</span><span style="color: rgb(0, 0, 255);"></</span><span style="color: rgb(128, 0, 0);">id</span><span style="color: rgb(0, 0, 255);">></span><span style="color: rgb(0, 0, 0);"> </span><span style="color: rgb(0, 0, 255);"><</span><span style="color: rgb(128, 0, 0);">name</span><span style="color: rgb(0, 0, 255);">></span><span style="color: rgb(0, 0, 0);">Jersey</span><span style="color: rgb(0, 0, 255);"></</span><span style="color: rgb(128, 0, 0);">name</span><span style="color: rgb(0, 0, 255);">></span><span style="color: rgb(0, 0, 0);"> </span><span style="color: rgb(0, 0, 255);"></</span><span style="color: rgb(128, 0, 0);">student</span><span style="color: rgb(0, 0, 255);">></span><span style="color: rgb(0, 0, 0);"> </span><span style="color: rgb(0, 0, 255);"></</span><span style="color: rgb(128, 0, 0);">students</span><span style="color: rgb(0, 0, 255);">></span>输入: http://localhost:8080/jerseywstest/rest/students/1则会返回一个学生的信息。
13.2) 测试POST方法。
添加一个testpost.htm文件
<div class="cnblogs_code"><div class="cnblogs_code_toolbar"><span class="cnblogs_code_copy"><a target=_blank title="复制代码" style="color: rgb(61, 107, 167);"><img alt="复制代码" src="http://image55.360doc.com/DownloadImg/2012/10/2021/27665308_8.gif" style="border: 0px;" /></a></span></div><pre><span style="color: rgb(0, 0, 255);"><!</span><span style="color: rgb(255, 0, 255);">DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd"</span><span style="color: rgb(0, 0, 255);">></span><span style="color: rgb(0, 0, 0);">
</span><span style="color: rgb(0, 0, 255);"><</span><span style="color: rgb(128, 0, 0);">html</span><span style="color: rgb(0, 0, 255);">></span><span style="color: rgb(0, 0, 0);">
</span><span style="color: rgb(0, 0, 255);"><</span><span style="color: rgb(128, 0, 0);">head</span><span style="color: rgb(0, 0, 255);">></span><span style="color: rgb(0, 0, 0);">
</span><span style="color: rgb(0, 0, 255);"><</span><span style="color: rgb(128, 0, 0);">meta </span><span style="color: rgb(255, 0, 0);">http-equiv</span><span style="color: rgb(0, 0, 255);">="Content-Type"</span><span style="color: rgb(255, 0, 0);"> content</span><span style="color: rgb(0, 0, 255);">="text/html; charset=ISO-8859-1"</span><span style="color: rgb(0, 0, 255);">></span><span style="color: rgb(0, 0, 0);">
</span><span style="color: rgb(0, 0, 255);"><</span><span style="color: rgb(128, 0, 0);">title</span><span style="color: rgb(0, 0, 255);">></span><span style="color: rgb(0, 0, 0);">Insert title here</span><span style="color: rgb(0, 0, 255);"></</span><span style="color: rgb(128, 0, 0);">title</span><span style="color: rgb(0, 0, 255);">></span><span style="color: rgb(0, 0, 0);">
</span><span style="color: rgb(0, 0, 255);"></</span><span style="color: rgb(128, 0, 0);">head</span><span style="color: rgb(0, 0, 255);">></span><span style="color: rgb(0, 0, 0);">
</span><span style="color: rgb(0, 0, 255);"><</span><span style="color: rgb(128, 0, 0);">body</span><span style="color: rgb(0, 0, 255);">></span><span style="color: rgb(0, 0, 0);">
</span><span style="color: rgb(0, 0, 255);"><</span><span style="color: rgb(128, 0, 0);">form </span><span style="color: rgb(255, 0, 0);">action</span><span style="color: rgb(0, 0, 255);">="/jerseywstest/rest/students/add"</span><span style="color: rgb(255, 0, 0);"> method</span><span style="color: rgb(0, 0, 255);">="post"</span><span style="color: rgb(0, 0, 255);">></span><span style="color: rgb(0, 0, 0);">
</span><span style="color: rgb(0, 0, 255);"><</span><span style="color: rgb(128, 0, 0);">input </span><span style="color: rgb(255, 0, 0);">type</span><span style="color: rgb(0, 0, 255);">="text"</span><span style="color: rgb(255, 0, 0);"> id</span><span style="color: rgb(0, 0, 255);">="name"</span><span style="color: rgb(255, 0, 0);"> name</span><span style="color: rgb(0, 0, 255);">="name"</span><span style="color: rgb(0, 0, 255);">/><</span><span style="color: rgb(128, 0, 0);">br</span><span style="color: rgb(0, 0, 255);">/></span><span style="color: rgb(0, 0, 0);">
</span><span style="color: rgb(0, 0, 255);"><</span><span style="color: rgb(128, 0, 0);">input </span><span style="color: rgb(255, 0, 0);">type</span><span style="color: rgb(0, 0, 255);">="text"</span><span style="color: rgb(255, 0, 0);"> id</span><span style="color: rgb(0, 0, 255);">="dept"</span><span style="color: rgb(255, 0, 0);"> name</span><span style="color: rgb(0, 0, 255);">="dept"</span><span style="color: rgb(0, 0, 255);">/><</span><span style="color: rgb(128, 0, 0);">br</span><span style="color: rgb(0, 0, 255);">/></span><span style="color: rgb(0, 0, 0);">
</span><span style="color: rgb(0, 0, 255);"><</span><span style="color: rgb(128, 0, 0);">input </span><span style="color: rgb(255, 0, 0);">type</span><span style="color: rgb(0, 0, 255);">= "submit"</span><span style="color: rgb(0, 0, 255);">/></span><span style="color: rgb(0, 0, 0);">
</span><span style="color: rgb(0, 0, 255);"></</span><span style="color: rgb(128, 0, 0);">form</span><span style="color: rgb(0, 0, 255);">></span><span style="color: rgb(0, 0, 0);">
</span><span style="color: rgb(0, 0, 255);"></</span><span style="color: rgb(128, 0, 0);">body</span><span style="color: rgb(0, 0, 255);">></span><span style="color: rgb(0, 0, 0);">
</span><span style="color: rgb(0, 0, 255);"></</span><span style="color: rgb(128, 0, 0);">html</span><span style="color: rgb(0, 0, 255);">></span>
提交后你在用list方法就可以看到数据的变化。
13.3) PUT和DELETE方法的测试
添加一个Junit测试类
<div class="cnblogs_code"><div class="cnblogs_code_toolbar"><span class="cnblogs_code_copy"><a target=_blank title="复制代码" style="color: rgb(61, 107, 167);"><img alt="复制代码" src="http://image55.360doc.com/DownloadImg/2012/10/2021/27665308_8.gif" style="border: 0px;" /></a></span></div><pre><span style="color: rgb(0, 128, 128);"> 1</span> <span style="color: rgb(0, 0, 255);">package</span><span style="color: rgb(0, 0, 0);"> net.jianxi.tutorials.jerseyws;
</span><span style="color: rgb(0, 128, 128);"> 2</span> <span style="color: rgb(0, 0, 0);">
</span><span style="color: rgb(0, 128, 128);"> 3</span> <span style="color: rgb(0, 0, 0);">
</span><span style="color: rgb(0, 128, 128);"> 4</span> <span style="color: rgb(0, 0, 0);"> </span><span style="color: rgb(0, 0, 255);">import</span><span style="color: rgb(0, 0, 0);"> javax.ws.rs.core.MultivaluedMap;
</span><span style="color: rgb(0, 128, 128);"> 5</span> <span style="color: rgb(0, 0, 0);">
</span><span style="color: rgb(0, 128, 128);"> 6</span> <span style="color: rgb(0, 0, 0);"> </span><span style="color: rgb(0, 0, 255);">import</span><span style="color: rgb(0, 0, 0);"> org.junit.Before;
</span><span style="color: rgb(0, 128, 128);"> 7</span> <span style="color: rgb(0, 0, 0);"> </span><span style="color: rgb(0, 0, 255);">import</span><span style="color: rgb(0, 0, 0);"> org.junit.BeforeClass;
</span><span style="color: rgb(0, 128, 128);"> 8</span> <span style="color: rgb(0, 0, 0);"> </span><span style="color: rgb(0, 0, 255);">import</span><span style="color: rgb(0, 0, 0);"> org.junit.Test;
</span><span style="color: rgb(0, 128, 128);"> 9</span> <span style="color: rgb(0, 0, 0);">
</span><span style="color: rgb(0, 128, 128);">10</span> <span style="color: rgb(0, 0, 0);"> </span><span style="color: rgb(0, 0, 255);">import</span><span style="color: rgb(0, 0, 0);"> com.sun.jersey.api.client.Client;
</span><span style="color: rgb(0, 128, 128);">11</span> <span style="color: rgb(0, 0, 0);"> </span><span style="color: rgb(0, 0, 255);">import</span><span style="color: rgb(0, 0, 0);"> com.sun.jersey.api.client.ClientResponse;
</span><span style="color: rgb(0, 128, 128);">12</span> <span style="color: rgb(0, 0, 0);"> </span><span style="color: rgb(0, 0, 255);">import</span><span style="color: rgb(0, 0, 0);"> com.sun.jersey.api.client.WebResource;
</span><span style="color: rgb(0, 128, 128);">13</span> <span style="color: rgb(0, 0, 0);"> </span><span style="color: rgb(0, 0, 255);">import</span><span style="color: rgb(0, 0, 0);"> com.sun.jersey.core.util.MultivaluedMapImpl;
</span><span style="color: rgb(0, 128, 128);">14</span> <span style="color: rgb(0, 0, 0);">
</span><span style="color: rgb(0, 128, 128);">15</span> <span style="color: rgb(0, 0, 0);"> </span><span style="color: rgb(0, 0, 255);">public</span><span style="color: rgb(0, 0, 0);"> </span><span style="color: rgb(0, 0, 255);">class</span><span style="color: rgb(0, 0, 0);"> RestWsDemoTest {
</span><span style="color: rgb(0, 128, 128);">16</span> <span style="color: rgb(0, 0, 0);"> </span><span style="color: rgb(0, 0, 255);">private</span><span style="color: rgb(0, 0, 0);"> String url </span><span style="color: rgb(0, 0, 0);">=</span><span style="color: rgb(0, 0, 0);"> </span><span style="color: rgb(0, 0, 0);">"</span><span style="color: rgb(0, 0, 0);">http://localhost:8080/jerseywstest/rest/students</span><span style="color: rgb(0, 0, 0);">"</span><span style="color: rgb(0, 0, 0);">;
</span><span style="color: rgb(0, 128, 128);">17</span> <span style="color: rgb(0, 0, 0);">
</span><span style="color: rgb(0, 128, 128);">18</span> <span style="color: rgb(0, 0, 0);"> @Test
</span><span style="color: rgb(0, 128, 128);">19</span> <span style="color: rgb(0, 0, 0);"> </span><span style="color: rgb(0, 0, 255);">public</span><span style="color: rgb(0, 0, 0);"> </span><span style="color: rgb(0, 0, 255);">void</span><span style="color: rgb(0, 0, 0);"> testDelete() {
</span><span style="color: rgb(0, 128, 128);">20</span> <span style="color: rgb(0, 0, 0);"> Client client </span><span style="color: rgb(0, 0, 0);">=</span><span style="color: rgb(0, 0, 0);"> Client.create();
</span><span style="color: rgb(0, 128, 128);">21</span> <span style="color: rgb(0, 0, 0);"> WebResource webResource </span><span style="color: rgb(0, 0, 0);">=</span><span style="color: rgb(0, 0, 0);"> client.resource(url </span><span style="color: rgb(0, 0, 0);">+</span><span style="color: rgb(0, 0, 0);"> </span><span style="color: rgb(0, 0, 0);">"</span><span style="color: rgb(0, 0, 0);">/delete/1</span><span style="color: rgb(0, 0, 0);">"</span><span style="color: rgb(0, 0, 0);">);
</span><span style="color: rgb(0, 128, 128);">22</span> <span style="color: rgb(0, 0, 0);"> ClientResponse response </span><span style="color: rgb(0, 0, 0);">=</span><span style="color: rgb(0, 0, 0);"> webResource.delete(ClientResponse.</span><span style="color: rgb(0, 0, 255);">class</span><span style="color: rgb(0, 0, 0);">);
</span><span style="color: rgb(0, 128, 128);">23</span> <span style="color: rgb(0, 0, 0);">
</span><span style="color: rgb(0, 128, 128);">24</span> <span style="color: rgb(0, 0, 0);"> System.out.println(</span><span style="color: rgb(0, 0, 0);">"</span><span style="color: rgb(0, 0, 0);">Response for delete request: </span><span style="color: rgb(0, 0, 0);">"</span><span style="color: rgb(0, 0, 0);"> </span><span style="color: rgb(0, 0, 0);">+</span><span style="color: rgb(0, 0, 0);"> response.getStatus());
</span><span style="color: rgb(0, 128, 128);">25</span> <span style="color: rgb(0, 0, 0);"> }
</span><span style="color: rgb(0, 128, 128);">26</span> <span style="color: rgb(0, 0, 0);">
</span><span style="color: rgb(0, 128, 128);">27</span> <span style="color: rgb(0, 0, 0);"> @Test
</span><span style="color: rgb(0, 128, 128);">28</span> <span style="color: rgb(0, 0, 0);"> </span><span style="color: rgb(0, 0, 255);">public</span><span style="color: rgb(0, 0, 0);"> </span><span style="color: rgb(0, 0, 255);">void</span><span style="color: rgb(0, 0, 0);"> testPut() {
</span><span style="color: rgb(0, 128, 128);">29</span> <span style="color: rgb(0, 0, 0);"> Client client </span><span style="color: rgb(0, 0, 0);">=</span><span style="color: rgb(0, 0, 0);"> Client.create();
</span><span style="color: rgb(0, 128, 128);">30</span> <span style="color: rgb(0, 0, 0);"> WebResource webResource </span><span style="color: rgb(0, 0, 0);">=</span><span style="color: rgb(0, 0, 0);"> client.resource(url </span><span style="color: rgb(0, 0, 0);">+</span><span style="color: rgb(0, 0, 0);"> </span><span style="color: rgb(0, 0, 0);">"</span><span style="color: rgb(0, 0, 0);">/put</span><span style="color: rgb(0, 0, 0);">"</span><span style="color: rgb(0, 0, 0);">);
</span><span style="color: rgb(0, 128, 128);">31</span> <span style="color: rgb(0, 0, 0);"> MultivaluedMap queryParams </span><span style="color: rgb(0, 0, 0);">=</span><span style="color: rgb(0, 0, 0);"> </span><span style="color: rgb(0, 0, 255);">new</span><span style="color: rgb(0, 0, 0);"> MultivaluedMapImpl();
</span><span style="color: rgb(0, 128, 128);">32</span> <span style="color: rgb(0, 0, 0);"> queryParams.add(</span><span style="color: rgb(0, 0, 0);">"</span><span style="color: rgb(0, 0, 0);">studentid</span><span style="color: rgb(0, 0, 0);">"</span><span style="color: rgb(0, 0, 0);">, </span><span style="color: rgb(0, 0, 0);">"</span><span style="color: rgb(0, 0, 0);">2</span><span style="color: rgb(0, 0, 0);">"</span><span style="color: rgb(0, 0, 0);">);
</span><span style="color: rgb(0, 128, 128);">33</span> <span style="color: rgb(0, 0, 0);"> queryParams.add(</span><span style="color: rgb(0, 0, 0);">"</span><span style="color: rgb(0, 0, 0);">name</span><span style="color: rgb(0, 0, 0);">"</span><span style="color: rgb(0, 0, 0);">, </span><span style="color: rgb(0, 0, 0);">"</span><span style="color: rgb(0, 0, 0);">nametest</span><span style="color: rgb(0, 0, 0);">"</span><span style="color: rgb(0, 0, 0);">);
</span><span style="color: rgb(0, 128, 128);">34</span> <span style="color: rgb(0, 0, 0);"> queryParams.add(</span><span style="color: rgb(0, 0, 0);">"</span><span style="color: rgb(0, 0, 0);">dept</span><span style="color: rgb(0, 0, 0);">"</span><span style="color: rgb(0, 0, 0);">, </span><span style="color: rgb(0, 0, 0);">"</span><span style="color: rgb(0, 0, 0);">depttest</span><span style="color: rgb(0, 0, 0);">"</span><span style="color: rgb(0, 0, 0);">);
</span><span style="color: rgb(0, 128, 128);">35</span> <span style="color: rgb(0, 0, 0);"> ClientResponse response </span><span style="color: rgb(0, 0, 0);">=</span><span style="color: rgb(0, 0, 0);"> webResource.queryParams(queryParams).put(ClientResponse.</span><span style="color: rgb(0, 0, 255);">class</span><span style="color: rgb(0, 0, 0);">, </span><span style="color: rgb(0, 0, 0);">"</span><span style="color: rgb(0, 0, 0);">foo:test</span><span style="color: rgb(0, 0, 0);">"</span><span style="color: rgb(0, 0, 0);">);
</span><span style="color: rgb(0, 128, 128);">36</span> <span style="color: rgb(0, 0, 0);"> System.out.println(</span><span style="color: rgb(0, 0, 0);">"</span><span style="color: rgb(0, 0, 0);">Response for put request: </span><span style="color: rgb(0, 0, 0);">"</span><span style="color: rgb(0, 0, 0);"> </span><span style="color: rgb(0, 0, 0);">+</span><span style="color: rgb(0, 0, 0);"> response.getStatus());
</span><span style="color: rgb(0, 128, 128);">37</span> <span style="color: rgb(0, 0, 0);"> }
</span><span style="color: rgb(0, 128, 128);">38</span> <span style="color: rgb(0, 0, 0);">}
</span><span style="color: rgb(0, 128, 128);">39</span> <span style="color: rgb(0, 0, 0);"> </span>