JBoss 7.2中的JHipster app部署

时间:2015-01-27 18:06:18

标签: java jboss jboss7.x jhipster

我想在JBoss AS 7.2.0中使用webapp上下文部署我的JHipster应用程序。首先,我添加了jboss-deployment-structure.xml文件以允许部署并解决JBoss库和应用程序库之间的冲突:

<jboss-deployment-structure xmlns="urn:jboss:deployment-structure:1.2">
    <deployment>
        <dependencies>

            <!-- Add some dependencies because of javaee.api exclusion -->
            <module name="javax.xml.bind.api" />
            <module name="javax.xml.ws.api" />
            <module name="javax.jws.api" />
            <module name="javax.annotation.api" />

        </dependencies>

        <exclude-subsystems>
            <subsystem name="webservices" />
            <subsystem name="jaxrs" />
            <subsystem name="jpa" />
        </exclude-subsystems>
        <exclusions>
            <module name="org.slf4j" />
            <module name="org.slf4j.impl" />
            <module name="org.slf4j.jcl-over-slf4j" />
            <module name="org.apache.commons.logging" />
            <module name="org.jboss.logging" />
            <module name="org.apache.log4j" />

            <module name="javaee.api" />

        </exclusions>
    </deployment>
</jboss-deployment-structure>

要添加webapp上下文,我添加了jboss-web.xml

<?xml version="1.0" encoding="UTF-8"?>
<jboss-web>
    <context-root>myapp</context-root>
</jboss-web>

因此,我可以转到http://localhost:8080/myapp查看主页面,但REST请求失败。请求将发送到http://localhost:8080/app/rest ...而不是http://localhost:8080/myapp/app/rest ...(即使我尝试使用网址中的webapp上下文,也会出现404错误)

我尝试删除jboss-web.xml并部署myapp.war文件,现在请求是使用上下文路径(http://localhost:8080/myapp/app/rest)完成的,但总是出现404错误。我已将文件重命名为ROOT.war,但行为相同:(

我看到一个问题因webapp上下文而关闭,所以它应该没问题? https://github.com/jhipster/generator-jhipster/issues/235 当我部署相同的应用程序Tomcat时,没关系......

我认为我的问题与servlet registring有关。例如,swagger-ui的/ api-docs也不可用。

请问,好吗?

1 个答案:

答案 0 :(得分:2)

该问题与使用/映射的Spring Dispatcher Servlet相关联。 Tomcat可以,但JBoss AS没有。所以我添加了/*的映射,以允许在JBoss AS中进行部署。可以在WebConfigurer中添加此方法:

public void onStartup(ServletContext servletContext) throws ServletException {
    [...]
    initDispatcherServlet(servletContext, disps);
    [...]
}

/**
 * Initializes Spring Dispatch Servlet to allow deployment in JBoss
 */
private void initDispatcherServlet(ServletContext servletContext, EnumSet<DispatcherType> disps) {


    // Using listener to be able to get the Dispatcher Servlet not yet initialized
    servletContext.addListener(new ServletContextListener() {

        @Override
        public void contextInitialized(ServletContextEvent event) {
            try {
                Map<String, ? extends ServletRegistration> servlets=null;
                servlets = event.getServletContext().getServletRegistrations();
                Set<String> keys = servlets.keySet();       

                log.debug("Registred servlets : "+keys);

                ServletRegistration dspSrvlt = servlets.get("dispatcherServlet");
                if(dspSrvlt != null) {
                    Collection<String> maps = dspSrvlt.getMappings();
                    log.debug("Dispatcher servlet mapping size : "+maps.toArray().length);
                    log.debug("Servlet dispatcher mapping : "+maps);
                    if( !maps.contains("/*") ) {
                        log.debug("Adding /* for Spring Dispatcher servlet");
                        servlets.get("dispatcherServlet").addMapping("/*");
                    }
                } else {
                    log.warn("Unable to change the Servlet Request dispatcher mapping to allow deployment with JBoss");
                }
            } catch (Exception e) {
                log.warn("Unable to change the Servlet Context to allow deployment with JBoss");
            }

        }

        @Override
        public void contextDestroyed(ServletContextEvent arg0) {
        }
    });

}