本节课视频:http://v.youku.com/v_show/id_XMTYwOTM3NzUxNg==.htmlJersey内置WADL动态生成的功能,为WebService提供WADL描述文件,本文简单介绍它的使用方法。首先使用maven命令生成Jersey项目:mvn archetype:generate -DarchetypeArtifactId=jersey-quickstart-grizzly2 \-DarchetypeGroupId=org.glassfish.jersey.archetypes -DinteractiveMode=false \-DgroupId=org.weli -DartifactId=wadl-service -Dpackage=org.weli \-DarchetypeVersion=2.23.1生成的项目目录结构如下:cute:wadl-service weli$ tree.├── pom.xml└── src
├── main
│
└── java
│
└── org
│
└── weli
│
├── Main.java
│
└── MyResource.java
└── test
└── java
└── org
└── weli
└── MyResourceTest.java9 directories, 4 files运行代码:mvn exec:java生成的资源如下:package org.weli;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 org.weli;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);
}}测试代码中使用轻量级的Grizzly容器。此外,生成的Main类里面包含了一个main方法可以启动服务器:package org.weli;import org.glassfish.grizzly.http.server.HttpServer;import org.glassfish.jersey.grizzly2.httpserver.GrizzlyHttpServerFactory;import org.glassfish.jersey.server.ResourceConfig;import java.io.IOException;import java.net.URI;/** * Main class. * */public class Main {
// Base URI the Grizzly HTTP server will listen on
public static final String BASE_URI = "http://localhost:8080/myapp/";
/**
* Starts Grizzly HTTP server exposing JAX-RS resources defined in this application.
* @return Grizzly HTTP server.
*/
public static HttpServer startServer() {
// create a resource config that scans for JAX-RS resources and providers
// in org.weli package
final ResourceConfig rc = new ResourceConfig().packages("org.weli");
// create and start a new instance of grizzly http server
// exposing the Jersey application at BASE_URI
return GrizzlyHttpServerFactory.createHttpServer(URI.create(BASE_URI), rc);
}
/**
* Main method.
* @param args
* @throws IOException
*/
public static void main(String[] args) throws IOException {
final HttpServer server = startServer();
System.out.println(String.format("Jersey app started with WADL available at "
+ "%sapplication.wadl\nHit enter to stop it...", BASE_URI));
System.in.read();
server.stop();
}}启动这个服务后的日志输出:Jersey app started with WADL available at http://localhost:8080/myapp/application.wadl访问WADL输出如下:cute:Desktop weli$ curl http://localhost:8080/myapp/application.wadl<?xml version="1.0" encoding="UTF-8" standalone="yes"?>
参考资料:[1] https://jersey.java.net/documentation/latest/getting-started.html#new-from-archetype