具有嵌入式 Jetty 服务的 JAX-RS - 主页 URL

JAX-RS With Embedded Jetty Service - Home URL

我已经下载了一个教程并根据我的需要对其进行了一些修改(添加了maven)

我只是想知道是什么让服务从特定主页开始 - 当我 运行 我的服务默认为以下

http://localhost:8080/RESTfulExample/WEB-INF/classes/com/ricki/test/JettyService.java

我的web.xml看起来如下

<web-app id="WebApp_ID" version="2.4"
    xmlns="http://java.sun.com/xml/ns/j2ee" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
    xsi:schemaLocation="http://java.sun.com/xml/ns/j2ee 
    http://java.sun.com/xml/ns/j2ee/web-app_2_4.xsd">
    <display-name>Restful Web Application</display-name>
    <servlet>
        <servlet-name>jersey-serlvet</servlet-name>
        <servlet-class>org.glassfish.jersey.servlet.ServletContainer</servlet-class>
        <init-param>
            <param-name>jersey.config.server.provider.packages</param-name>
            <param-value>com.ricki.test</param-value>
        </init-param>
        <load-on-startup>1</load-on-startup>
    </servlet>
    <servlet-mapping>
        <servlet-name>jersey-serlvet</servlet-name>
        <url-pattern>/rest/*</url-pattern>
    </servlet-mapping>
</web-app>

我的码头服务 class 看起来像这样

import com.google.common.util.concurrent.AbstractIdleService;
import java.lang.management.ManagementFactory;
import org.eclipse.jetty.jmx.MBeanContainer;
import org.eclipse.jetty.server.Server;
import org.eclipse.jetty.util.resource.Resource;
import org.eclipse.jetty.webapp.WebAppContext;

public class JettyService extends AbstractIdleService
{
    private Server server;

    @Override
    protected void startUp() throws Exception
    {
        server = new Server(8080);
        MBeanContainer mbContainer = new MBeanContainer(ManagementFactory.getPlatformMBeanServer());
        server.addBean(mbContainer);
        Resource resource = Resource.newClassPathResource("/webapp");
        WebAppContext context = new WebAppContext(resource.getURL().toExternalForm(), "/ricki-test/");
        server.setHandler(context);
        server.start();
    }

    @Override
    protected void shutDown() throws Exception
    {
        try
        {
            server.stop();
            server.join();
        }
        finally
        {
            server.destroy();
        }
    }
}

任何我的休息class看起来如下

@Path("/hello")
public class HelloWorldService
{
    private final Logger logger = Logger.getLogger(HelloWorldService.class);

    @GET
    @Path("/{param}")
    public Response getMsg(@PathParam("param") String msg)
    {
        logger.info("Received message " + msg);
        String output = "Hi : " + msg;
        return Response.status(200).entity(output).build();
    }
}

理想情况下,我的主页将设置为 http://localhost:8080/RESTfulExample/ whcih displays my home page or in fact http://localhost:8080/RESTfulExample/rest/hello/ricki,这样我就可以与我的服务进行交互。

感谢您的宝贵时间。

如果您不想,则无需使用 web.xml 文件。如果您使用的是嵌入式 Jetty 服务器,您可以手动连接到 Jersey:

public static void main(String[] args) throws Exception {
    ServletContextHandler context = new ServletContextHandler(ServletContextHandler.SESSIONS);
    context.setContextPath("/");

    Server jettyServer = new Server(8080);
    jettyServer.setHandler(context);

    ServletHolder jerseyServlet = context.addServlet(
         org.glassfish.jersey.servlet.ServletContainer.class, "/*");
    jerseyServlet.setInitOrder(0);

    // Tells the Jersey Servlet which REST service/class to load.
    jerseyServlet.setInitParameter(
       "jersey.config.server.provider.classnames",
       EntryPoint.class.getCanonicalName());

    try {
        jettyServer.start();
        jettyServer.join();
    } finally {
        jettyServer.destroy();
    }
}

示例来自:http://www.nikgrozev.org/2014/10/16/rest-with-embedded-jetty-and-jersey-in-a-single-jar-step-by-step/

您还可以使用 jersey-container-jetty-http 依赖项:

<dependency>
    <groupId>org.glassfish.jersey.containers</groupId>
    <artifactId>jersey-container-jetty-http</artifactId>
    <version>2.23.1</version>
</dependency>

这允许你做:

URI baseUri = UriBuilder.fromUri("http://localhost/").port(9998).build();
ResourceConfig config = new ResourceConfig(MyResource.class);
Server server = JettyHttpContainerFactory.createServer(baseUri, config);

如果你真的想使用 web.xml,你应该以不同的方式访问它:

Server server = new Server(8080);

String rootPath = SimplestServer.class.getClassLoader().getResource(".").toString();
WebAppContext webapp = new WebAppContext(rootPath + "../../src/main/webapp", "");
server.setHandler(webapp);

server.start();
server.join();

另请参阅:Configure embedded jetty with web.xml?

那时,使用 Jetty maven 插件会更容易,它会捆绑您的 war 文件并将其部署到本地 Jetty 服务器:

        <plugin>
            <groupId>org.eclipse.jetty</groupId>
            <artifactId>jetty-maven-plugin</artifactId>
            <version>9.3.6.v20151106</version>
            <configuration>
                <scanTargets>
                    <scanTarget>${project.basedir}/src/main</scanTarget>
                    <scanTarget>${project.basedir}/src/test</scanTarget>
                </scanTargets>
                <webAppConfig>
                    <contextPath>/${project.artifactId}-${project.version}</contextPath>
                </webAppConfig>
                <contextPath>${project.artifactId}</contextPath>
            </configuration>
        </plugin>