Spring Boot ErrorController不使用OpenShift

时间:2017-01-30 19:58:15

标签: java spring spring-boot tomcat7 openshift

我正在尝试在服务器上由于任何原因未满足请求时发送标准错误响应。为此,我实现了ErrorController接口并重写了getErrorPath方法。它在我的本地机器上工作,但当我使用Tomcat 7.0.54将其部署到OpenShift服务器上时。它不起作用它显示此异常而不是显示json响应:

counter

这是我做的:

Application.java

Dictionary<string, string> BigOne = CoulmnsDescription.OrderByDescending(dic => dic.Count()).FirstOrDefault();

ErrorJson.java

    HTTP Status 500 - org.springframework.web.context.request.async.WebAsyncManager cannot be cast to org.springframework.web.context.request.async.WebAsyncManager

    type Exception report

    message org.springframework.web.context.request.async.WebAsyncManager cannot be cast to org.springframework.web.context.request.async.WebAsyncManager

    description The server encountered an internal error that prevented it from fulfilling this request.

    exception

    java.lang.ClassCastException: org.springframework.web.context.request.async.WebAsyncManager cannot be cast to org.springframework.web.context.request.async.WebAsyncManager
        org.springframework.web.context.request.async.WebAsyncUtils.getAsyncManager(WebAsyncUtils.java:47)
        org.springframework.web.filter.OncePerRequestFilter.isAsyncDispatch(OncePerRequestFilter.java:137)
        org.springframework.web.filter.OncePerRequestFilter.skipDispatch(OncePerRequestFilter.java:118)
        org.springframework.web.filter.OncePerRequestFilter.doFilter(OncePerRequestFilter.java:98)
        org.springframework.web.filter.OncePerRequestFilter.doFilter(OncePerRequestFilter.java:101)
        org.springframework.boot.web.support.ErrorPageFilter.doFilter(ErrorPageFilter.java:112)
        org.springframework.boot.web.support.ErrorPageFilter.forwardToErrorPage(ErrorPageFilter.java:187)
        org.springframework.boot.web.support.ErrorPageFilter.handleException(ErrorPageFilter.java:170)
        org.springframework.boot.web.support.ErrorPageFilter.doFilter(ErrorPageFilter.java:134)
        org.springframework.boot.web.support.ErrorPageFilter.access$000(ErrorPageFilter.java:61)
        org.springframework.boot.web.support.ErrorPageFilter$1.doFilterInternal(ErrorPageFilter.java:94)
        org.springframework.web.filter.OncePerRequestFilter.doFilter(OncePerRequestFilter.java:107)
        org.springframework.boot.web.support.ErrorPageFilter.doFilter(ErrorPageFilter.java:112)    

CustomErrorController.java

    import org.apache.catalina.Context;
    import org.apache.catalina.deploy.ContextResource;
    import org.apache.catalina.startup.Tomcat;
    import org.apache.log4j.Logger;
    import org.springframework.boot.SpringApplication;
    import org.springframework.boot.autoconfigure.EnableAutoConfiguration;
    import org.springframework.boot.autoconfigure.SpringBootApplication;
    import org.springframework.boot.autoconfigure.web.ErrorMvcAutoConfiguration;
    import org.springframework.boot.builder.SpringApplicationBuilder;
    import org.springframework.boot.context.embedded.EmbeddedServletContainerFactory;
    import org.springframework.boot.context.embedded.tomcat.TomcatEmbeddedServletContainer;
    import org.springframework.boot.context.embedded.tomcat.TomcatEmbeddedServletContainerFactory;
    import org.springframework.boot.web.support.SpringBootServletInitializer;
    import org.springframework.context.annotation.Bean;
    import org.springframework.jndi.JndiObjectFactoryBean;

    @SpringBootApplication
    public class Application extends SpringBootServletInitializer{

        private static final Logger LOGGER = Logger.getLogger(Application.class);

        public static void main(String[] args) {
            LOGGER.info("Spring Boot Application Started");
            SpringApplication.run(Application.class, args);
            LOGGER.info("Spring Boot Application Ended");
        }

        @Override
        protected SpringApplicationBuilder configure(SpringApplicationBuilder application) {
            return application.sources(Application.class);
        }

        @Bean
        public EmbeddedServletContainerFactory tomcatFactory() {
            return new TomcatEmbeddedServletContainerFactory() {

                @Override
                protected TomcatEmbeddedServletContainer getTomcatEmbeddedServletContainer(Tomcat tomcat) {
                    tomcat.enableNaming();
                    return super.getTomcatEmbeddedServletContainer(tomcat);
                }
            };
        }
    }   

