package my.restfulwork.jerseyone;
import javax.ws.rs.GET;
import javax.ws.rs.Path;
import javax.ws.rs.Produces;
// The Java class will be hosted at the URI path "/myresource"
@Path("/myresource")
public class MyResource {
// TODO: update the class to suit your needs
// The Java method will process HTTP GET requests
@GET
// The Java method will produce content identified by the MIME Media
// type "text/plain"
@Produces("text/plain")
public String getIt() {
return "Got it!";
}
}
Main.java
package my.restfulwork.jerseyone;
import com.sun.jersey.api.container.grizzly2.GrizzlyServerFactory;
import com.sun.jersey.api.core.PackagesResourceConfig;
import com.sun.jersey.api.core.ResourceConfig;
import org.glassfish.grizzly.http.server.HttpServer;
import javax.ws.rs.core.UriBuilder;
import java.io.IOException;
import java.net.URI;
public class Main {
private static int getPort(int defaultPort) {
//grab port from environment, otherwise fall back to default port 9998
String httpPort = System.getProperty("jersey.test.port");
if (null != httpPort) {
try {
return Integer.parseInt(httpPort);
} catch (NumberFormatException e) {
}
}
return defaultPort;
}
private static URI getBaseURI() {
return UriBuilder.fromUri("http://localhost/").port(getPort(9998)).build();
}
public static final URI BASE_URI = getBaseURI();
protected static HttpServer startServer() throws IOException {
ResourceConfig resourceConfig = new PackagesResourceConfig("my.restfulwork.jerseyone");
System.out.println("Starting grizzly2...");
return GrizzlyServerFactory.createHttpServer(BASE_URI, resourceConfig);
}
public static void main(String[] args) throws IOException {
// Grizzly 2 initialization
HttpServer httpServer = 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();
httpServer.stop();
}
}
MainTest.java
package my.restfulwork.jerseyone;
import org.glassfish.grizzly.http.server.HttpServer;
import com.sun.jersey.core.header.MediaTypes;
import com.sun.jersey.api.client.Client;
import com.sun.jersey.api.client.WebResource;
import junit.framework.TestCase;
public class MainTest extends TestCase {
private HttpServer httpServer;
private WebResource r;
public MainTest(String testName) {
super(testName);
}
@Override
protected void setUp() throws Exception {
super.setUp();
//start the Grizzly2 web container
httpServer = Main.startServer();
// create the client
Client c = Client.create();
r = c.resource(Main.BASE_URI);
}
@Override
protected void tearDown() throws Exception {
super.tearDown();
httpServer.stop();
}
/**
* Test to see that the message "Got it!" is sent in the response.
*/
public void testMyResource() {
String responseMsg = r.path("myresource").get(String.class);
assertEquals("Got it!", responseMsg);
}
/**
* Test if a WADL document is available at the relative path
* "application.wadl".
*/
public void testApplicationWadl() {
String serviceWadl = r.path("application.wadl").
accept(MediaTypes.WADL).get(String.class);
assertTrue(serviceWadl.length() > 0);
}
}