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();
}