import org.junit.Before; import org.junit.Test; import org.junit.runner.RunWith; import org.springframework.http.MediaType; import org.springframework.test.context.junit4.SpringJUnit4ClassRunner; import org.springframework.test.web.servlet.MockMvc; import org.springframework.test.web.servlet.setup.MockMvcBuilders; import static org.springframework.test.web.servlet.request.MockMvcRequestBuilders.*; import com.lly.test.user.AbstractContextControllerTests; @RunWith(SpringJUnit4ClassRunner.class) public class UserControllerTest extends AbstractContextControllerTests { private static String URI = "/netloan/userManage/saveUserInfo"; private MockMvc mockMvc; private String json ="{\"userName\":luhiahua,\"passWord\":1235,\"certCode\":\"320681198903167015\"}"; @Before public void setup() { this.mockMvc = MockMvcBuilders.webAppContextSetup(this.wac).build(); } @Test public void addUserInfo() throws Exception { String ret = this.mockMvc.perform( post(URI,"json") .characterEncoding("UTF-8") .contentType(MediaType.APPLICATION_JSON) .content(json.getBytes())).andReturn() .getResponse().getContentAsString() ; System.out.println("===================:"+ret); } }
以上是源码:
JunitTest 日志:
2013-12-30 11:00:31,514 INFO [org.springframework.beans.factory.xml.XmlBeanDefinitionReader] - Loading XML bean definitions from class path resource [spring-dispather.xml] 2013-12-30 11:00:32,717 INFO [org.springframework.beans.factory.xml.XmlBeanDefinitionReader] - Loading XML bean definitions from class path resource [spring-base.xml] 2013-12-30 11:00:33,117 INFO [org.springframework.beans.factory.xml.XmlBeanDefinitionReader] - Loading XML bean definitions from class path resource [spring-dao.xml] 2013-12-30 11:00:33,273 INFO [org.springframework.web.context.support.GenericWebApplicationContext] - Refreshing org.springframework.web.context.support.GenericWebApplicationContext@b6e39f: startup date [Mon Dec 30 11:00:33 CST 2013]; root of context hierarchy 2013-12-30 11:00:33,727 INFO [org.springframework.beans.factory.config.PropertyPlaceholderConfigurer] - Loading properties file from class path resource [jdbc.properties] 2013-12-30 11:00:33,728 INFO [org.springframework.beans.factory.config.PropertyPlaceholderConfigurer] - Loading properties file from class path resource [hibernate.properties] 2013-12-30 11:00:34,222 INFO [org.springframework.beans.factory.support.DefaultListableBeanFactory] - Pre-instantiating singletons in org.springframework.beans.factory.support.DefaultListableBeanFactory@54083e1e: defining beans [mvcContentNegotiationManager,org.springframework.web.servlet.mvc.method.annotation.RequestMappingHandlerMapping#0,org.springframework.web.servlet.mvc.method.annotation.RequestMappingHandlerAdapter#0,org.springframework.web.servlet.handler.MappedInterceptor#0,org.springframework.web.servlet.mvc.method.annotation.ExceptionHandlerExceptionResolver#0,org.springframework.web.servlet.mvc.annotation.ResponseStatusExceptionResolver#0,org.springframework.web.servlet.mvc.support.DefaultHandlerExceptionResolver#0,org.springframework.web.servlet.handler.BeanNameUrlHandlerMapping,org.springframework.web.servlet.mvc.HttpRequestHandlerAdapter,org.springframework.web.servlet.mvc.SimpleControllerHandlerAdapter,validator,conversion-service,springApplicationContextHolder,globalExceptionResolver,CommonHibernateDao,generalHibernateDao,genericHibernateDao,hibernateMatchRuleContext,dictController,dictInfoDaoImpl,fileUploadController,springController,targetController,targetUserRelaDaoImpl,userController,userDaoImpl,org.springframework.context.annotation.internalConfigurationAnnotationProcessor,org.springframework.context.annotation.internalAutowiredAnnotationProcessor,org.springframework.context.annotation.internalRequiredAnnotationProcessor,org.springframework.context.annotation.internalCommonAnnotationProcessor,org.springframework.context.annotation.internalPersistenceAnnotationProcessor,org.springframework.web.servlet.resource.ResourceHttpRequestHandler#0,org.springframework.web.servlet.handler.SimpleUrlHandlerMapping#0,org.springframework.web.servlet.handler.MappedInterceptor#1,org.springframework.web.servlet.handler.MappedInterceptor#2,org.springframework.web.servlet.config.viewControllerHandlerMapping,org.springframework.web.servlet.mvc.annotation.DefaultAnnotationHandlerMapping#0,handlerAdapter,org.springframework.web.servlet.view.ContentNegotiatingViewResolver#0,multipartResolver,exceptionResolver,CommonService,dictServiceImpl,targetServiceImpl,userServiceImpl,propertyConfigurer,messageSource,dataSource,sessionFactory,org.springframework.aop.config.internalAutoProxyCreator,org.springframework.transaction.annotation.AnnotationTransactionAttributeSource#0,org.springframework.transaction.interceptor.TransactionInterceptor#0,org.springframework.transaction.config.internalTransactionAdvisor,txManager,txAdvice,txPointcut,org.springframework.aop.support.DefaultBeanFactoryPointcutAdvisor#0,org.springframework.context.annotation.ConfigurationClassPostProcessor.importAwareProcessor]; root of factory hierarchy 2013-12-30 11:00:34,486 INFO [org.springframework.web.servlet.mvc.method.annotation.RequestMappingHandlerMapping] - Mapped "{[/netloan/dictManage/findByDictType/{dictType}],methods=[GET],params=[],headers=[],consumes=[],produces=[],custom=[]}" onto public java.lang.Object com.lly.netloan.system.dict.controller.DictController.findByDictType(java.lang.String) throws java.lang.Exception 2013-12-30 11:00:34,488 INFO [org.springframework.web.servlet.mvc.method.annotation.RequestMappingHandlerMapping] - Mapped "{[/netloan/dictManage/findByParentDictId/{parentDictId}],methods=[GET],params=[],headers=[],consumes=[],produces=[],custom=[]}" onto public java.lang.Object com.lly.netloan.system.dict.controller.DictController.findByParentDictId(java.lang.String) throws java.lang.Exception 2013-12-30 11:00:34,491 INFO [org.springframework.web.servlet.mvc.method.annotation.RequestMappingHandlerMapping] - Mapped "{[/netloan/fileUpload],methods=[POST],params=[],headers=[],consumes=[],produces=[],custom=[]}" onto public java.lang.Object com.lly.netloan.system.fileservice.controller.FileUploadController.file(com.lly.netloan.system.user.entity.User) throws java.lang.Exception 2013-12-30 11:00:34,494 INFO [org.springframework.web.servlet.mvc.method.annotation.RequestMappingHandlerMapping] - Mapped "{[/spring/url-mapping],methods=[],params=[],headers=[],consumes=[],produces=[],custom=[]}" onto public java.lang.Object com.lly.netloan.system.spring.controller.SpringController.getURLMapping() 2013-12-30 11:00:34,497 INFO [org.springframework.web.servlet.mvc.method.annotation.RequestMappingHandlerMapping] - Mapped "{[/netloan/targetManage],methods=[POST],params=[],headers=[],consumes=[],produces=[],custom=[]}" onto public java.lang.Object com.lly.netloan.system.target.controller.TargetController.saveTur(com.lly.netloan.system.target.entity.TargetUserRela) throws java.lang.Exception 2013-12-30 11:00:34,501 INFO [org.springframework.web.servlet.mvc.method.annotation.RequestMappingHandlerMapping] - Mapped "{[/netloan/userManage/saveUserInfo],methods=[POST],params=[],headers=[],consumes=[application/json],produces=[],custom=[]}" onto public java.lang.Object com.lly.netloan.system.user.controller.UserController.saveUserInfo(com.lly.netloan.system.user.entity.User) throws java.lang.Exception 2013-12-30 11:00:34,503 INFO [org.springframework.web.servlet.mvc.method.annotation.RequestMappingHandlerMapping] - Mapped "{[/netloan/userManage/url-mapping],methods=[],params=[],headers=[],consumes=[],produces=[],custom=[]}" onto public java.lang.Object com.lly.netloan.system.user.controller.UserController.getURLMapping() throws java.lang.Exception 2013-12-30 11:00:34,918 INFO [org.hibernate.validator.util.Version] - Hibernate Validator 4.2.0.Final 2013-12-30 11:00:35,622 INFO [org.hibernate.annotations.common.Version] - HCANN000001: Hibernate Commons Annotations {4.0.1.Final} 2013-12-30 11:00:35,633 INFO [org.hibernate.Version] - HHH000412: Hibernate Core {4.1.0.Final} 2013-12-30 11:00:35,637 INFO [org.hibernate.cfg.Environment] - HHH000205: Loaded properties from resource hibernate.properties: {hibernate.default_batch_fetch_size=16, net.sf.ehcache.configurationResourceName=ehcache.xml, hibernate.cache.use_query_cache=true, hibernate.dialect=org.hibernate.dialect.MySQLDialect, hibernate.max_fetch_depth=2, hibernate.cache.use_second_level_cache=true, hibernate.format_sql=true, hibernate.cache.use_structured_entries=true, hibernate.cache.region.factory_class=org.hibernate.cache.EhCacheRegionFactory, hibernate.query.substitutions=true 1, false 0, hibernate.generate_statistics=true, hibernate.hbm2ddl.auto=none, hibernate.bytecode.use_reflection_optimizer=true, hibernate.show_sql=true} 2013-12-30 11:00:35,639 INFO [org.hibernate.cfg.Environment] - HHH000406: Using bytecode reflection optimizer 2013-12-30 11:00:35,639 INFO [org.hibernate.cfg.Environment] - HHH000021: Bytecode provider name : javassist 2013-12-30 11:00:37,684 INFO [org.hibernate.dialect.Dialect] - HHH000400: Using dialect: org.hibernate.dialect.MySQLDialect 2013-12-30 11:00:37,704 INFO [org.hibernate.engine.jdbc.internal.LobCreatorBuilder] - HHH000423: Disabling contextual LOB creation as JDBC driver reported JDBC version [3] less than 4 2013-12-30 11:00:37,722 INFO [org.hibernate.engine.transaction.internal.TransactionFactoryInitiator] - HHH000399: Using default transaction strategy (direct JDBC transactions) 2013-12-30 11:00:37,733 INFO [org.hibernate.hql.internal.ast.ASTQueryTranslatorFactory] - HHH000397: Using ASTQueryTranslatorFactory 2013-12-30 11:00:38,986 INFO [org.hibernate.cache.spi.UpdateTimestampsCache] - HHH000250: Starting update timestamps cache at region: org.hibernate.cache.spi.UpdateTimestampsCache 2013-12-30 11:00:38,993 WARN [org.hibernate.cache.ehcache.AbstractEhcacheRegionFactory] - HHH020003: Could not find a specific ehcache configuration for cache named [org.hibernate.cache.spi.UpdateTimestampsCache]; using defaults. 2013-12-30 11:00:39,049 INFO [org.hibernate.cache.internal.StandardQueryCache] - HHH000248: Starting query cache at region: org.hibernate.cache.internal.StandardQueryCache 2013-12-30 11:00:39,051 WARN [org.hibernate.cache.ehcache.AbstractEhcacheRegionFactory] - HHH020003: Could not find a specific ehcache configuration for cache named [org.hibernate.cache.internal.StandardQueryCache]; using defaults. 2013-12-30 11:00:39,271 INFO [org.springframework.orm.hibernate4.HibernateTransactionManager] - Using DataSource [org.logicalcobwebs.proxool.ProxoolDataSource@1f517997] of Hibernate SessionFactory for HibernateTransactionManager 2013-12-30 11:00:39,496 INFO [org.springframework.web.servlet.handler.SimpleUrlHandlerMapping] - Mapped URL path [/**] onto handler 'org.springframework.web.servlet.resource.ResourceHttpRequestHandler#0' 2013-12-30 11:00:39,527 INFO [org.springframework.web.servlet.handler.SimpleUrlHandlerMapping] - Root mapping to handler of type [class org.springframework.web.servlet.mvc.ParameterizableViewController] 2013-12-30 11:00:39,556 INFO [org.springframework.web.servlet.mvc.annotation.DefaultAnnotationHandlerMapping] - Mapped URL path [/netloan/dictManage/findByDictType/{dictType}] onto handler 'dictController' 2013-12-30 11:00:39,556 INFO [org.springframework.web.servlet.mvc.annotation.DefaultAnnotationHandlerMapping] - Mapped URL path [/netloan/dictManage/findByDictType/{dictType}.*] onto handler 'dictController' 2013-12-30 11:00:39,556 INFO [org.springframework.web.servlet.mvc.annotation.DefaultAnnotationHandlerMapping] - Mapped URL path [/netloan/dictManage/findByDictType/{dictType}/] onto handler 'dictController' 2013-12-30 11:00:39,556 INFO [org.springframework.web.servlet.mvc.annotation.DefaultAnnotationHandlerMapping] - Mapped URL path [/netloan/dictManage/findByParentDictId/{parentDictId}] onto handler 'dictController' 2013-12-30 11:00:39,556 INFO [org.springframework.web.servlet.mvc.annotation.DefaultAnnotationHandlerMapping] - Mapped URL path [/netloan/dictManage/findByParentDictId/{parentDictId}.*] onto handler 'dictController' 2013-12-30 11:00:39,556 INFO [org.springframework.web.servlet.mvc.annotation.DefaultAnnotationHandlerMapping] - Mapped URL path [/netloan/dictManage/findByParentDictId/{parentDictId}/] onto handler 'dictController' 2013-12-30 11:00:39,557 INFO [org.springframework.web.servlet.mvc.annotation.DefaultAnnotationHandlerMapping] - Mapped URL path [/netloan/fileUpload] onto handler 'fileUploadController' 2013-12-30 11:00:39,557 INFO [org.springframework.web.servlet.mvc.annotation.DefaultAnnotationHandlerMapping] - Mapped URL path [/netloan/fileUpload.*] onto handler 'fileUploadController' 2013-12-30 11:00:39,557 INFO [org.springframework.web.servlet.mvc.annotation.DefaultAnnotationHandlerMapping] - Mapped URL path [/netloan/fileUpload/] onto handler 'fileUploadController' 2013-12-30 11:00:39,558 INFO [org.springframework.web.servlet.mvc.annotation.DefaultAnnotationHandlerMapping] - Mapped URL path [/spring/url-mapping] onto handler 'springController' 2013-12-30 11:00:39,558 INFO [org.springframework.web.servlet.mvc.annotation.DefaultAnnotationHandlerMapping] - Mapped URL path [/spring/url-mapping.*] onto handler 'springController' 2013-12-30 11:00:39,558 INFO [org.springframework.web.servlet.mvc.annotation.DefaultAnnotationHandlerMapping] - Mapped URL path [/spring/url-mapping/] onto handler 'springController' 2013-12-30 11:00:39,559 INFO [org.springframework.web.servlet.mvc.annotation.DefaultAnnotationHandlerMapping] - Mapped URL path [/netloan/targetManage] onto handler 'targetController' 2013-12-30 11:00:39,559 INFO [org.springframework.web.servlet.mvc.annotation.DefaultAnnotationHandlerMapping] - Mapped URL path [/netloan/targetManage.*] onto handler 'targetController' 2013-12-30 11:00:39,559 INFO [org.springframework.web.servlet.mvc.annotation.DefaultAnnotationHandlerMapping] - Mapped URL path [/netloan/targetManage/] onto handler 'targetController' 2013-12-30 11:00:39,560 INFO [org.springframework.web.servlet.mvc.annotation.DefaultAnnotationHandlerMapping] - Mapped URL path [/netloan/userManage/saveUserInfo] onto handler 'userController' 2013-12-30 11:00:39,560 INFO [org.springframework.web.servlet.mvc.annotation.DefaultAnnotationHandlerMapping] - Mapped URL path [/netloan/userManage/saveUserInfo.*] onto handler 'userController' 2013-12-30 11:00:39,560 INFO [org.springframework.web.servlet.mvc.annotation.DefaultAnnotationHandlerMapping] - Mapped URL path [/netloan/userManage/saveUserInfo/] onto handler 'userController' 2013-12-30 11:00:39,560 INFO [org.springframework.web.servlet.mvc.annotation.DefaultAnnotationHandlerMapping] - Mapped URL path [/netloan/userManage/url-mapping] onto handler 'userController' 2013-12-30 11:00:39,560 INFO [org.springframework.web.servlet.mvc.annotation.DefaultAnnotationHandlerMapping] - Mapped URL path [/netloan/userManage/url-mapping.*] onto handler 'userController' 2013-12-30 11:00:39,560 INFO [org.springframework.web.servlet.mvc.annotation.DefaultAnnotationHandlerMapping] - Mapped URL path [/netloan/userManage/url-mapping/] onto handler 'userController' 2013-12-30 11:00:39,899 INFO [org.springframework.mock.web.MockServletContext] - Initializing Spring FrameworkServlet '' 2013-12-30 11:00:39,899 INFO [org.springframework.test.web.servlet.TestDispatcherServlet] - FrameworkServlet '': initialization started 2013-12-30 11:00:39,937 INFO [org.springframework.test.web.servlet.TestDispatcherServlet] - FrameworkServlet '': initialization completed in 38 ms ===================: 2013-12-30 11:00:40,069 INFO [org.logicalcobwebs.proxool.proxoolDataSource] - Shutting down 'proxoolDataSource' pool immediately [Shutdown Hook] 2013-12-30 11:00:40,071 INFO [org.springframework.web.context.support.GenericWebApplicationContext] - Closing org.springframework.web.context.support.GenericWebApplicationContext@b6e39f: startup date [Mon Dec 30 11:00:33 CST 2013]; root of context hierarchy 2013-12-30 11:00:40,073 INFO [org.springframework.beans.factory.support.DefaultListableBeanFactory] - Destroying singletons in org.springframework.beans.factory.support.DefaultListableBeanFactory@54083e1e: defining beans [mvcContentNegotiationManager,org.springframework.web.servlet.mvc.method.annotation.RequestMappingHandlerMapping#0,org.springframework.web.servlet.mvc.method.annotation.RequestMappingHandlerAdapter#0,org.springframework.web.servlet.handler.MappedInterceptor#0,org.springframework.web.servlet.mvc.method.annotation.ExceptionHandlerExceptionResolver#0,org.springframework.web.servlet.mvc.annotation.ResponseStatusExceptionResolver#0,org.springframework.web.servlet.mvc.support.DefaultHandlerExceptionResolver#0,org.springframework.web.servlet.handler.BeanNameUrlHandlerMapping,org.springframework.web.servlet.mvc.HttpRequestHandlerAdapter,org.springframework.web.servlet.mvc.SimpleControllerHandlerAdapter,validator,conversion-service,springApplicationContextHolder,globalExceptionResolver,CommonHibernateDao,generalHibernateDao,genericHibernateDao,hibernateMatchRuleContext,dictController,dictInfoDaoImpl,fileUploadController,springController,targetController,targetUserRelaDaoImpl,userController,userDaoImpl,org.springframework.context.annotation.internalConfigurationAnnotationProcessor,org.springframework.context.annotation.internalAutowiredAnnotationProcessor,org.springframework.context.annotation.internalRequiredAnnotationProcessor,org.springframework.context.annotation.internalCommonAnnotationProcessor,org.springframework.context.annotation.internalPersistenceAnnotationProcessor,org.springframework.web.servlet.resource.ResourceHttpRequestHandler#0,org.springframework.web.servlet.handler.SimpleUrlHandlerMapping#0,org.springframework.web.servlet.handler.MappedInterceptor#1,org.springframework.web.servlet.handler.MappedInterceptor#2,org.springframework.web.servlet.config.viewControllerHandlerMapping,org.springframework.web.servlet.mvc.annotation.DefaultAnnotationHandlerMapping#0,handlerAdapter,org.springframework.web.servlet.view.ContentNegotiatingViewResolver#0,multipartResolver,exceptionResolver,CommonService,dictServiceImpl,targetServiceImpl,userServiceImpl,propertyConfigurer,messageSource,dataSource,sessionFactory,org.springframework.aop.config.internalAutoProxyCreator,org.springframework.transaction.annotation.AnnotationTransactionAttributeSource#0,org.springframework.transaction.interceptor.TransactionInterceptor#0,org.springframework.transaction.config.internalTransactionAdvisor,txManager,txAdvice,txPointcut,org.springframework.aop.support.DefaultBeanFactoryPointcutAdvisor#0,org.springframework.context.annotation.ConfigurationClassPostProcessor.importAwareProcessor]; root of factory hierarchy 2013-12-30 11:00:40,081 INFO [org.logicalcobwebs.proxool.PrototyperController] - Stopping Prototyper thread 2013-12-30 11:00:40,081 INFO [org.logicalcobwebs.proxool.HouseKeeperController] - Stopping HouseKeeper thread
RESTful标签写得有问题,导致参数一直注入不了,后来查了Spring 官方文档,没发现问题 http://projects.spring.io/spring-framework/
后来无意之中发现自己请求的数据 JSON格式有问题,错误就在“
private String json ="{\"userName\":luhiahua,\"passWord\":1235,\"certCode\":\"320681198903167015\"}";”
的 String 串没加引号,
哎一失足成千古恨,查了两天终于解决了,把以上日志贴上来,希望码农们不要犯我的低级错误
private String json ="{\"userName\":\"luhiahua\",\"passWord\":1235,\"certCode\":\"320681198903167015\"}";