Spring MVC Converter 什么时候执行转换

一般从前台传到后台的参数都需要转换,比如转换成Date 类型,JsonObject类型,JsonArray类型等等。但是转换这一步spring是在什么时候做的呢?

java.text.ParseException : A JSONObject must begin with ‘{‘ at character 1 of [email protected]
    at org.json.JSONTokener.syntaxError(JSONTokener.java:374 )
    at org.json.JSONObject.<init>(JSONObject.java:127 )
    at org.json.JSONObject.<init>(JSONObject.java:166 )
    at com.trinitesolutions.core.web.convert.JSONObjectConverter.convert(JSONObjectConverter.java:25)
    at com.trinitesolutions.core.web.convert.JSONObjectConverter.convert(JSONObjectConverter.java:1)
    at org.springframework.core.convert.support.GenericConversionService$ConverterAdapter.convert(GenericConversionService.java:517)
    at org.springframework.core.convert.support.ConversionUtils.invokeConverter(ConversionUtils.java:35)
    at org.springframework.core.convert.support.GenericConversionService.convert(GenericConversionService.java:171)
    at org.springframework.beans.TypeConverterDelegate.convertIfNecessary(TypeConverterDelegate.java:143)
    at org.springframework.beans.TypeConverterDelegate.convertIfNecessary(TypeConverterDelegate.java:92)
    at org.springframework.beans.SimpleTypeConverter.convertIfNecessary(SimpleTypeConverter.java:49)
    at org.springframework.validation.DataBinder.convertIfNecessary(DataBinder.java:553)
    at org.springframework.web.method.annotation.AbstractNamedValueMethodArgumentResolver.resolveArgument(AbstractNamedValueMethodArgumentResolver.java:97)
    at org.springframework.web.method.support.HandlerMethodArgumentResolverComposite.resolveArgument(HandlerMethodArgumentResolverComposite.java:75)
    at org.springframework.web.method.support.InvocableHandlerMethod.getMethodArgumentValues(InvocableHandlerMethod.java:156)
    at org.springframework.web.method.support.InvocableHandlerMethod.invokeForRequest(InvocableHandlerMethod.java:117)
    at org.springframework.web.servlet.mvc.method.annotation.ServletInvocableHandlerMethod.invokeAndHandle(ServletInvocableHandlerMethod.java:96)
    at org.springframework.web.servlet.mvc.method.annotation.RequestMappingHandlerAdapter.invokeHandlerMethod(RequestMappingHandlerAdapter.java:617)
    at org.springframework.web.servlet.mvc.method.annotation.RequestMappingHandlerAdapter.handleInternal(RequestMappingHandlerAdapter.java:578)
    at org.springframework.web.servlet.mvc.method.AbstractHandlerMethodAdapter.handle(AbstractHandlerMethodAdapter.java:80)
    at org.springframework.web.servlet.DispatcherServlet.doDispatch(DispatcherServlet.java:923)
    at org.springframework.web.servlet.DispatcherServlet.doService(DispatcherServlet.java:852)
    at org.springframework.web.servlet.FrameworkServlet.processRequest(FrameworkServlet.java:882)
    at org.springframework.web.servlet.FrameworkServlet.doPost(FrameworkServlet.java:789)
    at javax.servlet.http.HttpServlet.service(HttpServlet.java:647 )
    at javax.servlet.http.HttpServlet.service(HttpServlet.java:728 )
    at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:305)
    at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:210)
    at org.apache.tomcat.websocket.server.WsFilter.doFilter(WsFilter.java:51)
    at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:243)
    at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:210)
    at org.apache.shiro.web.servlet.ProxiedFilterChain.doFilter(ProxiedFilterChain.java:61)
    at org.apache.shiro.web.servlet.AdviceFilter.executeChain(AdviceFilter.java:108)
    at org.apache.shiro.web.servlet.AdviceFilter.doFilterInternal(AdviceFilter.java:137)
    at org.apache.shiro.web.servlet.OncePerRequestFilter.doFilter(OncePerRequestFilter.java:125)
    at org.apache.shiro.web.servlet.ProxiedFilterChain.doFilter(ProxiedFilterChain.java:66)
    at org.apache.shiro.web.servlet.AbstractShiroFilter.executeChain(AbstractShiroFilter.java:449)
    at org.apache.shiro.web.servlet.AbstractShiroFilter$1.call(AbstractShiroFilter.java:365)
    at org.apache.shiro.subject.support.SubjectCallable.doCall(SubjectCallable.java:90)
    at org.apache.shiro.subject.support.SubjectCallable.call(SubjectCallable.java:83)
    at org.apache.shiro.subject.support.DelegatingSubject.execute(DelegatingSubject.java:383)
    at org.apache.shiro.web.servlet.AbstractShiroFilter.doFilterInternal(AbstractShiroFilter.java:362)
    at org.apache.shiro.web.servlet.OncePerRequestFilter.doFilter(OncePerRequestFilter.java:125)
    at org.springframework.web.filter.DelegatingFilterProxy.invokeDelegate(DelegatingFilterProxy.java:346)
    at org.springframework.web.filter.DelegatingFilterProxy.doFilter(DelegatingFilterProxy.java:259)
    at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:243)
    at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:210)
    at org.springframework.orm.jpa.support.OpenEntityManagerInViewFilter.doFilterInternal(OpenEntityManagerInViewFilter.java:147)
    at org.springframework.web.filter.OncePerRequestFilter.doFilter(OncePerRequestFilter.java:76)
    at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:243)
    at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:210)
    at org.springframework.web.filter.CharacterEncodingFilter.doFilterInternal(CharacterEncodingFilter.java:88)
    at org.springframework.web.filter.OncePerRequestFilter.doFilter(OncePerRequestFilter.java:76)
    at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:243)
    at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:210)
    at org.apache.catalina.core.StandardWrapperValve.invoke(StandardWrapperValve.java:222)
    at org.apache.catalina.core.StandardContextValve.invoke(StandardContextValve.java:123)
    at org.apache.catalina.authenticator.AuthenticatorBase.invoke(AuthenticatorBase.java:502)
    at org.apache.catalina.core.StandardHostValve.invoke(StandardHostValve.java:171)
    at org.apache.catalina.valves.ErrorReportValve.invoke(ErrorReportValve.java:100)
    at org.apache.catalina.valves.AccessLogValve.invoke(AccessLogValve.java:953)
    at org.apache.catalina.core.StandardEngineValve.invoke(StandardEngineValve.java:118)
    at org.apache.catalina.connector.CoyoteAdapter.service(CoyoteAdapter.java:408)
    at org.apache.coyote.http11.AbstractHttp11Processor.process(AbstractHttp11Processor.java:1041)
    at org.apache.coyote.AbstractProtocol$AbstractConnectionHandler.process(AbstractProtocol.java:603)
    at org.apache.tomcat.util.net.JIoEndpoint$SocketProcessor.run(JIoEndpoint.java:312)
    at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1110)
    at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:603)
    at java.lang.Thread.run( Thread.java:722)

