RESTful WebService 入门实例
![](https://images2017.cnblogs.com/blog/1203307/201707/1203307-20170725224131998-53559453.png)
/* 新建MavenProject,使用以下代码,创建类和POM文件。
使用命令行切换到Project根目录,运行mvn package(或者,选中pom.xml 文件右键单击 > run as maven build > 在goals中填入 "clean package") , mvn exec:java 或 对于包含main方法的类my.restful.Main, run as Java application。
,即可启动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 on 16 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 providers 24 // in my.restful package 25 final ResourceConfig rc = new ResourceConfig().packages("my.restful"); 26 27 // create and start a new instance of grizzly http server 28 // exposing the Jersey application at BASE_URI 29 return GrizzlyHttpServerFactory.createHttpServer(URI.create(BASE_URI), rc); 30 } 31 32 /** 33 * Main method. 34 * @param args 35 * @throws IOException 36 */ 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>
Author:LuffyStory
Origin:http://www.cnblogs.com/luffystory/