standaloneSetup(clrr).
build();

执行第二行 build() 时,出现下面的错误提示。

java.lang.NoClassDefFoundError: javax/servlet/SessionCookieConfig
at org.springframework.test.web.servlet.setup.StandaloneMockMvcBuilder.initWebAppContext(StandaloneMockMvcBuilder.java:329)
at org.springframework.test.web.servlet.setup.AbstractMockMvcBuilder.build(AbstractMockMvcBuilder.java:127)
at com.tony.springTest.HomeControllerTest.testHomeP(HomeControllerTest.java:34)
at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:39)
at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:25)
at java.lang.reflect.Method.invoke(Method.java:597)
at org.junit.runners.model.FrameworkMethod$1.runReflectiveCall(FrameworkMethod.java:44)
at org.junit.internal.runners.model.ReflectiveCallable.run(ReflectiveCallable.java:15)
at org.junit.runners.model.FrameworkMethod.invokeExplosively(FrameworkMethod.java:41)
at org.junit.internal.runners.statements.InvokeMethod.evaluate(InvokeMethod.java:20)
at org.junit.runners.BlockJUnit4ClassRunner.runChild(BlockJUnit4ClassRunner.java:76)
at org.junit.runners.BlockJUnit4ClassRunner.runChild(BlockJUnit4ClassRunner.java:50)
at org.junit.runners.ParentRunner$3.run(ParentRunner.java:193)
at org.junit.runners.ParentRunner$1.schedule(ParentRunner.java:52)
at org.junit.runners.ParentRunner.runChildren(ParentRunner.java:191)
at org.junit.runners.ParentRunner.access$000(ParentRunner.java:42)
at org.junit.runners.ParentRunner$2.evaluate(ParentRunner.java:184)
at org.junit.runners.ParentRunner.run(ParentRunner.java:236)
at org.eclipse.jdt.internal.junit4.runner.JUnit4TestReference.run(JUnit4TestReference.java:86)
at org.eclipse.jdt.internal.junit.runner.TestExecution.run(TestExecution.java:38)
at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.runTests(RemoteTestRunner.java:459)
at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.runTests(RemoteTestRunner.java:675)
at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.run(RemoteTestRunner.java:382)
at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.main(RemoteTestRunner.java:192)
Caused by: java.lang.ClassNotFoundException: javax.servlet.SessionCookieConfig
at java.net.URLClassLoader$1.run(URLClassLoader.java:202)
at java.security.AccessController.doPrivileged(Native Method)
at java.net.URLClassLoader.findClass(URLClassLoader.java:190)
at java.lang.ClassLoader.loadClass(ClassLoader.java:306)
at sun.misc.Launcher$AppClassLoader.loadClass(Launcher.java:301)
at java.lang.ClassLoader.loadClass(ClassLoader.java:247)
... 25 more

在 debug 模式下,可以知道 standaloneSetup(clrr) 得到的是 StandaloneMockMvcBuilder 对象和 exception 中报异常的类名一致。

在 Cannot create MockHttpSession with servlet 2.x, spring得到下面有用提示:Spring 4.0 需要得到 servlet 3.0 或以上版本的支持。

The Servlet API mocks in Spring Framework 4.0 support Servlet 3.0 and higher, as can be seen inGitHub commit deba32cad9.

追踪上面的 github 链接,可以发现 SessionCookieConfig 是在升级 servlet API 到 3.0 变动中新增的。

在查询SessionCookieConfig 文档,可以知道 SessionCookieConfig 是在 servlet 3.0 才引入的。

反观自己项目 pom.xml ,用的是 servlet 2.5 ,可以判断得到,是自己项目引用的 servlet 版本过低导致的,需要升级到 3.0 或以上。

        <!-- Servlet -->
<dependency>
<groupId>javax.servlet</groupId>
<artifactId>servlet-api</artifactId>
<version>2.5</version>
<scope>provided</scope>
</dependency>

变动 1 如下。

        <!-- Servlet -->
<dependency>
<groupId>javax.servlet</groupId>
<artifactId>servlet-api</artifactId>
<version>3.0.1</version>
<scope>provided</scope>
</dependency>

改动后,出现了新的两个错误 :

