Skip to content
Advertisement

Cannot forward to error page for request … as the response has already been committed. As a result, the response may have the wrong status code

I am using Spring Boot 2.0.0.RELEASE, Thymeleaf, Spring Security, JDK 10, Apache Tomcat 9.0.6 . I have controller

package com.donhuvy.controller;

import com.donhuvy.entity.Ccy;
import com.donhuvy.repository.CcyRepository;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.PageRequest;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.servlet.ModelAndView;

import java.util.Optional;

@Controller
@RequestMapping("/ccy")
public class CcyController {

    private static final int INITIAL_PAGE = 0;
    private static final int INITIAL_PAGE_SIZE = 10;

    @Autowired
    CcyRepository ccyRepository;

    @RequestMapping(value = "/all", method = RequestMethod.GET)
    public ModelAndView getAllPaginatingCcy(
            @RequestParam("pageSize") Optional<Integer> pageSize,
            @RequestParam("page") Optional<Integer> page) {
        int evalPageSize = pageSize.orElse(INITIAL_PAGE_SIZE);
        int evalPage = (page.orElse(0) < 1) ? INITIAL_PAGE : page.get() - 1;
        Page<Ccy> currencies = ccyRepository.findAll(PageRequest.of(evalPage, evalPageSize));
        ModelAndView modelAndView = new ModelAndView("cash/ccy");
        modelAndView.addObject("currencies", currencies);
        return modelAndView;
    }

    @GetMapping("/vy")
    public String foo(@RequestParam("pageSize") Optional<Integer> pageSize,
                      @RequestParam("page") Optional<Integer> page, Model model){

        int evalPageSize = pageSize.orElse(INITIAL_PAGE_SIZE);
        int evalPage = (page.orElse(0) < 1) ? INITIAL_PAGE : page.get() - 1;
        Page<Ccy> currencies = ccyRepository.findAll(PageRequest.of(evalPage, evalPageSize));

        model.addAttribute("currencies", currencies);
        return "cash/ccy";
    }

}

at application.properties I have

spring.mvc.view.prefix=templates/
spring.mvc.view.suffix=.html

I have configuration class

package com.donhuvy.config;

import org.springframework.boot.autoconfigure.EnableAutoConfiguration;
import org.springframework.boot.web.servlet.FilterRegistrationBean;
import org.springframework.boot.web.servlet.support.ErrorPageFilter;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.ComponentScan;
import org.springframework.context.annotation.Configuration;
import org.springframework.web.servlet.LocaleResolver;
import org.springframework.web.servlet.ViewResolver;
import org.springframework.web.servlet.config.annotation.DefaultServletHandlerConfigurer;
import org.springframework.web.servlet.config.annotation.EnableWebMvc;
import org.springframework.web.servlet.config.annotation.InterceptorRegistry;
import org.springframework.web.servlet.config.annotation.WebMvcConfigurationSupport;
import org.springframework.web.servlet.i18n.LocaleChangeInterceptor;
import org.springframework.web.servlet.i18n.SessionLocaleResolver;
import org.springframework.web.servlet.view.InternalResourceViewResolver;

import java.util.Locale;

/**
 * Configuration for overall application.
 */
@EnableWebMvc
@Configuration
@ComponentScan
@EnableAutoConfiguration
public class WebConfig extends WebMvcConfigurationSupport {

    @Bean
    public ViewResolver getViewResolver() {
        InternalResourceViewResolver resolver = new InternalResourceViewResolver();
        resolver.setPrefix("/templates/");
        resolver.setSuffix(".html");
        return resolver;
    }

    @Override
    public void configureDefaultServletHandling(
            DefaultServletHandlerConfigurer configurer) {
        configurer.enable();
    }


    /**
     * Switch language, default language is Vietnamese - Vy's mother tongue language.
     * If user would like to switch to other language, use parameter,
     * for example: http://localhost:8081/web/cash/cash_receipt?lang=en
     *
     * @return
     */
    @Bean
    public LocaleResolver localeResolver() {
        SessionLocaleResolver sessionLocaleResolver = new SessionLocaleResolver();
        // sessionLocaleResolver.setDefaultLocale(Locale.US);
        Locale vietnamLocale = new Locale("vi", "VN");
        sessionLocaleResolver.setDefaultLocale(vietnamLocale);
        return sessionLocaleResolver;
    }