的pom.xml

import java.util.Map;

public class ErrorJson {

public Integer status;
public String error;
public String message;
public String timeStamp;

public ErrorJson(int status, Map<String, Object> errorAttributes) {
    this.status = status;
    this.error = (String) errorAttributes.get("error");
    this.message = "Please Contact the Administrator for more detail.";
    this.timeStamp = errorAttributes.get("timestamp").toString();
}
}

当我只搜索解决方案时,我发现了这个: ind2sub

但它没有意义。

如果我运行 mvn依赖:树,我会得到这个:

    import org.springframework.beans.factory.annotation.Autowired;
    import org.springframework.beans.factory.annotation.Value;
    import org.springframework.boot.autoconfigure.web.ErrorAttributes;
    import org.springframework.boot.autoconfigure.web.ErrorController;
    import org.springframework.web.bind.annotation.RequestMapping;
    import org.springframework.web.bind.annotation.RestController;
    import org.springframework.web.context.request.RequestAttributes;
    import org.springframework.web.context.request.ServletRequestAttributes;

    import javax.servlet.http.HttpServletRequest;
    import javax.servlet.http.HttpServletResponse;
    import java.util.Map;

    @RestController
    public class CustomErrorController implements ErrorController {

        private static final String PATH = "/error";

        @Autowired
        private ErrorAttributes errorAttributes;

        @RequestMapping(value = PATH)
        ErrorJson error(HttpServletRequest request, HttpServletResponse response) {
            // Appropriate HTTP response code (e.g. 404 or 500) is automatically set by Spring. 
            // Here we just define response body.
            return new ErrorJson(response.getStatus(), getErrorAttributes(request));
        }

        @Override
        public String getErrorPath() {
            return PATH;
        }

        private Map<String, Object> getErrorAttributes(HttpServletRequest request) {
            RequestAttributes requestAttributes = new ServletRequestAttributes(request);
            return errorAttributes.getErrorAttributes(requestAttributes,true);
        }

    }

