Failed to load ApplicationContext caused by ArrayIndexOutOfBoundsException in ClassReader

Most likely you have lambda expression inside one of your spring beans. Apparently, spring 3 beans can’t initialize if there is a lambda somewhere in their code. In case migration to spring 4 is not an option, try to rewrite your lambda expressions using anonymous classes, like Function<A,B> lambda = new Function() { public B … Read more

Spring HandlerMethodArgumentResolver not executing

If anybody ever wants to prioritize custom handlers over default handlers added by spring, here’s a snippet that does it for me, I do this in a @Configuration file private @Inject RequestMappingHandlerAdapter adapter; @PostConstruct public void prioritizeCustomArgumentMethodHandlers () { List<HandlerMethodArgumentResolver> argumentResolvers = new ArrayList<> (adapter.getArgumentResolvers ()); List<HandlerMethodArgumentResolver> customResolvers = adapter.getCustomArgumentResolvers (); argumentResolvers.removeAll (customResolvers); argumentResolvers.addAll (0, … Read more

How does server prioritize which type of web.xml error page to use?

This is not specific to Tomcat. This is specific to the Servlet API. How the error page is determined is specified in chapter 9.9.2 of Servlet API specification 2.5. Here’s an extract of relevance: SRV.9.9.2 Error Pages If no error-page declaration containing an exception-type fits using the class-hierarchy match, and the exception thrown is a … Read more

Spring – Modifying headers for every request after processing (in postHandle)

It sounds like you’re on the right track with a servlet filter, what you probably need to do is wrap the servlet response object with one that detects when a 401 status code has been set and adds your custom header at that time: HttpServletResponse wrappedResponse = new HttpServletResponseWrapper(response) { public void setStatus(int code) { … Read more

Spring-MVC 406 Not Acceptable instead of JSON Response

Add the following in DispatcherServlet-servlet.xml. <bean id=”jacksonMessageConverter” class=”org.springframework.http.converter.json.MappingJacksonHttpMessageConverter”></bean> <bean class=”org.springframework.web.servlet.mvc.annotation.AnnotationMethodHandlerAdapter”> <property name=”messageConverters”> <list> <ref bean=”jacksonMessageConverter”/> </list> </property> </bean>

How I create an error handler (404, 500…) in Spring Boot/MVC

You may try the following code: @ControllerAdvice public class ExceptionController { @ExceptionHandler(Exception.class) public ModelAndView handleError(HttpServletRequest request, Exception e) { Logger.getLogger(getClass().getName()).log(Level.SEVERE, “Request: ” + request.getRequestURL() + ” raised ” + e); return new ModelAndView(“error”); } @ExceptionHandler(NoHandlerFoundException.class) public ModelAndView handleError404(HttpServletRequest request, Exception e) { Logger.getLogger(getClass().getName()).log(Level.SEVERE, “Request: ” + request.getRequestURL() + ” raised ” + e); return new … Read more

Spring Boot exception: Could not open ServletContext resource [/WEB-INF/dispatcherServlet-servlet.xml]

I found a workaround putting a dummy dispatcherServlet-servlet.xml file under WEB-INF: <beans xmlns=”http://www.springframework.org/schema/beans” xmlns:xsi=”http://www.w3.org/2001/XMLSchema-instance” xsi:schemaLocation=”http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd”> <!– Do not remove this file! –> </beans>