menu
more_vert
RESTful WebService 入门实例

作者:dayu

*/ @Path("myresource") public class MyResource { /** * Method handling HTTP GET requests. The returned object will be sent * to the client as "text/plain" media type. * * @return String that will be returned as a text/plain response. */ @GET @Produces(MediaType.TEXT_PLAIN) public String getIt() { return "Got it!"; } }
package my.restful;

import javax.ws.rs.client.Client;
import javax.ws.rs.client.ClientBuilder;
import javax.ws.rs.client.WebTarget;

import org.glassfish.grizzly.http.server.HttpServer;

import org.junit.After;
import org.junit.Before;
import org.junit.Test;
import static org.junit.Assert.assertEquals;

public class MyResourceTest {

    private HttpServer server;
    private WebTarget target;

    @Before
    public void setUp() throws Exception {
        // start the server
        server = Main.startServer();
        // create the client
        Client c = ClientBuilder.newClient();

        // uncomment the following line if you want to enable
        // support for JSON in the client (you also have to uncomment
        // dependency on jersey-media-json module in pom.xml and Main.startServer())
        // --
        // c.configuration().enable(new org.glassfish.jersey.media.json.JsonJaxbFeature());

        target = c.target(Main.BASE_URI);
    }

    @After
    public void tearDown() throws Exception {
        server.stop();
    }

    /**
     * Test to see that the message "Got it!" is sent in the response.
     */
    @Test
    public void testGetIt() {
        String responseMsg = target.path("myresource").request().get(String.class);
        assertEquals("Got it!", responseMsg);
    }
}

 1 <project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
 2          xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/maven-v4_0_0.xsd">
 3 
 4     <modelVersion>4.0.0</modelVersion>
 5 
 6     <groupId>my.restful</groupId>
 7     <artifactId>my-first-service</artifactId>
 8     <packaging>jar</packaging>
 9     <version>1.0-SNAPSHOT</version>
10     <name>my-first-service</name>
11 
12     <dependencyManagement>
13         <dependencies>
14             <dependency>
15                 <groupId>org.glassfish.jersey</groupId>
16                 <artifactId>jersey-bom</artifactId>
17                 <version>${jersey.version}</version>
18                 <type>pom</type>
19                 <scope>import</scope>
20             </dependency>
21         </dependencies>
22     </dependencyManagement>
23 
24     <dependencies>
25         <dependency>
26             <groupId>org.glassfish.jersey.containers</groupId>
27             <artifactId>jersey-container-grizzly2-http</artifactId>
28         </dependency>
29         <!-- uncomment this to get JSON support:
30          <dependency>
31             <groupId>org.glassfish.jersey.media</groupId>
32             <artifactId>jersey-media-moxy</artifactId>
33         </dependency>
34         -->
35         <dependency>
36             <groupId>junit</groupId>
37             <artifactId>junit</artifactId>
38             <version>4.9</version>
39             <scope>test</scope>
40         </dependency>
41     </dependencies>
42 
43     <build>
44         <plugins>
45             <plugin>
46                 <groupId>org.apache.maven.plugins</groupId>
47                 <artifactId>maven-compiler-plugin</artifactId>
48                 <version>2.5.1</version>
49                 <inherited>true</inherited>
50                 <configuration>
51                     <source>1.7</source>
52                     <target>1.7</target>
53                 </configuration>
54             </plugin>
55             <plugin>
56                 <groupId>org.codehaus.mojo</groupId>
57                 <artifactId>exec-maven-plugin</artifactId>
58                 <version>1.2.1</version>
59                 <executions>
60                     <execution>
61                         <goals>
62                             <goal>java</goal>
63                         </goals>
64                     </execution>
65                 </executions>
66                 <configuration>
67                     <mainClass>my.restful.Main</mainClass>
68                 </configuration>
69             </plugin>
70         </plugins>
71     </build>
72 
73     <properties>
74         <jersey.version>2.22.1</jersey.version>
75         <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
76     </properties>
77 </project>

RESTful WebService 入门实例

原文地址:http://www.cnblogs.com/luffystory/p/7208597.html