-
spring mvc 3.1 controller 测试报异常0
package yunnex.dining.client; import javax.servlet.http.HttpServletRequest; import org.junit.Before; import org.junit.Test; import org.junit.runner.RunWith; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.mock.web.MockHttpServletRequest; import org.springframework.mock.web.MockHttpServletResponse; import org.springframework.test.context.ContextConfiguration; import org.springframework.test.context.junit4.SpringJUnit4ClassRunner; import org.springframework.util.Assert; import org.springframework.web.method.HandlerMethod; import org.springframework.web.servlet.mvc.method.annotation.RequestMappingHandlerAdapter; import yunnex.dining.client.controller.MainController; @RunWith(SpringJUnit4ClassRunner.class) @ContextConfiguration(locations = {"/applicationContext.xml", "/spring-mvc-servlet.xml"}) public class MainControllerTest { @Autowired private MainController mainController; @Autowired private RequestMappingHandlerAdapter handlerAdapter; private MockHttpServletRequest request; private MockHttpServletResponse response; @Before public void setUp() { request = new MockHttpServletRequest(); response = new MockHttpServletResponse(); } @Test public void testFood() throws Exception { request.setRequestURI("/desk"); request.setContentType("application/json"); request.addHeader("DeviceId", "XXXXXXXXXXXXXXXX"); handlerAdapter.handle(request, response, new HandlerMethod(mainController, "desk", Long.class, HttpServletRequest.class)); Assert.notNull(response.getContentAsString()); } }
以上代码为我的测试代码,运行的时候一直报错:org.springframework.beans.factory.BeanExpressionException: Expression parsing failed; nested exception is java.lang.IllegalStateException: No thread-bound request found: Are you referring to request attributes outside of an actual web request, or processing a request outside of the originally receiving thread? If you are actually operating within a web request and still receive this message, your code is probably running outside of DispatcherServlet/DispatcherPortlet: In this case, use RequestContextListener or RequestContextFilter to expose the current request. at org.springframework.context.expression.StandardBeanExpressionResolver.evaluate(StandardBeanExpressionResolver.java:142) at org.springframework.web.method.annotation.AbstractNamedValueMethodArgumentResolver.resolveDefaultValue(AbstractNamedValueMethodArgumentResolver.java:165) at org.springframework.web.method.annotation.AbstractNamedValueMethodArgumentResolver.resolveArgument(AbstractNamedValueMethodArgumentResolver.java:87) at org.springframework.web.method.support.HandlerMethodArgumentResolverComposite.resolveArgument(HandlerMethodArgumentResolverComposite.java:75) at org.springframework.web.method.support.InvocableHandlerMethod.getMethodArgumentValues(InvocableHandlerMethod.java:162) at org.springframework.web.method.support.InvocableHandlerMethod.invokeForRequest(InvocableHandlerMethod.java:123) at org.springframework.web.servlet.mvc.method.annotation.ServletInvocableHandlerMethod.invokeAndHandle(ServletInvocableHandlerMethod.java:100) at org.springframework.web.servlet.mvc.method.annotation.RequestMappingHandlerAdapter.invokeHandlerMethod(RequestMappingHandlerAdapter.java:604) at org.springframework.web.servlet.mvc.method.annotation.RequestMappingHandlerAdapter.handleInternal(RequestMappingHandlerAdapter.java:565) at org.springframework.web.servlet.mvc.method.AbstractHandlerMethodAdapter.handle(AbstractHandlerMethodAdapter.java:80) at yunnex.dining.client.MainControllerTest.testFood(MainControllerTest.java:50) at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method) at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:57) at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43) at java.lang.reflect.Method.invoke(Method.java:606) at org.junit.runners.model.FrameworkMethod$1.runReflectiveCall(FrameworkMethod.java:47) at org.junit.internal.runners.model.ReflectiveCallable.run(ReflectiveCallable.java:12) at org.junit.runners.model.FrameworkMethod.invokeExplosively(FrameworkMethod.java:44) at org.junit.internal.runners.statements.InvokeMethod.evaluate(InvokeMethod.java:17) at org.junit.internal.runners.statements.RunBefores.evaluate(RunBefores.java:26) at org.springframework.test.context.junit4.statements.RunBeforeTestMethodCallbacks.evaluate(RunBeforeTestMethodCallbacks.java:74) at org.springframework.test.context.junit4.statements.RunAfterTestMethodCallbacks.evaluate(RunAfterTestMethodCallbacks.java:83) at org.springframework.test.context.junit4.statements.SpringRepeat.evaluate(SpringRepeat.java:72) at org.springframework.test.context.junit4.SpringJUnit4ClassRunner.runChild(SpringJUnit4ClassRunner.java:231) at org.junit.runners.BlockJUnit4ClassRunner.runChild(BlockJUnit4ClassRunner.java:50) at org.junit.runners.ParentRunner$3.run(ParentRunner.java:238) at org.junit.runners.ParentRunner$1.schedule(ParentRunner.java:63) at org.junit.runners.ParentRunner.runChildren(ParentRunner.java:236) at org.junit.runners.ParentRunner.access$000(ParentRunner.java:53) at org.junit.runners.ParentRunner$2.evaluate(ParentRunner.java:229) at org.springframework.test.context.junit4.statements.RunBeforeTestClassCallbacks.evaluate(RunBeforeTestClassCallbacks.java:61) at org.springframework.test.context.junit4.statements.RunAfterTestClassCallbacks.evaluate(RunAfterTestClassCallbacks.java:71) at org.junit.runners.ParentRunner.run(ParentRunner.java:309) at org.springframework.test.context.junit4.SpringJUnit4ClassRunner.run(SpringJUnit4ClassRunner.java:174) at org.eclipse.jdt.internal.junit4.runner.JUnit4TestReference.run(JUnit4TestReference.java:50) at org.eclipse.jdt.internal.junit.runner.TestExecution.run(TestExecution.java:38) at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.runTests(RemoteTestRunner.java:467) at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.runTests(RemoteTestRunner.java:683) at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.run(RemoteTestRunner.java:390) at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.main(RemoteTestRunner.java:197) Caused by: java.lang.IllegalStateException: No thread-bound request found: Are you referring to request attributes outside of an actual web request, or processing a request outside of the originally receiving thread? If you are actually operating within a web request and still receive this message, your code is probably running outside of DispatcherServlet/DispatcherPortlet: In this case, use RequestContextListener or RequestContextFilter to expose the current request. at org.springframework.web.context.request.RequestContextHolder.currentRequestAttributes(RequestContextHolder.java:131) at org.springframework.web.context.request.AbstractRequestAttributesScope.resolveContextualObject(AbstractRequestAttributesScope.java:67) at org.springframework.beans.factory.config.BeanExpressionContext.containsObject(BeanExpressionContext.java:51) at org.springframework.context.expression.BeanExpressionContextAccessor.canRead(BeanExpressionContextAccessor.java:36) at org.springframework.expression.spel.ast.PropertyOrFieldReference.readProperty(PropertyOrFieldReference.java:190) at org.springframework.expression.spel.ast.PropertyOrFieldReference.getValueInternal(PropertyOrFieldReference.java:72) at org.springframework.expression.spel.ast.CompoundExpression.getValueInternal(CompoundExpression.java:52) at org.springframework.expression.spel.ast.SpelNodeImpl.getValue(SpelNodeImpl.java:93) at org.springframework.expression.spel.standard.SpelExpression.getValue(SpelExpression.java:88) at org.springframework.context.expression.StandardBeanExpressionResolver.evaluate(StandardBeanExpressionResolver.java:139) ... 39 more
这个例子是我参考文档写出来的,检查了 好多遍了。麻烦大家指教。2014年6月27日 13:19
目前还没有答案
相关推荐
在Spring MVC中,我们可以使用@Controller和@RequestMapping等注解来声明控制器类和处理方法。@RequestMapping注解用于映射HTTP请求到特定的方法,这极大地提高了代码的可读性和维护性。此外,还可以通过@...
7. **MVC Test Framework**:Spring 3.1引入了专门用于测试Spring MVC控制器的测试框架,简化了单元测试和集成测试的编写。 8. **Web Flow Integration**:Spring 3.1加强了与Spring Web Flow的整合,使得构建复杂...
一个简单SpringMVC例子,从请求到controller跳转再到页面显示能完整执行成功。使用myeclipse6.0搭建,使用Spring3.1部分jar包,使用jdk1.6.0_45和自带的myeclipse tomcat。
它采用模型-视图-控制器(MVC)设计模式,将业务逻辑、数据和用户界面分离,提高了代码的可维护性和可测试性。SpringMVC通过DispatcherServlet作为前端控制器,接收HTTP请求,然后根据请求信息分发到相应的处理器,...
Spring MVC提供专门的测试框架,使得对控制器的单元测试和集成测试变得简单。 在实践过程中,你需要创建一个Spring MVC工程,配置web.xml以加载`DispatcherServlet`,然后按照上述步骤编写控制器、视图和模型数据...
- **Spring MVC集成**:在Spring MVC项目中,Spring Security可以轻松集成,保护Controller方法。 - **Remember Me服务**:实现自动登录功能,通过设置cookie存储用户的登录状态。 - **CSRF防护**:防止跨站请求...
标题中的“spring3.1 mvc jdbc 合成”指的是在Spring 3.1版本中,如何集成使用Spring MVC框架和JDBC进行数据访问的技术。Spring MVC是Spring框架的一个核心模块,用于构建Web应用程序,而JDBC(Java Database ...
4. **MVC(Model-View-Controller)**:Spring MVC是Spring框架的Web层组件,用于构建RESTful Web服务和Web应用程序。Spring 3.1 提供了更好的URL映射、模型绑定和数据验证功能,增强了视图解析,支持更多的视图技术...
此外,Spring 3.1引入了更多关于bean的注解,如`@Component`、`@Service`、`@Repository`和`@Controller`,这些注解简化了bean的声明和自动扫描,使得基于组件的开发更加便捷。 除了这些,这个集合还可能包括其他...
Spring MVC 是 Spring 框架的一部分,主要用于构建基于 Model-View-Controller (MVC) 设计模式的 Web 应用程序。它提供了丰富的功能来简化 Web 开发过程,包括请求映射、视图解析、表单绑定等。本文将详细介绍 ...
- spring-test 和 mockito 等库用于编写单元测试和集成测试,确保 Spring MVC 应用的功能正确性。 以上这些 jar 包组合在一起,构成了一个完整的 Spring MVC 开发环境,涵盖了从请求处理到视图渲染,再到数据持久...
在构建Web应用程序时,"spring mvc3.2.7 + servlet3.1(jetty) + maven3 + eclipse"的组合提供了一种高效且灵活的开发环境。这个配置涉及了多个关键组件,它们共同作用于项目的搭建、管理和运行。 **Spring MVC ...
3. **Web**:Spring的Web模块包含MVC(Model-View-Controller)框架,用于构建可重用、松耦合的Web应用程序。它提供了一个强大的调度器,支持多种视图技术,如JSP、FreeMarker和Thymeleaf。 4. **AOP**:面向切面...
1. spring-webmvc-3.1.x.jar:Spring MVC的核心库,包含了Controller、ViewResolver、DispatcherServlet等关键组件。 2. spring-context-3.1.x.jar:提供ApplicationContext接口,管理bean的生命周期和依赖注入。 3....
Spring MVC is a modern web application framework built upon the Spring Framework, and Spring Web Flow is a project that complements Spring MVC for building reusable web controller modules that ...
在Spring MVC 3.1版本中,它引入了多项增强功能和改进,以提升性能和开发效率。多视图是指在同一个应用中支持多种不同的展现方式,如JSP和FreeMarker,这为开发者提供了更大的灵活性。 **1. 视图解析器(View ...
通过这个过程,我们创建了一个简单的Spring MVC应用,并配置了必要的组件,如Controller、DispatcherServlet和视图解析器。随着对Spring MVC的深入学习,你可以添加更多的功能,如数据访问、模板引擎、安全控制等,...
`spring-mvc-3.1.xsd` 文件定义了与 MVC 相关的配置元素,如控制器(Controller)、视图解析器(View Resolver)、模型-视图映射(Model-View-Mapping)等。通过版本号 3.1,我们可以看到这是针对 Spring 3.1 版本的...
前者包含HTTP上下文、请求参数处理等功能,后者则是Spring的Model-View-Controller(MVC)框架,负责处理HTTP请求,提供视图解析,使Web应用开发更为简洁。 此外,`spring-orm.jar`和`spring-oxm.jar`分别提供了与...
10. **集成Spring MVC**:Spring Security 可无缝集成到Spring MVC框架中,提供对Controller方法级别的安全控制,同时能够与Spring的其他模块协同工作,如Spring AOP、Spring Data等。 以上只是Spring Security 3.1...