1

UPDATE: I was unable to find a way to use the XML, however I found a much better way (see below) of doing what I needed based on dzirtbry's comment.


So I've been working on a project using Spring Boot which I very easily generated with Gradle. So far the XML-less model has been working great, however I have come to a point where I need to add a tiny bit of XML configuration.

ie I want to add a custom error-page to my project like so

   <error-page>
     <error-code>404</error-code>
     <location>/custom-error-page</location>
   </error-page>

As I said, my Gradle generated project did not include a web.xml file, so I attempted creating the typical directory structure of src/main/webapp/WEB-INF and placing one there, however it does not seem to load the web.xml.

Is there a specific place I should put my web.xml for spring boot to pick it up? Or do I need to add additional configuration somewhere?

My boilerplate application is pretty standard as follows:

@SpringBootApplication
public class Application {

    public static void main(String[] args) {
        SpringApplication.run(Application.class, args);
    }
}


After skimming the article from @reto 's comment, I tried adding this class to my project:
import org.springframework.data.rest.webmvc.ResourceNotFoundException;
import org.springframework.web.bind.annotation.ControllerAdvice;
import org.springframework.web.bind.annotation.ExceptionHandler;
import org.springframework.web.servlet.ModelAndView;

@ControllerAdvice
class GlobalControllerExceptionHandler {
    @ExceptionHandler( ResourceNotFoundException.class)
    public ModelAndView handle404() {
        ModelAndView mav = new ModelAndView();
        mav.setViewName("error");
        System.out.println("caught 404");
        return mav;
    }
}

Although it doesn't seem to be intercepting the error.


A much cleaner way of solving my problem without XML turned out to be as follows:

Creating this class:

public class ServerCustomization extends ServerProperties {
  @Override
  public void customize(ConfigurableEmbeddedServletContainer container) {

    super.customize(container);
    container.addErrorPages(new ErrorPage(HttpStatus.NOT_FOUND, 
        "/custom-error-page"));
  }
}

And adding this bean to my Application class:

@Bean
public ServerProperties getServerProperties() {
    return new ServerCustomization();
}
FoxMulder900
  • 1,272
  • 13
  • 27
  • Have you read https://spring.io/blog/2013/11/01/exception-handling-in-spring-mvc ? – reto Aug 06 '15 at 19:56
  • 1
    @reto - thanks, I read through that post, however still was unable to get anything working. I updated my question to reflect what I tried. – FoxMulder900 Aug 06 '15 at 20:48
  • 1
    Have you looked at http://stackoverflow.com/questions/27386261/spring-boot-custom-404-page-with-standalone-tomcat ? It sounds very similar to what you are trying to do. – dzirtbry Aug 06 '15 at 21:00
  • @dzirtbry - That did it! And I didn't even have to use XML – FoxMulder900 Aug 07 '15 at 13:12

0 Answers0