enc test pom.xml jar snap private fis lai
/* 新建 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 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 2 xsi: schemaLocation = "http://maven.apache.org/POM/4.0.0 http://maven.apache.org/maven-v4_0_0.xsd" > 3 4 4.0.0 5 6 my.restful 7 my - first - service 8 jar 9 1.0 - SNAPSHOT 10 my - first - service 11 12 13 14 15 org.glassfish.jersey 16 jersey - bom 17 $ {
- jersey.version
- }
- 18 pom 19 import 20 21 22 23 24 25 26 org.glassfish.jersey.containers 27 jersey - container - grizzly2 - http 28 29 this to get JSON support: 30 31 org.glassfish.jersey.media 32 jersey - media - moxy 33 34-->35 36 junit 37 junit 38 4.9 39 test 40 41 42 43 44 45 46 org.apache.maven.plugins 47 maven - compiler - plugin 48 2.5.1 49 true 50 51 1.7 52 1.7 53 54 55 56 org.codehaus.mojo 57 exec - maven - plugin 58 1.2.1 59 60 61 62 java 63 64 65 66 67 my.restful.Main 68 69 70 71 72 73 74 2.22.1 75 UTF - 8 76 77
RESTful WebService 入门实例
来源: http://www.bubuko.com/infodetail-2217647.html