每当前台发送一个请求,后台都会有相应的controller的方法去执行这个请求。spring通过http请求调用controller的方法是同过反射的方式做的,通过HanderMapping找到相对应的controller及方法,之后要做的就是根据参数类型,参数名称从请求或者ModelAndViewContainer,WebDataBinderFactory找到相对应的参数传到这个方法中。在找到这个参数时,spring 会根据参数的类型来判断要不要把这个参数转换,然后找到相应的注册过的转换器执行转换。

通过转化异常信息也可以很方便的看出来,spring的确是在找方法参数的时候做的转换。这个时间做转换其实是最恰当的时机。因为如果在调用controller方法之前做转换,可能那些在这次请求中用不到的参数也转换了,这样执行效率就降低了。

时间: 2024-11-10 02:11:44

Spring MVC Converter 什么时候执行转换的相关文章

Spring MVC 原理介绍(执行流程)

Spring MVC工作流程图 图一 图二  Spring工作流程描述 1. 用户向服务器发送请求,请求被Spring 前端控制Servelt DispatcherServlet捕获: 2. DispatcherServlet对请求URL进行解析,得到请求资源标识符(URI).然后根据该URI,调用HandlerMapping获得该Handler配置的所有相关的对象(包括Handler对象以及Handler对象对应的拦截器),最后以HandlerExecutionChain对象的形式返回: 3.

