Pax-Web Jetty-Bundle发布Jersey App

时间:2014-11-17 12:42:52

标签: jetty osgi web.xml apache-felix pax-web

我试图在Apache Felix上部署Jersey应用程序。我安装了这些软件包:

    ID|State      |Level|Name
    0|Active     |    0|System Bundle (4.4.1)
    1|Active     |    1|Apache Felix Bundle Repository (2.0.2)
    2|Active     |    1|Apache Felix Gogo Command (0.14.0)
    4|Active     |    1|Apache Felix Gogo Shell (0.10.0)
   10|Active     |    1|Apache Felix Configuration Admin Service (1.2.8)
   11|Active     |    1|Apache Felix EventAdmin (1.2.10)
   35|Active     |    1|Apache Felix Gogo Runtime (0.12.1)
   90|Installed  |    1|Apache Felix Log Service (1.0.0)
   91|Resolved   |    1|OSGi R4 Compendium Bundle (4.0.0)
   92|Installed  |    1|Apache Felix Declarative Services (1.6.0)
   94|Installed  |    1|Apache Felix Web Management Console (3.1.2)
   95|Installed  |    1|Apache Felix iPOJO WebConsole Plugins (1.6.0)
   96|Resolved   |    1|Apache Felix Shell Service (1.4.2)
  119|Installed  |    1|Servlet 2.1 API (1.0.0)
  144|Active     |    1|OSGi JAX-RS Connector (4.1.0.201407081443)
  147|Active     |    1|jersey-all (2.10.1)
  157|Active     |    1|OPS4J Pax Web - Jetty Bundle (4.0.0)
  158|Active     |    1|OPS4J Pax Web - Runtime (4.0.0)
  163|Active     |    1|http://repo1.maven.org/maven2/org/slf4j/slf4j-api/1.7.7/slf4j-api-1.7.7-sources.jar (0.0.0)
  165|Active     |    1|Java Servlet API (3.0.1)
  167|Active     |    1|OPS4J Pax Logging - API (1.8.1)
  170|Active     |    1|Apache Felix Dependency Manager (3.2.0)
  171|Active     |    1|OPS4J Pax Web - Extender - WAR (4.0.0)
  174|Active     |    1|Apache Felix Dependency Manager Runtime (3.2.0)
  177|Active     |    1|ASM (5.0.3)
  178|Active     |    1|ASM commons classes (5.0.3)
  179|Active     |    1|ASM Tree class visitor (5.0.3)
  182|Active     |    1|Apache XBean OSGI Bundle Utilities (3.18.0)
  183|Active     |    1|Apache XBean :: Classpath Resource Finder (3.18.0)
  216|Active     |    1|Auth Test (2.0.0.SNAPSHOT)

最后一个,Auth Test是我要发布的Jersey应用程序。我可以安装捆绑包并启动它。当我开始它时,我得到这些日志:

g! start 216
Started Activator
org.ops4j.pax.web.pax-web-extender-war[org.ops4j.pax.web.extender.war.internal.WebEventDispatcher] : Sending web event WebEvent [replay=false, type=DEPLOYING, bundle=platform-component-web-rest-service-authentication [216], extenderBundle=org.ops4j.pax.web.pax-web-extender-war [171], cause=null, timestamp=1416226629360, contextPath=/authentication, collisionIds=null, httpService=null, httpContext=null] for bundle platform-component-web-rest-service-authentication
org.ops4j.pax.web.pax-web-extender-war[org.ops4j.pax.web.extender.war.internal.WebEventDispatcher] : org/osgi/service/web/DEPLOYING
org.ops4j.pax.web.pax-web-extender-war[org.ops4j.pax.web.extender.war.internal.parser.WebAppParser] : version found in web.xml - 3.0
org.ops4j.pax.web.pax-web-extender-war[org.ops4j.pax.web.extender.war.internal.parser.WebAppParser] : metadata-complete is: false
org.ops4j.pax.web.pax-web-extender-war[org.ops4j.pax.web.extender.war.internal.parser.WebAppParser] : scanning for ServletContainerInitializers
org.ops4j.pax.web.pax-web-extender-war[org.ops4j.pax.web.extender.war.internal.parser.WebAppParser] : found container initializers by SafeServiceLoader ... skip the old impl. 
org.ops4j.pax.web.pax-web-extender-war[org.ops4j.pax.web.extender.war.internal.Activator] : Scheduling start of extension for bundle platform-component-web-rest-service-authentication asynchronously
org.ops4j.pax.web.pax-web-extender-war[org.ops4j.pax.web.extender.war.internal.WebAppPublisher] : Publishing web application [WebApp{displayName=Authentication Manager,contextName=authentication,m_httpContext=null}]

