无法转发到请求的错误页面...因为响应已经提交。因此,响应可能具有错误的状态代码

时间:2018-03-25 04:09:47

标签: java spring spring-mvc spring-boot thymeleaf

我正在使用Spring Boot 2.0.0.RELEASE,Thymeleaf,Spring Security,JDK 10,Apache Tomcat 9.0.6。我有控制器

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";
    }

}

application.properties我有

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

我有配置类

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

}

当我访问http://localhost:8081/cash/ccyhttp://localhost:8081/cash/vy时,我看到相同的错误

/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

完整的控制台日志为https://gist.github.com/donhuvy/ad3f65955292d9874805c99c03f3227f#file-console-log-L450

如何解决?

3 个答案:

答案 0 :(得分:3)

您可以通过在Spring配置中进行配置来禁用ErrorPageFilter来删除错误日志:

@Bean
public ErrorPageFilter errorPageFilter() {
    return new ErrorPageFilter();
}

@Bean
public FilterRegistrationBean<Filter> disableSpringBootErrorFilter(ErrorPageFilter filter) {
    FilterRegistrationBean filterRegistrationBean = new FilterRegistrationBean();
    filterRegistrationBean.setFilter(filter);
    filterRegistrationBean.setEnabled(false);

    return filterRegistrationBean;
}

在Spring Boot中,通过配置第二个@Bean就足够了。

答案 1 :(得分:1)

对我来说,这是由我的 jsp 中的 <s:include> 指向一个无法找到的文件引起的。
我的 Java 控制台和日志显示 org.springframework.boot.web.servlet.support.ErrorPageFilter : Cannot forward to error page for request [/my-app/my-app-sub] as the response has already been committed.,而网络浏览器显示 404
当我更正我在 <s:include> 中提供的文件位置时,错误消失了。

答案 2 :(得分:0)

我错过了Thymeleaf依赖。我必须将此行添加到build.gradle

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