spring mvc ajax success不执行 反倒执行error

问题描述:spring mvc中前端使用ajax发送请求,后台使用@ResponseBody直接写数据到响应体中, 返回一个字符串,ajax的success回调函数不执行,反而执行error的回调函数 上代码: ajax请求: $.ajax({                url:"/eCommerceSSM/updateAdmin",                type:"put",                data:JSON.stringify({a

spring mvc 406 (Not Acceptable) json转换错误

spring mvc通过@RequestMapping("/register")和@ResponseBody返回json格式的字符串时出现如下异常: The resource identified by this request is only capable of generating responses with characteristics not acceptable according to the request "accept" headers. 控

[Spring MVC] 表单提交日期转换问题,比如可能导致封装实体类时400错误

三种格式的InitBinder @InitBinder//https://stackoverflow.com/questions/20616319/the-request-sent-by-the-client-was-syntactically-incorrect-spring-mvc-jdbc-te public void initBinder(WebDataBinder binder) { SimpleDateFormat sdf = new SimpleDateFormat("MM/dd/

Spring MVC入门配置

1. Spring MVC 介绍 Spring MVC是Spring框架中的一个子项目,为Web应用程序开发提供MVC模式的支持.Spring MVC的功能与Struts2框架相似,可以把Java Web程序员从繁复的Serlvet API中解放出来:与Struts2不同的是,尤其在Spring 3.1之后,Spring MVC广泛采用了“规范优于配置”(convention-over-configuration)的设计思想,因此配置信息大大减少,灵活性也有很大的提升. 2.Spring MVC

Spring MVC配置

spring mvc的配置 添加spring的支持,导入spring需要的jar包. 配置web.xml. 1 <!--IOC容器配置 --> 2 <context-param> 3 <param-name>contextConfigLocation</param-name> 4 <param-value>/WEB-INF/spring/spring.xml 5 /WEB-INF/spring/spring-*.xml 6 </param-

spring mvc 原理(一)

文章来自http://jinnianshilongnian.iteye.com/blog/1594806 1:使用spring mvc 的好处 √让我们能非常简单的设计出干净的Web层和薄薄的Web层: √进行更简洁的Web层的开发: √天生与Spring框架集成(如IoC容器.AOP等): √提供强大的约定大于配置的契约式编程支持: √能简单的进行Web层的单元测试: √支持灵活的URL到页面控制器的映射: √非常容易与其他视图技术集成,如Velocity.FreeMarker等等,因为模型数

Spring MVC -- 应用测试

测试在软件开发中的重要性不言而喻.测试的主要目的是尽早发现错误,最好是在代码开发的同时.逻辑上认为,错误发现的越早,修复的成本越低.如果在编程中发现错误,可以立即更改代码:如果软件发布后,客户发现错误所需要的修复成本会很大. 在软件开发中有许多不同的测试,其中两个是单元测试和集成测试.通常从单元测试开始测试类中的单个方法,然后进行集成测试,以测试不同的模块是否可以无缝协同工作. 本篇博客中的示例使用JUnit测试框架以及Spring test模块.Spring test模块中的API可用于单元测

对于Spring MVC 拦截器的一些了解

Spring MVC 拦截器的执行顺序 应用场景 假设请求 localhost:8080/ 则要求直接重定向到 localhost:8080/login ; 定义拦截器顺序 permission login 执行顺序 pre 先执行先定义的,而 post 和 after 先执行后定义的. 原文地址:https://www.cnblogs.com/bjio/p/12242920.html