Missing artifact javax.servlet:servlet-api:jar: 3.0.1
Description    Resource    Path    Location    Type
The superclass "javax.servlet.http.HttpServlet" was not found on the Java Build Path home.jsp /SpringTest/src/main/webapp/WEB-INF/views line 1 JSP Problem
The superclass "javax.servlet.http.HttpServlet" was not found on the Java Build Path reg.jsp /SpringTest/src/main/webapp/WEB-INF/views line 1 JSP Problem
The superclass "javax.servlet.http.HttpServlet" was not found on the Java Build Path regSucess.jsp /SpringTest/src/main/webapp/WEB-INF/views line 1 JSP Problem
The superclass "javax.servlet.http.HttpServlet" was not found on the Java Build Path spittles.jsp /SpringTest/src/main/webapp/WEB-INF/views line 1 JSP Problem
HttpServlet 找不到是很不合理的,第一个错误表示无法找到 3.0.1 版本的 servlet 。

项目依赖是用 maven 管理的,通过 mvn clean,尝试解决无法找到 3.0.1 servlet artifact 问题。命令如下:
$ cd {project folder}
$ mvn clean compile

但是出现了新的错误:

[INFO] ------------------------------------------------------------------------
[ERROR] Failed to execute goal on project springTest: Could not resolve dependencies for project com.tony:springTest:war:1.0.-BUILD-SNAPSHOT: Failure to find javax.servlet:servlet-api:jar:3.0. in https://repo.maven.apache.org/maven2/ was cached in the local repository, resolution will not be reattempted until the update interval of maven.dev.snaponglobal.com has elapsed or updates are forced -> [Help 1]

错误提示 maven repository 无法找到需要的 3.0.1 servlet 。在 repository URL : https://repo.maven.apache.org/ 下面一级一级找,确实没有 servlet 3.0.1 版本的 artifiact 。那么就需要给 maven 换一个有 3.0.1 servlet 的 repository 或者 mirror 。

在网上找到一个有效的 servlet 3.0.1 : http://repo1.maven.org/maven2/javax/servlet/javax.servlet-api/。

于是在 ~/.m2/setting.xml 中将 maven 的 mirror 改为 repo1 的 。如下:

    <mirrors>
<mirror>
<id>maven.dev.snaponglobal.com</id>
<name>Internal Artifactory Maven repository</name>
<url>http://repo1.maven.org/maven2/</url>
<mirrorOf>*</mirrorOf>
</mirror>
</mirrors>

修改好 mirror 后,还是提示无法解决依赖的 3.0.1 servlet :

