RESTful WebService 入門執行個體

來源:互聯網
上載者:User

標籤:private   for   fine   string   jar   命令   param   apache   snapshot   

 

/* 建立MavenProject,使用以下代碼,建立類和POM檔案。
使用命令列切換到Project根目錄,運行mvn package , mvn exec:java,即可啟動RESTful service。
在瀏覽器中輸入http://localhost:8080/myapp/myresource/,
此時會顯示內容: Got it!

---Java RESTful Web Service 實戰 */




package my.restful; 2 3 import org.glassfish.grizzly.http.server.HttpServer; 4 import org.glassfish.jersey.grizzly2.httpserver.GrizzlyHttpServerFactory; 5 import org.glassfish.jersey.server.ResourceConfig; 6 7 import java.io.IOException; 8 import java.net.URI; 9 10 /**11 * Main class.12 *13 */14 public class Main {15 // Base URI the Grizzly HTTP server will listen on16 public static final String BASE_URI = "http://localhost:8080/myapp/";17 18 /**19 * Starts Grizzly HTTP server exposing JAX-RS resources defined in this application.20 * @return Grizzly HTTP server.21 */22 public static HttpServer startServer() {23 // create a resource config that scans for JAX-RS resources and providers24 // in my.restful package25 final ResourceConfig rc = new ResourceConfig().packages("my.restful");26 27 // create and start a new instance of grizzly http server28 // exposing the Jersey application at BASE_URI29 return GrizzlyHttpServerFactory.createHttpServer(URI.create(BASE_URI), rc);30 }31 32 /**33 * Main method.34 * @param args35 * @throws IOException36 */37 public static void main(String[] args) throws IOException {38 final HttpServer server = startServer();39 System.out.println(String.format("Jersey app started with WADL available at "40 + "%sapplication.wadl\nHit enter to stop it...", BASE_URI));41 System.in.read();42 server.stop();43 }44 }
package my.restful;import javax.ws.rs.GET;import javax.ws.rs.Path;import javax.ws.rs.Produces;import javax.ws.rs.core.MediaType;/** * Root resource (exposed at "myresource" path) */@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 入門執行個體

相關文章

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.