我猜整个问题都在web.xml背后,如下所示:

<?xml version="1.0" encoding="UTF-8"?>
<web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
    xmlns="http://java.sun.com/xml/ns/javaee"
    xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_3_0.xsd"
    id="platform-component-web-rest-manager-authentication" version="3.0">


    <display-name>Authentication Manager</display-name>


    <!-- ****************************************************************** -->
    <!-- SERVLETS -->
    <!-- ****************************************************************** -->         
    <servlet>
        <servlet-name>wd-platform-product-web</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.mycompany.platform.component.web.rest;
                com.mycompany.wd.platform.component.web.rest,
            </param-value>
        </init-param>
        <init-param>
            <param-name>jersey.config.server.provider.scanning.recursive</param-name>
            <param-value>true</param-value>
        </init-param>
        <init-param>
            <param-name>jersey.config.server.provider.classnames</param-name>
            <param-value>
                com.wordnik.swagger.jersey.listing.ApiListingResourceJSON;
                com.wordnik.swagger.jersey.listing.JerseyApiDeclarationProvider;
                com.wordnik.swagger.jersey.listing.JerseyResourceListingProvider
            </param-value>
        </init-param>
        <load-on-startup>1</load-on-startup>
    </servlet>

    <servlet-mapping>
        <servlet-name>wd-platform-product-web</servlet-name>
        <url-pattern>/rest/2.0.0/*</url-pattern>
    </servlet-mapping>


    <servlet>
        <servlet-name>swagger-default-jaxrs-config</servlet-name>
        <servlet-class>com.wordnik.swagger.jaxrs.config.DefaultJaxrsConfig</servlet-class>
        <init-param>
            <param-name>api.version</param-name>
            <param-value>2.0.0</param-value>
        </init-param>
        <init-param>
            <param-name>swagger.api.basepath</param-name>
            <param-value>/rest/2.0.0/</param-value>
        </init-param>
        <load-on-startup>2</load-on-startup>
    </servlet>

</web-app>

我的激活类是标准模型:

import java.util.HashMap;
import javax.servlet.ServletContextEvent;
import javax.servlet.ServletContextListener;
import org.osgi.framework.BundleActivator;
import org.osgi.framework.BundleContext;
import org.osgi.framework.ServiceReference;
import org.osgi.service.event.Event;
import org.osgi.service.event.EventAdmin;

/**
 * This is to make sure we signal the application has been deployed/un-deployed
 * via the OSGi EventAdmin service.
 *
 * @author Jakub Podlesak (jakub.podlesak at oracle.com)
 */
public class WebAppContextListener implements BundleActivator, ServletContextListener {

    static EventAdmin ea;

    BundleContext bc;
    ServiceReference eaRef;

    synchronized static EventAdmin getEa() {
        return ea;
    }

    synchronized static void setEa(EventAdmin ea) {
        WebAppContextListener.ea = ea;
    }

    @Override
    public void contextInitialized(final ServletContextEvent sce) {
        if (getEa() != null) {
            final String contextPath = sce.getServletContext().getContextPath();
            getEa().sendEvent(new Event("jersey/test/DEPLOYED",new HashMap<String, String>(){{
                put("context-path", contextPath);}}));
        }
    }

    @Override
    public void contextDestroyed(final ServletContextEvent sce) {
        if (getEa() != null) {
            getEa().sendEvent(new Event("jersey/test/UNDEPLOYED",new HashMap<String, String>(){{put("context-path", sce.getServletContext().getContextPath());}}));
        }
    }

    @Override
    public void start(BundleContext context) throws Exception {
        System.out.println("Started Activator");
        bc = context;
        eaRef = bc.getServiceReference(EventAdmin.class.getName());
        if (eaRef != null) {
            setEa((EventAdmin)bc.getService(eaRef));
        }
    }

