【问题标题】:NoSuchMethodError while calling response.getStatus()调用 response.getStatus() 时出现 NoSuchMethodError
【发布时间】:2017-09-20 15:51:47
【问题描述】:

我想这是某种依赖问题,所以这是我的树:

[INFO] Sikor:BrowserGame:war:1.0-SNAPSHOT
[INFO] +- junit:junit:jar:3.8.1:test
[INFO] +- org.springframework:spring-context:jar:4.1.1.RELEASE:compile
[INFO] |  +- org.springframework:spring-aop:jar:4.1.1.RELEASE:compile
[INFO] |  +- org.springframework:spring-beans:jar:4.1.1.RELEASE:compile
[INFO] |  +- org.springframework:spring-core:jar:4.1.1.RELEASE:compile
[INFO] |  |  \- commons-logging:commons-logging:jar:1.1.3:compile
[INFO] |  \- org.springframework:spring-expression:jar:4.1.1.RELEASE:compile
[INFO] +- org.springframework:spring-webmvc:jar:4.1.1.RELEASE:compile
[INFO] |  \- org.springframework:spring-web:jar:4.1.1.RELEASE:compile
[INFO] +- org.springframework:spring-context-support:jar:4.1.1.RELEASE:compile
[INFO] +- org.springframework.security:spring-security-web:jar:3.2.5.RELEASE:compile
[INFO] |  +- aopalliance:aopalliance:jar:1.0:compile
[INFO] |  \- org.springframework.security:spring-security-core:jar:3.2.5.RELEASE:compile
[INFO] +- org.springframework.security:spring-security-config:jar:3.2.5.RELEASE:compile
[INFO] +- org.springframework.security:spring-security-taglibs:jar:3.2.5.RELEASE:compile
[INFO] |  \- org.springframework.security:spring-security-acl:jar:3.2.5.RELEASE:compile
[INFO] +- org.thymeleaf:thymeleaf-spring4:jar:2.1.3.RELEASE:compile
[INFO] |  \- org.thymeleaf:thymeleaf:jar:2.1.3.RELEASE:compile
[INFO] |     +- ognl:ognl:jar:3.0.6:compile
[INFO] |     \- org.unbescape:unbescape:jar:1.0:compile
[INFO] +- org.thymeleaf.extras:thymeleaf-extras-springsecurity3:jar:2.1.1.RELEASE:compile
[INFO] +- nz.net.ultraq.thymeleaf:thymeleaf-layout-dialect:jar:1.2.7:compile
[INFO] +- org.hibernate:hibernate-entitymanager:jar:4.3.5.FINAL:compile
[INFO] |  +- org.jboss.logging:jboss-logging:jar:3.1.3.GA:compile
[INFO] |  +- org.jboss.logging:jboss-logging-annotations:jar:1.2.0.Beta1:compile
[INFO] |  +- org.hibernate:hibernate-core:jar:4.3.5.Final:compile
[INFO] |  |  +- antlr:antlr:jar:2.7.7:compile
[INFO] |  |  \- org.jboss:jandex:jar:1.1.0.Final:compile
[INFO] |  +- dom4j:dom4j:jar:1.6.1:compile
[INFO] |  |  \- xml-apis:xml-apis:jar:1.0.b2:compile
[INFO] |  +- org.hibernate.common:hibernate-commons-annotations:jar:4.0.4.Final:compile
[INFO] |  +- org.hibernate.javax.persistence:hibernate-jpa-2.1-api:jar:1.0.0.Final:compile
[INFO] |  +- org.jboss.spec.javax.transaction:jboss-transaction-api_1.2_spec:jar:1.0.0.Final:compile
[INFO] |  \- org.javassist:javassist:jar:3.18.1-GA:compile
[INFO] +- org.hibernate:hibernate-validator:jar:4.3.2.Final:compile
[INFO] |  \- javax.validation:validation-api:jar:1.0.0.GA:compile
[INFO] +- mysql:mysql-connector-java:jar:5.1.32:compile
[INFO] +- org.springframework:spring-orm:jar:4.1.1.RELEASE:compile
[INFO] |  +- org.springframework:spring-jdbc:jar:4.1.1.RELEASE:compile
[INFO] |  \- org.springframework:spring-tx:jar:4.1.1.RELEASE:compile
[INFO] +- org.springframework.data:spring-data-jpa:jar:1.6.4.RELEASE:compile
[INFO] |  +- org.springframework.data:spring-data-commons:jar:1.8.4.RELEASE:compile
[INFO] |  +- org.aspectj:aspectjrt:jar:1.8.1:compile
[INFO] |  \- org.slf4j:jcl-over-slf4j:jar:1.7.7:runtime
[INFO] +- javax.servlet:javax.servlet-api:jar:3.1.0:provided
[INFO] +- org.slf4j:slf4j-api:jar:1.7.5:compile
[INFO] +- org.slf4j:slf4j-log4j12:jar:1.6.1:compile
[INFO] |  \- log4j:log4j:jar:1.2.16:compile
[INFO] \- javax.mail:mail:jar:1.4:compile
[INFO]    \- javax.activation:activation:jar:1.1:compile

