I got started by following the notes here as much as possible and then tried to boil the information down in order to give myself the most basic understanding of how these frameworks could be used in projects:
https://jersey.dev.java.net/use/getting-started.html
I created a new project in my favourite editor (IntelliJ IDEA) and then placed the following .jar files in my lib directory:
http://repo1.maven.org/maven2/asm/asm/3.1/asm-3.1.jar
http://download.java.net/maven/2/com/sun/grizzly/grizzly-framework/1.8.6.4/grizzly-framework-1.8.6.4.jar
http://download.java.net/maven/2/com/sun/grizzly/grizzly-http/1.8.6.4/grizzly-http-1.8.6.4.jar
http://download.java.net/maven/2/com/sun/grizzly/grizzly-http-webserver/1.8.6.4/grizzly-http-webserver-1.8.6.4.jar
http://download.java.net/maven/2/com/sun/grizzly/grizzly-servlet-webserver/1.8.6.4/grizzly-servlet-webserver-1.8.6.4.jar
http://www.java2s.com/Code/Jar/jboss-5.0.0.Beta2/Downloadjavaxservletjar.htm
http://download.java.net/maven/2/com/sun/jersey/jersey-bundle/1.0/jersey-bundle-1.0.jar
http://download.java.net/maven/2/javax/ws/rs/jsr311-api/0.9/jsr311-api-0.9.jar
In the src folder, I created the following files (note: you'll need to edit the line containing "YOUR.NAMESPACE.HERE" and replace it with your namespace:
LaunchApp.java:
import com.sun.grizzly.http.SelectorThread;
import com.sun.jersey.api.container.grizzly.GrizzlyWebContainerFactory;
import java.io.IOException;
import java.net.URI;
import java.util.HashMap;
import java.util.Map;
import javax.ws.rs.core.UriBuilder;
public class LaunchApp {
private static int getPort(int defaultPort) {
String port = System.getenv("JERSEY_HTTP_PORT");
if (null != port) {
try {
return Integer.parseInt(port);
} 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 SelectorThread startServer() throws IOException {
final Map<String, String> initParams = new HashMap<String, String>();
initParams.put("com.sun.jersey.config.property.packages",
"YOUR.NAMESPACE.HERE.resources");
System.out.println("Starting grizzly...");
SelectorThread threadSelector = GrizzlyWebContainerFactory.create(BASE_URI, initParams);
return threadSelector;
}
public static void main(String[] args) throws IOException {
SelectorThread threadSelector = startServer();
System.out.println("Jersey started");
System.in.read();
threadSelector.stopEndpoint();
System.exit(0);
}
}
I placed the next two files in a folder called resources (src/YOUR/NAMESPACE/resources), so the config class could find and register them:
HelloWorldResource.java:
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 "/helloworld"
@Path("/helloworld")
public class HelloWorldResource {
// 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 getClichedMessage() {
// Return some cliched textual content
return "Hello World";
}
}
GreetPersonResource.java:
import javax.ws.rs.GET;
import javax.ws.rs.Produces;
import javax.ws.rs.Path;
import javax.ws.rs.PathParam;
@Path("/greet/{username}")
public class GreetPersonResource {
@GET
// The Java method will produce content identified by the MIME Media
// type "text/plain"
@Produces("text/plain")
public String getUser(@PathParam("username") String userName) {
return "Hello " + userName + "!";
}
}
Once they're all in place and you have imported the jar folder into the project, you should be able to run LaunchApp and visit the following URIs:
http://localhost:9998/application.wadl
http://localhost:9998/helloworld
http://localhost:9998/greet/{YOUR_NAME_HERE}
And finally, I got myself up and running by looking at this page and downloading the project:
http://docs.sun.com/app/docs/doc/820-4867/ggqzb?a=view
I hope this helps someone get started quickly.
0 comments:
Post a Comment