0

According to the documentation here it says:

If you have a modified web.xml template then you will need to migrate this to Spring as Grails 3.x does not use a web.xml (although it is still possible to have on in src/main/webapp/WEB-INF/web.xml).

which I interpret to mean that if I'm incorporating a 3rd party proprietary library that has a web.xml, then I can put it in src/main/webapp/WEB-INF unaltered (along with everything else that they put in their tomcat webapp directory) and grails will load it. Is this interpretation correct? That's what seems to be implied by this answer.

I started a grails 3 app with the react profile (I tried the web profile too) and a webpage with calls their servlet. However, while an html file in webapp can be found, the servlet call itself is returning 404 and I can't figure out why. If I build a war file and deploy on a standalone tomcat, the servlet call works, but when I run like this:

./gradlew server:bootRun --debug

then it doesn't, and I don't see anything interesting printed to the console.

Is there some URL mapping I need to manipulate or something in application.yml?

In the web.xml, the servlet that's being called looks like this (this is a small piece of it, does it am):

<servlet>
    <servlet-name>DataSourceLoader</servlet-name>
    <servlet-class>com.isomorphic.servlet.DataSourceLoader</servlet-class>
</servlet>
<servlet-mapping>
    <servlet-name>DataSourceLoader</servlet-name>
    <url-pattern>/isomorphic/DataSourceLoader</url-pattern>
</servlet-mapping>

I realize the alternative is to rewrite web.xml using Beans and put stuff in resources.groovy, but I'd prefer an approach that requires as little of my coding as possible.

[update]

I've been able to update my grails-app/conf/spring/resources.groovy with this:

import org.springframework.boot.web.servlet.ServletRegistrationBean
// Place your Spring DSL code here
beans = {
    DataSourceLoader(ServletRegistrationBean) { bean ->
         servlet = new com.isomorphic.servlet.DataSourceLoader()
         urlMappings = ['/isomorphic/DataSourceLoader']
    }        
}

and it seems to be working... Nevertheless, I am still interested in ways to only use web.xml, if possible, which is my original question.

amos
  • 5,092
  • 4
  • 34
  • 43

1 Answers1

0

As part of an upgrade from Grails 2.x to Grails 3.3, I had been previously using web.xml to define 3rd party servlets.

The approach I took in order to make these servlets available(and load at startup) was through a custom class. So you can define a custom class, in src/java as such:

import org.springframework.boot.web.servlet.ServletContextInitializer;
import org.springframework.boot.web.servlet.ServletRegistrationBean;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;

@Configuration
public class ServletRegistrations {

    @Bean
    public ServletRegistrationBean fileServlet(){        
        ServletRegistrationBean registration = new ServletRegistrationBean(new FileServlet(), "/files/*");

        // to load add startup uncomment the line below
        //registration.setLoadOnStartup(1);

        // define init param
        registration.addInitParameter("basePath","/WEB-INF/resources");
        return registration;
    }
}

So although you don't define everything in one XML file, you can still define all your servlets in this one single class, so it's not a big change and I now I've gone through this once, I prefer to be able to do the definitions in code rather than xml. Hope that helps!

David Brown
  • 3,021
  • 3
  • 26
  • 46
  • Thanks for the example, I think I'll do some testing today. Do you happen to have some documentation for how to convert a web.xml into `Bean`s? It sounds like migrating away from web.xml might be a good move but I'd like to confirm that it's not some configuration/misconfiguration that's preventing my web.xml from working unaltered... – amos Jul 02 '18 at 11:46
  • I believe it's the change within Grails to use Spring Boot that's the factor here. – David Brown Jul 02 '18 at 16:57