Downloading: http://repo1.maven.org/maven2/javax/servlet/servlet-api/3.0.1/servlet-api-3.0.1.jar
[INFO] ------------------------------------------------------------------------
[INFO] BUILD FAILURE
[INFO] ------------------------------------------------------------------------
[INFO] Total time: 4.511 s
[INFO] Finished at: --09T00::+:
[INFO] Final Memory: 11M/245M
[INFO] ------------------------------------------------------------------------
[ERROR] Failed to execute goal on project springTest: Could not resolve dependencies for project com.tony:springTest:war:1.0.-BUILD-SNAPSHOT: Could not find artifact javax.servlet:servlet-api:jar:3.0. in maven.dev.snaponglobal.com (http://repo1.maven.org/maven2/) -> [Help 1]

通过仔细对比错误提示的 URL 和 浏览器的 URL ,发现路径有点不一样。

错误提示的是 /servlet/servlet-api/ ,而浏览器的实际路径为 /servlet/javax.servlet-api/ ,将 pom.xml 中的 artifactId 纠正过来就好了。纠正后配置如下 :

        <!-- Servlet -->
<dependency>
<groupId>javax.servlet</groupId>
<artifactId>javax.servlet-api</artifactId>
<version>3.0.1</version>
<scope>provided</scope>
</dependency>

重新运行 mvn clean 后,可以成功下载 3.0.1 servlet 。然后运行 Spring 的 unit test 也没也报错。

问题解决完毕。

Spring 4.0 StandaloneMockMvcBuilder java.lang.NoClassDefFoundError: javax/servlet/SessionCookieConfig 问题解决的更多相关文章

  1. Spring boot ---- java.lang.NoClassDefFoundError: javax/servlet/ServletContext

    Spring boot ---- java.lang.NoClassDefFoundError: javax/servlet/ServletContext   场景描述 项目中用到spring boo ...

  2. 【异常】idea执行Main方法出现 Exception in thread "main" java.lang.NoClassDefFoundError: javax/servlet/http/HttpServletRequest

    一.异常复现步骤 1)首先得是一个Spring MVC项目 注:Spring Boot项目有内置的web 容器,不会出现该问题 2)main方法存在于使用HttpServletRequest类的类中 ...

  3. java.lang.NoClassDefFoundError: javax/servlet/ServletContext

    方法1:把SpringBoot中main方法所在的class不再继承org.springframework.boot.context.web.SpringBootServletInitializer即 ...

  4. java.lang.NoClassDefFoundError: javax/servlet/AsyncContext

    报错:java.lang.NoClassDefFoundError: javax/servlet/AsyncContext 我认为你需要在Servlet API,而不是2.5.AsyncContext ...

  5. java.lang.NoClassDefFoundError: javax/servlet/AsyncListener解决方案

    问题:spring3.2的架构在tomcat6.0中无法正常启动,抛出java.lang.NoClassDefFoundError: javax/servlet/AsyncListener错误 原因: ...

  6. java.lang.NoClassDefFoundError: javax/servlet/jsp/jstl/core/Config

    今天写SpringMvc时,遇到这样一个问题: java.lang.NoClassDefFoundError: javax/servlet/jsp/jstl/core/Config at org.sp ...

  7. Handler processing failed; nested exception is java.lang.NoClassDefFoundError: javax/servlet/jsp/jstl/core/Config解决

    出现这个问题往往伴随  HTTP-500错误 报错信息: HTTP Status - Handler processing failed; nested exception is java.lang. ...

  8. HTTP Status 500 - javax.servlet.ServletException: java.lang.NoClassDefFoundError: javax/servlet/jsp/jstl/core/LoopTag

    我在项目中导入了jar,还是不能使用EL表达式,一运行就出现了下面的额错误: org.apache.jasper.JasperException: javax.servlet.ServletExcep ...

  9. java.lang.NoClassDefFoundError: javax/el/ELResolver 问题解决

    HTTP Status 500 - java.lang.NoClassDefFoundError: javax/el/ELResolver type Exception report message ...

随机推荐

  1. NSIndexPath初始化

    在UITableView中经常用到这个类,但一直不知道怎么初始化,网上抄录的代码如下,果然好用 NSIndexPath *index = [NSIndexPath indexPathForRow:0 ...

  2. Java基础知识强化之IO流笔记20:FileOutputStream写出数据实现换行和追加写入

    1.  如何实现数据的换行? (1) package com.himi.fileoutputstream; import java.io.FileNotFoundException; import j ...

  3. python----------反射和设计模式

    反射: 把字符串映动态射成对象内存地址. hasattr():判断一个对象里是否有对应的字符串的方法 getattr():根据字符串去获取obj对象里的对应方法的内存地址. class Dog(obj ...

  4. python-增删改查

    ###增删改查 names = ["zhangding","wangxu","wudong","cheng"] #增 n ...

  5. C#解leetcode 11. Container With Most Water

    Given n non-negative integers a1, a2, ..., an, where each represents a point at coordinate (i, ai).  ...

  6. jQueryUI 日期控件

    <!DOCTYPE html><html><head><meta charset="UTF-8"><title>Inse ...

  7. js求字符长度

    </script> <!DOCTYPE html> <html lang="en"> <head> <meta charset ...

  8. SQL批量信息保存(XML格式字符串数据)

    /* *功能:SQL批量信息录入 *此存储过程获取表单信息,插入表中.*/CREATE  PROC [dbo].[sp_SaveToMX1]@XML text   --明细表XML字符串信息ASBEG ...

  9. Oracle解析 xml 记录一下(未完待续)

    Oracle解析 xml 记录一下. SQL> desc xmlparser; PROCEDURE FREEPARSER Argument Name                  Type  ...

  10. 安卓 报错 Check the Eclipse log for stack trace.

    移除Android Dependencies就行了. 在properties的Libraries里