与整合码头JAX-RS-新泽西(Integrating Jetty with JAX-RS-Jers

2019-06-23 10:45发布

穷举搜索网络和#2之后,我还是坚持了试图找出如何整合泽西与码头提供的Restlet风格界面。

我有我的Jetty服务器启动和运行,因此,似乎新泽西州很容易使用为好,没有人知道如何配合两个在一起吗? 任何具体的联系将帮助 - 我是小新servlet编程为好。

Answer 1:

我创建使用Jetty和新泽西州而回的应用程序。 这只是一个标准的web应用程序真正做到:

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

码头首发:

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>

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



Answer 2:

你可能想看看Dropwizard的码头,泽西岛,杰克逊的logback / Log4j的/ JUL / CJL和JDBI间外的即装即用的集成,均能仪表与Yammer的指标。



文章来源: Integrating Jetty with JAX-RS-Jersey