添加过滤器

    <project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
        xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
        <modelVersion>4.0.0</modelVersion>

        <artifactId>tomcat7</artifactId>
        <version>1.0-SNAPSHOT</version>
        <packaging>war</packaging>

        <name>newapp-tomcat7</name>

        <parent>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-parent</artifactId>
            <version>1.4.1.RELEASE</version>
        </parent>

        <properties>
            <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
            <java.version>1.7</java.version>
            <tomcat.version>7.0.54</tomcat.version>
            <json-simple.version>1.1.1</json-simple.version>
            <commons-codec.verison>1.10</commons-codec.verison>
            <log4j-core.version>2.4.1</log4j-core.version>
            <log4j-api.version>2.4.1</log4j-api.version>
        </properties>

        <dependencies>
            <dependency>
                <groupId>org.springframework.session</groupId>
                <artifactId>spring-session</artifactId>
            </dependency>
            <dependency>
                <groupId>org.springframework.session</groupId>
                <artifactId>spring-session-jdbc</artifactId>
            </dependency>
            <dependency>
                <groupId>org.springframework.boot</groupId>
                <artifactId>spring-boot-starter-freemarker</artifactId>
            </dependency>
            <dependency>
                <groupId>org.springframework.boot</groupId>
                <artifactId>spring-boot-starter-mail</artifactId>
            </dependency>
            <dependency>
                <groupId>org.springframework.boot</groupId>
                <artifactId>spring-boot-starter-jdbc</artifactId>
            </dependency>
            <dependency>
                <groupId>org.springframework.boot</groupId>
                <artifactId>spring-boot-starter-jersey</artifactId>
            </dependency>
            <dependency>
                <groupId>org.springframework.boot</groupId>
                <artifactId>spring-boot-starter-actuator</artifactId>
            </dependency>
            <dependency>
                <groupId>org.springframework.boot</groupId>
                <artifactId>spring-boot-starter-web</artifactId>
                <exclusions>
                    <exclusion>
                        <groupId>org.springframework.boot</groupId>
                        <artifactId>spring-boot-starter-tomcat</artifactId>
                    </exclusion>
                </exclusions>
            </dependency>
            <!-- <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-thymeleaf</artifactId> 
                </dependency> -->
            <dependency>
                <groupId>org.springframework.boot</groupId>
                <artifactId>spring-boot-starter-test</artifactId>
                <scope>test</scope>
            </dependency>
            <!-- http://mvnrepository.com/artifact/commons-codec/commons-codec -->
            <dependency>
                <groupId>commons-codec</groupId>
                <artifactId>commons-codec</artifactId>
            </dependency>
            <dependency>
                <groupId>com.googlecode.json-simple</groupId>
                <artifactId>json-simple</artifactId>
            </dependency>
            <dependency>
                <groupId>org.json</groupId>
                <artifactId>json</artifactId>
            </dependency>
            <dependency>
                <groupId>org.apache.logging.log4j</groupId>
                <artifactId>log4j-core</artifactId>
            </dependency>
            <dependency>
                <groupId>org.apache.logging.log4j</groupId>
                <artifactId>log4j-api</artifactId>
            </dependency>
            <dependency>
                <groupId>org.slf4j</groupId>
                <artifactId>slf4j-log4j12</artifactId>
            </dependency>
            <dependency>
                <groupId>junit</groupId>
                <artifactId>junit</artifactId>
                <scope>test</scope>
            </dependency>

            <!-- Dependencies added for WAS and Oracle -->
            <dependency>
                <groupId>com.oracle</groupId>
                <artifactId>classes12</artifactId>
                <version>10.2.0.5</version>
                <scope>provided</scope>
            </dependency>
            <!-- <dependency> <groupId>com.oracle</groupId> <artifactId>ojdbc6</artifactId> 
                <version>11.2.0.3</version> <scope>provided</scope> </dependency> -->

            <dependency>
                <groupId>com.oracle</groupId>
                <artifactId>ojdbc14</artifactId>
                <version>10.2.0.5</version>
            </dependency>

            <!-- Dependencies WAS and Oracle ends -->

            <!-- Server Dependency -->
            <dependency>
                <groupId>org.apache.tomcat</groupId>
                <artifactId>tomcat-juli</artifactId>
                <version>${tomcat.version}</version>
            </dependency>
            <dependency>
                <groupId>org.apache.tomcat</groupId>
                <artifactId>tomcat-dbcp</artifactId>
                <version>${tomcat.version}</version>
            </dependency>
            <dependency>
                <groupId>javax.servlet</groupId>
                <artifactId>javax.servlet-api</artifactId>
            </dependency>
            <dependency>
                <groupId>org.apache.tomcat</groupId>
                <artifactId>tomcat-jdbc</artifactId>
            </dependency>
            <!-- Server Dependency ends -->

        </dependencies>

        <build>
            <finalName>cctgmap</finalName>
            <plugins>
                <plugin>
                    <groupId>org.apache.tomcat.maven</groupId>
                    <artifactId>tomcat7-maven-plugin</artifactId>
                    <version>2.2</version>
                    <configuration>
                        <server>TomcatServer</server>
                        <port>8008</port>
                    </configuration>
                </plugin>
                <plugin>
                    <groupId>org.apache.maven.plugins</groupId>
                    <artifactId>maven-assembly-plugin</artifactId>
                    <version>2.4</version>
                    <executions>
                        <execution>
                            <phase>package</phase>
                            <goals>
                                <goal>single</goal>
                            </goals>
                        </execution>
                    </executions>
                    <configuration>
                        <appendAssemblyId>false</appendAssemblyId>
                        <descriptors>
                            <descriptor>assembly.xml</descriptor>
                        </descriptors>
                    </configuration>
                </plugin>
                <plugin>
                    <artifactId>maven-war-plugin</artifactId>
                    <version>2.1.1</version>
                    <configuration>
                        <outputDirectory>${project.build.directory}/../package/dependencies/jbossews/webapps </outputDirectory>
                        <warName>ROOT</warName>
                    </configuration>
                </plugin>
            </plugins>
        </build>
    </project>

