【问题标题】:JAX-RS With Embedded Jetty Service - Home URL具有嵌入式 Jetty 服务的 JAX-RS - 主页 URL
【发布时间】:2016-11-15 16:35:23
【问题描述】:

我已经下载了一个教程并对其进行了一些修改以满足我的需要(添加了 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>

我的码头服务类是这样的

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();
        }
    }
}

我的任何休息类如下所示

@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/ 以显示我的主页,或者实际上是 http://localhost:8080/RESTfulExample/rest/hello/ricki,它允许我与我的服务进行交互。

感谢您的宝贵时间。

【问题讨论】:

    标签: java web-services rest maven jetty


    【解决方案1】:

    如果您不想使用,则无需使用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>
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2014-04-25
      • 2011-03-02
      • 2013-06-06
      • 2012-10-15
      • 2013-12-18
      • 1970-01-01
      相关资源
      最近更新 更多