将Jetty与JAX-RS-Jersey集成

时间:2012-04-06 19:02:04

标签: java servlets jetty jersey jax-rs

在详尽搜索网络和Stackoverflow之后,我仍然在努力弄清楚如何整合Jersey和Jetty提供的RESTlet样式界面。

我的Jetty服务器已启动并运行,因此Jersey似乎也很容易使用,有没有人知道如何将两者结合在一起?任何具体的链接都会有所帮助 - 我对servlet编程也有点新意。

2 个答案:

答案 0 :(得分:26)

我使用Jetty和Jersey创建了一个应用程序。它只是一个标准的webapp:

的web.xml:

<servlet>
    <servlet-name>rest.service</servlet-name>
    <servlet-class>
        com.sun.jersey.spi.spring.container.servlet.SpringServlet</servlet-class>
    <init-param>
        <param-name>com.sun.jersey.config.property.resourceConfigClass</param-name>
        <param-value>com.sun.jersey.api.core.PackagesResourceConfig</param-value>
    </init-param>
    <init-param>
        <param-name>com.sun.jersey.config.property.packages</param-name>
        <param-value>your.package.with.jersey.resources</param-value>
    </init-param>
    <load-on-startup>1</load-on-startup>
</servlet>

<servlet-mapping>
    <servlet-name>rest.service</servlet-name>
    <url-pattern>/service/*</url-pattern>
</servlet-mapping>

休息资源:

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";
    }
}

Jetty发起人:

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:

<!-- Jetty -->
<dependency>
    <groupId>org.mortbay.jetty</groupId>
    <artifactId>jetty</artifactId>
</dependency>
<dependency>
    <groupId>org.mortbay.jetty</groupId>
    <artifactId>jetty-util</artifactId>
</dependency>
<dependency>
    <groupId>org.mortbay.jetty</groupId>
    <artifactId>jetty-management</artifactId>
</dependency>

<!-- Jersey (JAX-RS) -->
<dependency>
    <groupId>com.sun.jersey</groupId>
    <artifactId>jersey-server</artifactId>
</dependency>
<dependency>
    <groupId>com.sun.jersey.contribs</groupId>
    <artifactId>jersey-spring</artifactId>
</dependency>
<dependency>
    <groupId>javax.ws.rs</groupId>
    <artifactId>jsr311-api</artifactId>
</dependency>
<dependency>
    <groupId>com.sun.jersey</groupId>
    <artifactId>jersey-test-framework</artifactId>
    <scope>test</scope>
</dependency>
<dependency>
    <groupId>com.sun.grizzly</groupId>
    <artifactId>grizzly-servlet-webserver</artifactId>
</dependency>

(...)

<plugin>
    <groupId>org.mortbay.jetty</groupId>
    <artifactId>maven-jetty-plugin</artifactId>     
</plugin>

希望这些片段能指向正确的方向。

答案 1 :(得分:16)

您可能希望查看Dropwizard以便在Jetty,Jersey,Jackson,Logback / Log4j / JUL / CJL和JDBI之间进行开箱即用的集成,所有这些都很好地配备了Yammer Metrics。