    @Bean
    public LocaleChangeInterceptor localeChangeInterceptor() {
        LocaleChangeInterceptor lci = new LocaleChangeInterceptor();
        lci.setParamName("lang");
        return lci;
    }

    @Override
    public void addInterceptors(InterceptorRegistry ir) {
        ir.addInterceptor(localeChangeInterceptor());
    }

}

When I access http://localhost:8081/cash/ccy or http://localhost:8081/cash/vy I see the same error

/Users/donhuvy/Documents/tools/apache-tomcat-9.0.6/bin/catalina.sh run
[2018-03-25 11:01:17,218] Artifact Gradle : com.donhuvy : accounting-1.0.0-SNAPSHOT.war (exploded): Waiting for server connection to start artifact deployment...
NOTE: Picked up JDK_JAVA_OPTIONS:  --add-opens=java.base/java.lang=ALL-UNNAMED --add-opens=java.rmi/sun.rmi.transport=ALL-UNNAMED
Connected to the target VM, address: '127.0.0.1:49344', transport: 'socket'
25-Mar-2018 11:01:18.548 INFO [main] org.apache.catalina.startup.VersionLoggerListener.log Server version:        Apache Tomcat/9.0.6
25-Mar-2018 11:01:18.551 INFO [main] org.apache.catalina.startup.VersionLoggerListener.log Server built:          Mar 5 2018 09:34:35 UTC
25-Mar-2018 11:01:18.551 INFO [main] org.apache.catalina.startup.VersionLoggerListener.log Server number:         9.0.6.0
25-Mar-2018 11:01:18.551 INFO [main] org.apache.catalina.startup.VersionLoggerListener.log OS Name:               Mac OS X
25-Mar-2018 11:01:18.551 INFO [main] org.apache.catalina.startup.VersionLoggerListener.log OS Version:            10.13.3
25-Mar-2018 11:01:18.552 INFO [main] org.apache.catalina.startup.VersionLoggerListener.log Architecture:          x86_64
25-Mar-2018 11:01:18.552 INFO [main] org.apache.catalina.startup.VersionLoggerListener.log Java Home:             /Library/Java/JavaVirtualMachines/jdk-10.jdk/Contents/Home
25-Mar-2018 11:01:18.553 INFO [main] org.apache.catalina.startup.VersionLoggerListener.log JVM Version:           10+46
25-Mar-2018 11:01:18.553 INFO [main] org.apache.catalina.startup.VersionLoggerListener.log JVM Vendor:            "Oracle Corporation"
25-Mar-2018 11:01:18.553 INFO [main] org.apache.catalina.startup.VersionLoggerListener.log CATALINA_BASE:         /Users/donhuvy/Library/Caches/IntelliJIdea2017.3/tomcat/Unnamed_accounting
25-Mar-2018 11:01:18.553 INFO [main] org.apache.catalina.startup.VersionLoggerListener.log CATALINA_HOME:         /Users/donhuvy/Documents/tools/apache-tomcat-9.0.6
25-Mar-2018 11:01:18.558 INFO [main] org.apache.catalina.startup.VersionLoggerListener.log Command line argument: --add-opens=java.base/java.lang=ALL-UNNAMED
25-Mar-2018 11:01:18.558 INFO [main] org.apache.catalina.startup.VersionLoggerListener.log Command line argument: --add-opens=java.rmi/sun.rmi.transport=ALL-UNNAMED
25-Mar-2018 11:01:18.558 INFO [main] org.apache.catalina.startup.VersionLoggerListener.log Command line argument: -Djava.util.logging.config.file=/Users/donhuvy/Library/Caches/IntelliJIdea2017.3/tomcat/Unnamed_accounting/conf/logging.properties
25-Mar-2018 11:01:18.559 INFO [main] org.apache.catalina.startup.VersionLoggerListener.log Command line argument: -Djava.util.logging.manager=org.apache.juli.ClassLoaderLogManager
25-Mar-2018 11:01:18.559 INFO [main] org.apache.catalina.startup.VersionLoggerListener.log Command line argument: -agentlib:jdwp=transport=dt_socket,address=127.0.0.1:49344,suspend=y,server=n
25-Mar-2018 11:01:18.559 INFO [main] org.apache.catalina.startup.VersionLoggerListener.log Command line argument: -javaagent:/Users/donhuvy/Library/Caches/IntelliJIdea2017.3/captureAgent/debugger-agent.jar=/private/var/folders/lj/tgdhz0mx5t54vglcc8982vbc0000gn/T/capture823.props
25-Mar-2018 11:01:18.559 INFO [main] org.apache.catalina.startup.VersionLoggerListener.log Command line argument: -javaagent:/Users/donhuvy/Library/Caches/IntelliJIdea2017.3/groovyHotSwap/gragent.jar
25-Mar-2018 11:01:18.559 INFO [main] org.apache.catalina.startup.VersionLoggerListener.log Command line argument: --illegal-access=warn
25-Mar-2018 11:01:18.559 INFO [main] org.apache.catalina.startup.VersionLoggerListener.log Command line argument: -Dcom.sun.management.jmxremote=
25-Mar-2018 11:01:18.559 INFO [main] org.apache.catalina.startup.VersionLoggerListener.log Command line argument: -Dcom.sun.management.jmxremote.port=1099
25-Mar-2018 11:01:18.559 INFO [main] org.apache.catalina.startup.VersionLoggerListener.log Command line argument: -Dcom.sun.management.jmxremote.ssl=false
25-Mar-2018 11:01:18.560 INFO [main] org.apache.catalina.startup.VersionLoggerListener.log Command line argument: -Dcom.sun.management.jmxremote.authenticate=false
25-Mar-2018 11:01:18.560 INFO [main] org.apache.catalina.startup.VersionLoggerListener.log Command line argument: -Djava.rmi.server.hostname=127.0.0.1
25-Mar-2018 11:01:18.560 INFO [main] org.apache.catalina.startup.VersionLoggerListener.log Command line argument: -Djdk.tls.ephemeralDHKeySize=2048
25-Mar-2018 11:01:18.560 INFO [main] org.apache.catalina.startup.VersionLoggerListener.log Command line argument: -Djava.protocol.handler.pkgs=org.apache.catalina.webresources
25-Mar-2018 11:01:18.560 INFO [main] org.apache.catalina.startup.VersionLoggerListener.log Command line argument: -Dignore.endorsed.dirs=
25-Mar-2018 11:01:18.560 INFO [main] org.apache.catalina.startup.VersionLoggerListener.log Command line argument: -Dcatalina.base=/Users/donhuvy/Library/Caches/IntelliJIdea2017.3/tomcat/Unnamed_accounting
25-Mar-2018 11:01:18.560 INFO [main] org.apache.catalina.startup.VersionLoggerListener.log Command line argument: -Dcatalina.home=/Users/donhuvy/Documents/tools/apache-tomcat-9.0.6
25-Mar-2018 11:01:18.560 INFO [main] org.apache.catalina.startup.VersionLoggerListener.log Command line argument: -Djava.io.tmpdir=/Users/donhuvy/Documents/tools/apache-tomcat-9.0.6/temp
25-Mar-2018 11:01:18.561 INFO [main] org.apache.catalina.core.AprLifecycleListener.lifecycleEvent The APR based Apache Tomcat Native library which allows optimal performance in production environments was not found on the java.library.path: [/Users/donhuvy/Library/Java/Extensions:/Library/Java/Extensions:/Network/Library/Java/Extensions:/System/Library/Java/Extensions:/usr/lib/java:.]
25-Mar-2018 11:01:18.606 INFO [main] org.apache.coyote.AbstractProtocol.init Initializing ProtocolHandler 

2018-03-25 11:05:14 ERROR o.s.b.w.s.support.ErrorPageFilter - Cannot forward to error page for request [/ccy/vy] as the response has already been committed. As a result, the response may have the wrong status code. If your application is running on WebSphere Application Server you may be able to resolve this problem by setting com.ibm.ws.webcontainer.invokeFlushAfterService to false

The full console log is https://gist.github.com/donhuvy/ad3f65955292d9874805c99c03f3227f#file-console-log-L450

How to fix it?

Advertisement

Answer

I am missing Thymeleaf dependency. I must add this line to build.gradle

compile ('org.springframework.boot:spring-boot-starter-thymeleaf:2.0.0.RELEASE')
Advertisement