Mop So
Mop So

Reputation: 401

JHipster app deployment in JBoss 7.2

I would like to deploy my JHipster app in JBoss AS 7.2.0 with a webapp context. First I have added a jboss-deployment-structure.xml file to allow the deployment and to resolve conflicts between JBoss libraries and applicatin libraries :

<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>

To add a webapp context, I have added a jboss-web.xml :

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

So, I can go to http://localhost:8080/myapp to see the main page, but REST requests fail. The requests are sent to http://localhost:8080/app/rest... instead of http://localhost:8080/myapp/app/rest... (Even If i try with the webapp context in the URL, a 404 error occurs)

I tried to remove jboss-web.xml and to deploy a myapp.war file, now the request are done with the context path (http://localhost:8080/myapp/app/rest) but always with a 404 error. I have renamed the file as ROOT.war, but the behaviour is the same :(

I saw that an issue was closed with the webapp context, so it should be ok ? https://github.com/jhipster/generator-jhipster/issues/235 When I deploy the same application Tomcat, it's ok...

I think my issue is linked with servlet registring. For instance /api-docs for swagger-ui is not available also.

Any idea, please ?

Upvotes: 3

Views: 1809

Answers (1)

Mop So
Mop So

Reputation: 401

The issue is linked with Spring Dispatcher Servlet that is mapped with /. It's ok with Tomcat but not with JBoss AS. So I added a mapping with /* to allow deployment in JBoss AS. This method could be added in 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) {
        }
    });

}

Upvotes: 2

Related Questions