我使用Jetty和Jersey创建了一个应用程序。这只是一个标准的webapp:
web.xml中:
rest.service
com.sun.jersey.spi.spring.container.servlet.SpringServlet
com.sun.jersey.config.property.resourceConfigClass
com.sun.jersey.api.core.PackagesResourceConfig
com.sun.jersey.config.property.packages
your.package.with.jersey.resources
1
rest.service
/service/*
休息资源:
package your.package.with.jersey.resources;
import javax.ws.rs.GET;
import javax.ws.rs.Path;
import javax.ws.rs.Produces;
import javax.ws.rs.core.Context;
import javax.ws.rs.core.MediaType;
import javax.ws.rs.core.SecurityContext;
@Path("login")
public class LoginResource {
@Context
private SecurityContext security;
@GET
@Produces(MediaType.APPLICATION_XML)
public String login() {
String email = security.getUserPrincipal().getName();
return "ok";
}
}
码头起动机:
public class StartJetty {
public static void main(String[] args) throws Exception {
Server server = new Server();
SocketConnector connector = new SocketConnector();
// Set some timeout options to make debugging easier.
connector.setMaxIdleTime(1000 * 60 * 60);
connector.setSoLingerTime(-1);
connector.setPort(8080);
server.setConnectors(new Connector[] { connector });
WebAppContext bb = new WebAppContext();
bb.setServer(server);
bb.setContextPath("/");
bb.setWar("src/main/webapp");
server.addHandler(bb);
try {
System.out.println(">>> STARTING EMBEDDED JETTY SERVER, PRESS ANY KEY TO STOP");
server.start();
while (System.in.available() == 0) {
Thread.sleep(5000);
}
server.stop();
server.join();
} catch (Exception e) {
e.printStackTrace();
System.exit(100);
}
}
}
pom.xml中:
org.mortbay.jetty
jetty
org.mortbay.jetty
jetty-util
org.mortbay.jetty
jetty-management
com.sun.jersey
jersey-server
com.sun.jersey.contribs
jersey-spring
javax.ws.rs
jsr311-api
com.sun.jersey
jersey-test-framework
test
com.sun.grizzly
grizzly-servlet-webserver
(...)
org.mortbay.jetty
maven-jetty-plugin
希望这些片段指向正确的方向。