据我所知,spring 4.1.1 应该与 javax.servlet 3.1.0 一起工作,但我得到 java.lang.NoSuchMethodError: javax.servlet.http.HttpServletResponse.getStatus()I 在我的拦截器中调用 response.getStatus() 时。
如果有人可以提供帮助,我会很高兴。提前致谢。

编辑:添加了 web.xml

<?xml version="1.0" encoding="UTF-8"?>
<web-app version="2.5" xmlns="http://java.sun.com/xml/ns/javaee"
         xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
         xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd">

    <servlet>
        <servlet-name>mvc-dispatcher</servlet-name>
        <servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
        <init-param>
            <param-name>contextConfigLocation</param-name>
            <param-value>/WEB-INF/config/mvc-dispatcher-servlet.xml</param-value>
        </init-param>
        <init-param>
            <param-name>throwExceptionIfNoHandlerFound</param-name>
            <param-value>true</param-value>
        </init-param>
        <load-on-startup>1</load-on-startup>
    </servlet>
    <servlet-mapping>
        <servlet-name>mvc-dispatcher</servlet-name>
        <url-pattern>/</url-pattern>
    </servlet-mapping>

    <filter>
        <filter-name>springSecurityFilterChain</filter-name>
        <filter-class>org.springframework.web.filter.DelegatingFilterProxy</filter-class>
    </filter>
    <filter-mapping>
        <filter-name>springSecurityFilterChain</filter-name>
        <url-pattern>/*</url-pattern>
    </filter-mapping>

    <listener>
        <listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
    </listener>

    <context-param>
        <param-name>contextConfigLocation</param-name>
        <param-value>
            /WEB-INF/config/root-context.xml
        </param-value>
    </context-param>

</web-app>



EDIT2: 更改了以下内容:

<web-app version="2.5" xmlns="http://java.sun.com/xml/ns/javaee"
             xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
             xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd">

到:

<web-app xmlns="http://xmlns.jcp.org/xml/ns/javaee"
         xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
         xsi:schemaLocation="http://xmlns.jcp.org/xml/ns/javaee http://xmlns.jcp.org/xml/ns/javaee/web-app_3_1.xsd"
         version="3.1">

但我在调用 response.getStatus(); 时仍然收到错误消息;

【问题讨论】:

  • 你检查过你在 web.xml 中使用的 Servlet 版本吗?
  • 是的,这种错误几乎总是意味着代码是针对一个版本的依赖项编译的,但最终会针对该依赖项的不同版本运行。几乎可以肯定,servlet-api 3 不是在运行时提供的。
  • 添加了我的 web.xml。我尝试将 web-app 版本更改为 3.1,但它给了我一个错误,因此更改为 3.0 但 NoSuchMethodError 仍然存在。
  • 你应该知道有两个改变要做:xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_3_0.xsd"version="3.0"
  • 您是否正在部署到还不支持 Servlet API 3.0 的容器中?你能看一下你的容器提供的servlet.jar吗?你用什么做容器?

标签: java spring servlets response


【解决方案1】:

我在一个简单的 tomcat:run 上运行我的应用程序。在将其部署到战争并确保我使用的是 Tomcat 7 之后,一切都很好。感谢所有评论员,您为我指明了正确的方向。干杯。

编辑:请记住,tomcat 7 不支持 servlet 3.1,因此您必须将 servlet 3.0 与 tomcat 7 或 servlet 3.1 和 tomcat 8 一起使用。

【讨论】:

    【解决方案2】:

    这是4.1.1版本已经解决的问题,使用4.1.1或更高版本,或者设置DispatcherServlet bean,设置publishEventsfalse

    这是4.1.1版本已经解决的问题,请使用 4.1.1 或更高版本,或设置DispatcherServlet bean,设置 publishEventsfalse

    【讨论】:

      【解决方案3】:

      我有同样的问题,它与 groovy 有关。我使用来自sdk 源的IDE 将groovy 添加到类路径(外部库)中,而不是使用mavengradle 依赖项。

      似乎HttpServletResponse 附带groovy 的依赖项没有getStatus() 方法。所以最后,我从类路径中删除并将它添加到 maven 中并且它可以工作。

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 2015-12-23
        • 1970-01-01
        • 2016-09-20
        • 1970-01-01
        • 2016-09-14
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多