但它仍然无法正常显示这个例外:

    [INFO] +- org.springframework.session:spring-session:jar:1.2.2.RELEASE:compile
    [INFO] |  \- commons-logging:commons-logging:jar:1.2:compile
    [INFO] +- org.springframework.session:spring-session-jdbc:jar:1.2.2.RELEASE:compile
    [INFO] |  \- org.springframework:spring-jdbc:jar:4.3.3.RELEASE:compile
    [INFO] |     +- org.springframework:spring-beans:jar:4.3.3.RELEASE:compile
    [INFO] |     \- org.springframework:spring-tx:jar:4.3.3.RELEASE:compile
    [INFO] +- org.springframework.boot:spring-boot-starter-freemarker:jar:1.4.1.RELEASE:compile
    [INFO] |  +- org.springframework.boot:spring-boot-starter:jar:1.4.1.RELEASE:compile
    [INFO] |  |  +- org.springframework.boot:spring-boot:jar:1.4.1.RELEASE:compile
    [INFO] |  |  +- org.springframework.boot:spring-boot-autoconfigure:jar:1.4.1.RELEASE:compile
    [INFO] |  |  +- org.springframework.boot:spring-boot-starter-logging:jar:1.4.1.RELEASE:compile
    [INFO] |  |  |  +- ch.qos.logback:logback-classic:jar:1.1.7:compile
    [INFO] |  |  |  |  \- ch.qos.logback:logback-core:jar:1.1.7:compile
    [INFO] |  |  |  +- org.slf4j:jcl-over-slf4j:jar:1.7.21:compile
    [INFO] |  |  |  +- org.slf4j:jul-to-slf4j:jar:1.7.21:compile
    [INFO] |  |  |  \- org.slf4j:log4j-over-slf4j:jar:1.7.21:compile
    [INFO] |  |  \- org.yaml:snakeyaml:jar:1.17:runtime
    [INFO] |  +- org.freemarker:freemarker:jar:2.3.25-incubating:compile
    [INFO] |  \- org.springframework:spring-context-support:jar:4.3.3.RELEASE:compile
    [INFO] +- org.springframework.boot:spring-boot-starter-mail:jar:1.4.1.RELEASE:compile
    [INFO] |  +- org.springframework:spring-context:jar:4.3.3.RELEASE:compile
    [INFO] |  |  +- org.springframework:spring-aop:jar:4.3.3.RELEASE:compile
    [INFO] |  |  \- org.springframework:spring-expression:jar:4.3.3.RELEASE:compile
    [INFO] |  \- com.sun.mail:javax.mail:jar:1.5.6:compile
    [INFO] |     \- javax.activation:activation:jar:1.1:compile
    [INFO] +- org.springframework.boot:spring-boot-starter-jdbc:jar:1.4.1.RELEASE:compile
    [INFO] +- org.springframework.boot:spring-boot-starter-jersey:jar:1.4.1.RELEASE:compile
    [INFO] |  +- org.springframework.boot:spring-boot-starter-tomcat:jar:1.4.1.RELEASE:compile
    [INFO] |  |  +- org.apache.tomcat.embed:tomcat-embed-core:jar:7.0.59:compile
    [INFO] |  |  +- org.apache.tomcat.embed:tomcat-embed-el:jar:7.0.59:compile
    [INFO] |  |  \- org.apache.tomcat.embed:tomcat-embed-websocket:jar:7.0.59:compile
    [INFO] |  +- org.springframework.boot:spring-boot-starter-validation:jar:1.4.1.RELEASE:compile
    [INFO] |  +- com.fasterxml.jackson.core:jackson-databind:jar:2.8.3:compile
    [INFO] |  |  +- com.fasterxml.jackson.core:jackson-annotations:jar:2.8.3:compile
    [INFO] |  |  \- com.fasterxml.jackson.core:jackson-core:jar:2.8.3:compile
    [INFO] |  +- org.springframework:spring-web:jar:4.3.3.RELEASE:compile
    [INFO] |  +- org.glassfish.jersey.core:jersey-server:jar:2.23.2:compile
    [INFO] |  |  +- org.glassfish.jersey.core:jersey-common:jar:2.23.2:compile
    [INFO] |  |  |  +- org.glassfish.jersey.bundles.repackaged:jersey-guava:jar:2.23.2:compile
    [INFO] |  |  |  \- org.glassfish.hk2:osgi-resource-locator:jar:1.0.1:compile
    [INFO] |  |  +- org.glassfish.jersey.core:jersey-client:jar:2.23.2:compile
    [INFO] |  |  +- javax.ws.rs:javax.ws.rs-api:jar:2.0.1:compile
    [INFO] |  |  +- org.glassfish.jersey.media:jersey-media-jaxb:jar:2.23.2:compile
    [INFO] |  |  +- javax.annotation:javax.annotation-api:jar:1.2:compile
    [INFO] |  |  +- org.glassfish.hk2:hk2-api:jar:2.5.0-b05:compile
    [INFO] |  |  |  +- org.glassfish.hk2:hk2-utils:jar:2.5.0-b05:compile
    [INFO] |  |  |  \- org.glassfish.hk2.external:aopalliance-repackaged:jar:2.5.0-b05:compile
    [INFO] |  |  +- org.glassfish.hk2.external:javax.inject:jar:2.5.0-b05:compile
    [INFO] |  |  +- org.glassfish.hk2:hk2-locator:jar:2.5.0-b05:compile
    [INFO] |  |  |  \- org.javassist:javassist:jar:3.20.0-GA:compile
    [INFO] |  |  \- javax.validation:validation-api:jar:1.1.0.Final:compile
    [INFO] |  +- org.glassfish.jersey.containers:jersey-container-servlet-core:jar:2.23.2:compile
    [INFO] |  +- org.glassfish.jersey.containers:jersey-container-servlet:jar:2.23.2:compile
    [INFO] |  +- org.glassfish.jersey.ext:jersey-bean-validation:jar:2.23.2:compile
    [INFO] |  +- org.glassfish.jersey.ext:jersey-spring3:jar:2.23.2:compile
    [INFO] |  |  +- org.glassfish.hk2:hk2:jar:2.5.0-b05:compile
    [INFO] |  |  |  +- org.glassfish.hk2:config-types:jar:2.5.0-b05:compile
    [INFO] |  |  |  +- org.glassfish.hk2:hk2-core:jar:2.5.0-b05:compile
    [INFO] |  |  |  +- org.glassfish.hk2:hk2-config:jar:2.5.0-b05:compile
    [INFO] |  |  |  +- org.glassfish.hk2:hk2-runlevel:jar:2.5.0-b05:compile
    [INFO] |  |  |  \- org.glassfish.hk2:class-model:jar:2.5.0-b05:compile
    [INFO] |  |  |     \- org.glassfish.hk2.external:asm-all-repackaged:jar:2.5.0-b05:compile
    [INFO] |  |  \- org.glassfish.hk2:spring-bridge:jar:2.5.0-b05:compile
    [INFO] |  \- org.glassfish.jersey.media:jersey-media-json-jackson:jar:2.23.2:compile
    [INFO] |     +- org.glassfish.jersey.ext:jersey-entity-filtering:jar:2.23.2:compile
    [INFO] |     +- com.fasterxml.jackson.jaxrs:jackson-jaxrs-base:jar:2.8.3:compile
    [INFO] |     \- com.fasterxml.jackson.jaxrs:jackson-jaxrs-json-provider:jar:2.8.3:compile
    [INFO] |        \- com.fasterxml.jackson.module:jackson-module-jaxb-annotations:jar:2.8.3:compile
    [INFO] +- org.springframework.boot:spring-boot-starter-actuator:jar:1.4.1.RELEASE:compile
    [INFO] |  \- org.springframework.boot:spring-boot-actuator:jar:1.4.1.RELEASE:compile
    [INFO] +- org.springframework.boot:spring-boot-starter-web:jar:1.4.1.RELEASE:compile
    [INFO] |  +- org.hibernate:hibernate-validator:jar:5.2.4.Final:compile
    [INFO] |  |  +- org.jboss.logging:jboss-logging:jar:3.3.0.Final:compile
    [INFO] |  |  \- com.fasterxml:classmate:jar:1.3.1:compile
    [INFO] |  \- org.springframework:spring-webmvc:jar:4.3.3.RELEASE:compile
    [INFO] +- org.springframework.boot:spring-boot-starter-test:jar:1.4.1.RELEASE:test
    [INFO] |  +- org.springframework.boot:spring-boot-test:jar:1.4.1.RELEASE:test
    [INFO] |  +- org.springframework.boot:spring-boot-test-autoconfigure:jar:1.4.1.RELEASE:test
    [INFO] |  +- com.jayway.jsonpath:json-path:jar:2.2.0:test
    [INFO] |  |  \- net.minidev:json-smart:jar:2.2.1:test
    [INFO] |  |     \- net.minidev:accessors-smart:jar:1.1:test
    [INFO] |  |        \- org.ow2.asm:asm:jar:5.0.3:test
    [INFO] |  +- org.assertj:assertj-core:jar:2.5.0:test
    [INFO] |  +- org.mockito:mockito-core:jar:1.10.19:test
    [INFO] |  |  \- org.objenesis:objenesis:jar:2.1:test
    [INFO] |  +- org.hamcrest:hamcrest-core:jar:1.3:test
    [INFO] |  +- org.hamcrest:hamcrest-library:jar:1.3:test
    [INFO] |  +- org.skyscreamer:jsonassert:jar:1.3.0:test
    [INFO] |  +- org.springframework:spring-core:jar:4.3.3.RELEASE:compile
    [INFO] |  \- org.springframework:spring-test:jar:4.3.3.RELEASE:test
    [INFO] +- commons-codec:commons-codec:jar:1.10:compile
    [INFO] +- com.googlecode.json-simple:json-simple:jar:1.1.1:compile
    [INFO] +- org.json:json:jar:20140107:compile
    [INFO] +- org.apache.logging.log4j:log4j-core:jar:2.6.2:compile
    [INFO] +- org.apache.logging.log4j:log4j-api:jar:2.6.2:compile
    [INFO] +- org.slf4j:slf4j-log4j12:jar:1.7.21:compile
    [INFO] |  +- org.slf4j:slf4j-api:jar:1.7.21:compile
    [INFO] |  \- log4j:log4j:jar:1.2.17:compile
    [INFO] +- junit:junit:jar:4.12:test
    [INFO] +- com.oracle:classes12:jar:10.2.0.5:provided
    [INFO] +- com.oracle:ojdbc14:jar:10.2.0.5:compile
    [INFO] +- org.apache.tomcat:tomcat-juli:jar:7.0.59:compile
    [INFO] +- org.apache.tomcat:tomcat-dbcp:jar:7.0.59:compile
    [INFO] +- javax.servlet:javax.servlet-api:jar:3.1.0:compile
    [INFO] \- org.apache.tomcat:tomcat-jdbc:jar:7.0.59:compile

1 个答案:

答案 0 :(得分:0)

它看起来像是一个类加载问题。通常这样的错误表明加载了相同库的两个版本。问题可能会“随机”发生,具体取决于您运行它的环境。

您可以在libs目录中检查生成的war文件是否包含类org.springframework.web.context.request.async.WebAsyncManager两次。

要检查pom.xml,您可以使用mvn dependency:tree,它会打印出所有包含的库:Maven: Resolving conflicts using the dependency tree