    @Override
    public void stop(BundleContext context) throws Exception {
        if (eaRef != null) {
            setEa(null);
            bc.ungetService(eaRef);
        }
    }
}

我的主要REST服务看起来像这样:

@Path("/service/authentication")
@Api(value = "/service/authentication", description = "Authentication Service")
public class AuthenticationServiceRestService extends RestService
{

    ServiceLayer<Request<?>> serviceLayer;
    AuthenticationLayer<Request<?>> authenticationLayer;
    ExceptionLayer<Request<?>> exceptionLayer;


    public AuthenticationServiceRestService() throws AuthenticationServiceRestServiceException
    {
        this.serviceLayer = new ServiceLayer<Request<?>>();

        try
        {
            this.authenticationLayer = new AuthenticationLayer<Request<?>>(this.serviceLayer);
        }
        catch (AuthenticationServiceException exception)
        {
            throw new AuthenticationServiceRestServiceException(exception.getMessage(), exception);
        }

        this.exceptionLayer = new ExceptionLayer<Request<?>>(this.authenticationLayer);
    }


    @POST
    @Path("/auth")
    @Consumes(MediaType.APPLICATION_JSON)
    @Produces(MediaType.APPLICATION_JSON)
    @ApiOperation(value = "Get authentication token.", response = AuthenticationTokenFacade.class)
    public Response getAuthenticationToken(@ApiParam(value = "Authentication Data", required = true) AuthenticationRequestFacade authenticationRequestFacade)
    {
        System.out.println("auth request");
        GetAuthenticationTokenJsonRequest getAuthenticationTokenJsonRequest = new GetAuthenticationTokenJsonRequest(authenticationRequestFacade);

        return exceptionLayer.execute(getAuthenticationTokenJsonRequest);
    }

当我向http://localhost:8080/authentication/rest/2.0.0/service/authentication/auth发出POST请求时,我得到404.我甚至不知道我的整个服务是否在jetty上发布。我怎样才能做到这一点?当我发出请求时,Jetty会在大量日志的中间显示它:

org.ops4j.pax.logging.pax-logging-api[org.ops4j.pax.web.service.spi.model.ServerModel] : Matching [/authentication/rest/2.0.0/service/authentication/auth]...
org.ops4j.pax.logging.pax-logging-api[org.ops4j.pax.web.service.spi.model.ServerModel] : Path [/authentication/rest/2.0.0/service/authentication/auth] does not match any context

任何帮助完成这项工作都会很棒。 提前致谢并对这个长期问题感到抱歉,但我必须确保提供所有信息。

1 个答案:

答案 0 :(得分:2)

首先,感谢详细的问题,这样做更好,唯一缺少的是您正在使用的Pax Web版本;-)
其次,您可能有兴趣将Karaf与Pax Web一起使用。通过发布

,很容易在Karaf中安装Pax-Web
feature:install war

命令在Karaf中安装Pax Web。现在为什么我一开始就建议这样做,因为你说你不知道应用程序是否真的可用。为此,Karaf shell中有两个命令可以告诉您它是否存在。 一个是

web:list

另一个是

http:list

任何一个都会给你一个列表:a)部署的Web应用程序包或b)注册的servlet。

这可以让您更好地了解已部署的应用程序 现在,我不能得到的是,为什么你的WebApplicationBundle(WAB)中有一个Activator,因为WAB你只需要使用&#34;常规&#34;与OSGi Manifest的战争,包括Web-ContextPath。从那里开始,WebExtender将为您完成工作,就像您已经完成的那样。 如果您想使用ServletContextListener,请在web.xml中以常规方式注册它,或者因为您使用带有标准注释的Servlet3 web.xml。

现在最后一部分,日志消息告诉您在向服务器发出请求时,还没有找到请求的httpContext。这样做的原因很可能是你之前的长日志中的某个地方。注册servlet可能会出错。再一次 web:list 命令可能有助于获得更好的图片。

由于Pax Web尚未明确对此进行测试:-)可能有兴趣向Pax Web社区提供一个简单的Sample,将其添加到样本并进行Integration测试。在ops4j邮件列表上询问也总是好的。