我真的需要Grails中的某些帮助。 我是Grails的新手,也是处理日常工作,测量等的体育馆的后端。 我遇到的问题是在解析JSON时为新客户端或现有客户端创建例程。 该例程由一系列日期组成,总共最多4天。 长话短说,当JSON请求只有1天时,我得到了IllegalArgumentException,并且我没有关于原因的想法。 请帮我! 这是一个样本json:
{"objetivo":"test martes","fechaRealizacion":"2015-01-20T00:00:00Z","carne":115500153,"dias":[{"numDia":"1","ejercicios":[{"descripcionTrans":"1.1","idHoja":1},{"descripcionTrans":"1.2","idHoja":2},{"descripcionTrans":"1.3","idHoja":3}]}]}
这里是解析JSON请求的代码:
def guardar = {
println request.JSON
jsonObj = request.JSON
Rutinas r = new Rutinas(jsonObj)
def ide = request.JSON.carne?.toLong() ?:0
r.perfilAsociado = requestService.getPerfil(ide)
r.observaciones = r.perfilAsociado.observaciones
println jsonObj
ArrayList test = jsonObj.dias
ArrayList<Dia> dias = new ArrayList<Dia>()
def testO
for(int i = 0; i < test.size(); i++) {
Dia d = test.get(i)
ArrayList<Ejercicios> ejerAdd = new ArrayList<Ejercicios>()
for(int j = 0; j < d.ejercicios.size(); j++) {
testO = [idHoja: d.ejercicios.get(j).idHoja]
Ejercicios e = Ejercicios.findByIdHoja(testO.idHoja)
ejerAdd.add(e)
}
d.ejercicios = ejerAdd
dias.add(d)
}
r.dias = dias
/**/
try {
insertRutinas(r)
insertDia(r, jsonObj)
}catch(Exception ex) {
response.status = 500
println ex
render "Internal server error"
}
response.status = 201
render r as JSON//requestService.encodeRutinas(r)
}
编辑:
调用ArrayList test = jsonObj.dias
时会抛出错误
这是堆栈跟踪:
| Error 2015-01-21 09:18:04,884 [http-bio-8080-exec-4] ERROR errors.GrailsExceptionResolver - IllegalArgumentException occurred when processing request: [POST] /backend-gimnasio/rutinas/guardar/
argument type mismatch. Stacktrace follows:
Message: argument type mismatch
Line | Method
->> -2 | invoke0 in sun.reflect.NativeMethodAccessorImpl
- - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
| 57 | invoke in ''
| 43 | invoke . . . . . . . . . . . . in sun.reflect.DelegatingMethodAccessorImpl
| 606 | invoke in java.lang.reflect.Method
| 1270 | jlrMethodInvoke . . . . . . . . in org.springsource.loaded.ri.ReflectiveInterceptor
| 90 | invoke in org.codehaus.groovy.reflection.CachedMethod
| 324 | doMethodInvoke . . . . . . . . in groovy.lang.MetaMethod
| 2662 | setProperty in groovy.lang.MetaClassImpl
| 1170 | setProperty . . . . . . . . . . in groovy.lang.ExpandoMetaClass
| 3702 | setProperty in groovy.lang.MetaClassImpl
| 1741 | setProperties . . . . . . . . . in ''
| 1718 | invokeConstructor in ''
| 1526 | invokeConstructor . . . . . . . in ''
| 675 | invokeConstructor in groovy.lang.ExpandoMetaClass
| 932 | invokeConstructorOf . . . . . . in org.codehaus.groovy.runtime.InvokerHelper
| 362 | continueCastOnSAM in org.codehaus.groovy.runtime.typehandling.DefaultTypeTransformation
| 306 | continueCastOnNumber . . . . . in ''
| 262 | continueCastOnCollection in ''
| 214 | castToType . . . . . . . . . . in ''
| 599 | castToType in org.codehaus.groovy.runtime.ScriptBytecodeAdapter
| 386 | doCall . . . . . . . . . . . . in gimnasio.backend.RutinasController$_closure2
| -1 | doCall in ''
| -2 | invoke0 . . . . . . . . . . . . in sun.reflect.NativeMethodAccessorImpl
| 57 | invoke in ''
| 43 | invoke . . . . . . . . . . . . in sun.reflect.DelegatingMethodAccessorImpl
| 606 | invoke in java.lang.reflect.Method
| 1270 | jlrMethodInvoke . . . . . . . . in org.springsource.loaded.ri.ReflectiveInterceptor
| 90 | invoke in org.codehaus.groovy.reflection.CachedMethod
| 324 | doMethodInvoke . . . . . . . . in groovy.lang.MetaMethod
| 1207 | invokeMethod in groovy.lang.MetaClassImpl
| 1110 | invokeMethod . . . . . . . . . in groovy.lang.ExpandoMetaClass
| 1016 | invokeMethod in groovy.lang.MetaClassImpl
| 423 | call . . . . . . . . . . . . . in groovy.lang.Closure
| -1 | call in gimnasio.backend.RutinasController$_closure2
| 417 | call . . . . . . . . . . . . . in groovy.lang.Closure
| -1 | call in gimnasio.backend.RutinasController$_closure2
| -2 | invoke0 . . . . . . . . . . . . in sun.reflect.NativeMethodAccessorImpl
| 57 | invoke in ''
| 43 | invoke . . . . . . . . . . . . in sun.reflect.DelegatingMethodAccessorImpl
| 606 | invoke in java.lang.reflect.Method
| 1270 | jlrMethodInvoke . . . . . . . . in org.springsource.loaded.ri.ReflectiveInterceptor
| 154 | invoke in org.codehaus.groovy.grails.web.servlet.mvc.MixedGrailsControllerHelper
| 375 | handleAction . . . . . . . . . in org.codehaus.groovy.grails.web.servlet.mvc.AbstractGrailsControllerHelper
| 252 | executeAction in ''
| 205 | handleURI . . . . . . . . . . . in ''
| 126 | handleURI in ''
| 72 | handleRequest . . . . . . . . . in org.codehaus.groovy.grails.web.servlet.mvc.SimpleGrailsController
| 50 | handle in org.springframework.web.servlet.mvc.SimpleControllerHandlerAdapter
| 347 | doDispatch . . . . . . . . . . in org.codehaus.groovy.grails.web.servlet.GrailsDispatcherServlet
| 870 | doService in org.springframework.web.servlet.DispatcherServlet
| 961 | processRequest . . . . . . . . in org.springframework.web.servlet.FrameworkServlet
| 863 | doPost in ''
| 646 | service . . . . . . . . . . . . in javax.servlet.http.HttpServlet
| 837 | service in org.springframework.web.servlet.FrameworkServlet
| 727 | service . . . . . . . . . . . . in javax.servlet.http.HttpServlet
| 303 | internalDoFilter in org.apache.catalina.core.ApplicationFilterChain
| 208 | doFilter . . . . . . . . . . . in ''
| 52 | doFilter in org.apache.tomcat.websocket.server.WsFilter
| 241 | internalDoFilter . . . . . . . in org.apache.catalina.core.ApplicationFilterChain
| 208 | doFilter in ''
| 198 | doFilter . . . . . . . . . . . in grails.plugin.cache.web.filter.PageFragmentCachingFilter
| 63 | doFilter in grails.plugin.cache.web.filter.AbstractFilter
| 344 | invokeDelegate . . . . . . . . in org.springframework.web.filter.DelegatingFilterProxy
| 261 | doFilter in ''
| 241 | internalDoFilter . . . . . . . in org.apache.catalina.core.ApplicationFilterChain
| 208 | doFilter in ''
| 101 | doFilter . . . . . . . . . . . in org.springframework.web.filter.OncePerRequestFilter
| 241 | internalDoFilter in org.apache.catalina.core.ApplicationFilterChain
| 208 | doFilter . . . . . . . . . . . in ''
| 101 | doFilter in org.springframework.web.filter.OncePerRequestFilter
| 241 | internalDoFilter . . . . . . . in org.apache.catalina.core.ApplicationFilterChain
| 208 | doFilter in ''
| 101 | doFilter . . . . . . . . . . . in org.springframework.web.filter.OncePerRequestFilter
| 241 | internalDoFilter in org.apache.catalina.core.ApplicationFilterChain
| 208 | doFilter . . . . . . . . . . . in ''
| 748 | invoke in org.apache.catalina.core.ApplicationDispatcher
| 486 | processRequest . . . . . . . . in ''
| 411 | doForward in ''
| 338 | forward . . . . . . . . . . . . in ''
| 178 | forwardRequestForUrlMappingInfo in org.codehaus.groovy.grails.web.mapping.UrlMappingUtils
| 144 | forwardRequestForUrlMappingInfo in ''
| 135 | forwardRequestForUrlMappingInfo in ''
| 216 | doFilterInternal . . . . . . . in org.codehaus.groovy.grails.web.mapping.filter.UrlMappingsFilter
| 107 | doFilter in org.springframework.web.filter.OncePerRequestFilter
| 241 | internalDoFilter . . . . . . . in org.apache.catalina.core.ApplicationFilterChain
| 208 | doFilter in ''
| 330 | doFilter . . . . . . . . . . . in org.springframework.security.web.FilterChainProxy$VirtualFilterChain
| 118 | invoke in org.springframework.security.web.access.intercept.FilterSecurityInterceptor
| 84 | doFilter . . . . . . . . . . . in ''
| 342 | doFilter in org.springframework.security.web.FilterChainProxy$VirtualFilterChain
| 113 | doFilter . . . . . . . . . . . in org.springframework.security.web.access.ExceptionTranslationFilter
| 342 | doFilter in org.springframework.security.web.FilterChainProxy$VirtualFilterChain
| 113 | doFilter . . . . . . . . . . . in org.springframework.security.web.access.ExceptionTranslationFilter
| 342 | doFilter in org.springframework.security.web.FilterChainProxy$VirtualFilterChain
| -1 | call . . . . . . . . . . . . . in javax.servlet.FilterChain$doFilter
| 108 | processFilterChain in com.odobo.grails.plugin.springsecurity.rest.RestTokenValidationFilter
| -1 | this$3$processFilterChain . . . in ''
| 77 | doFilter in ''
| 342 | doFilter . . . . . . . . . . . in org.springframework.security.web.FilterChainProxy$VirtualFilterChain
| 53 | doFilter in grails.plugin.springsecurity.web.filter.GrailsAnonymousAuthenticationFilter
| 342 | doFilter . . . . . . . . . . . in org.springframework.security.web.FilterChainProxy$VirtualFilterChain
| 146 | doFilter in org.springframework.security.web.authentication.rememberme.RememberMeAuthenticationFilter
| 342 | doFilter . . . . . . . . . . . in org.springframework.security.web.FilterChainProxy$VirtualFilterChain
| 154 | doFilter in org.springframework.security.web.servletapi.SecurityContextHolderAwareRequestFilter
| 342 | doFilter . . . . . . . . . . . in org.springframework.security.web.FilterChainProxy$VirtualFilterChain
| -1 | call in javax.servlet.FilterChain$doFilter
| 122 | doFilter . . . . . . . . . . . in com.odobo.grails.plugin.springsecurity.rest.RestAuthenticationFilter
| 342 | doFilter in org.springframework.security.web.FilterChainProxy$VirtualFilterChain
| 199 | doFilter . . . . . . . . . . . in org.springframework.security.web.authentication.AbstractAuthenticationProcessingFilter
| 49 | doFilter in grails.plugin.springsecurity.web.authentication.RequestHolderAuthenticationFilter
| 342 | doFilter . . . . . . . . . . . in org.springframework.security.web.FilterChainProxy$VirtualFilterChain
| 82 | doFilter in grails.plugin.springsecurity.web.authentication.logout.MutableLogoutFilter
| 342 | doFilter . . . . . . . . . . . in org.springframework.security.web.FilterChainProxy$VirtualFilterChain
| -1 | call in javax.servlet.FilterChain$doFilter
| 63 | doFilter . . . . . . . . . . . in com.odobo.grails.plugin.springsecurity.rest.RestLogoutFilter
| 342 | doFilter in org.springframework.security.web.FilterChainProxy$VirtualFilterChain
| 87 | doFilter . . . . . . . . . . . in org.springframework.security.web.context.SecurityContextPersistenceFilter
| 342 | doFilter in org.springframework.security.web.FilterChainProxy$VirtualFilterChain
| 192 | doFilterInternal . . . . . . . in org.springframework.security.web.FilterChainProxy
| 160 | doFilter in ''
| 344 | invokeDelegate . . . . . . . . in org.springframework.web.filter.DelegatingFilterProxy
| 261 | doFilter in ''
| 241 | internalDoFilter . . . . . . . in org.apache.catalina.core.ApplicationFilterChain
| 208 | doFilter in ''
| 69 | doFilterInternal . . . . . . . in org.codehaus.groovy.grails.web.servlet.mvc.GrailsWebRequestFilter
| 107 | doFilter in org.springframework.web.filter.OncePerRequestFilter
| 241 | internalDoFilter . . . . . . . in org.apache.catalina.core.ApplicationFilterChain
| 208 | doFilter in ''
| 67 | doFilterInternal . . . . . . . in org.codehaus.groovy.grails.web.filters.HiddenHttpMethodFilter
| 107 | doFilter in org.springframework.web.filter.OncePerRequestFilter
| 241 | internalDoFilter . . . . . . . in org.apache.catalina.core.ApplicationFilterChain
| 208 | doFilter in ''
| 88 | doFilterInternal . . . . . . . in org.springframework.web.filter.CharacterEncodingFilter
| 107 | doFilter in org.springframework.web.filter.OncePerRequestFilter
| 344 | invokeDelegate . . . . . . . . in org.springframework.web.filter.DelegatingFilterProxy
| 261 | doFilter in ''
| 241 | internalDoFilter . . . . . . . in org.apache.catalina.core.ApplicationFilterChain
| 208 | doFilter in ''
| 220 | invoke . . . . . . . . . . . . in org.apache.catalina.core.StandardWrapperValve
| 122 | invoke in org.apache.catalina.core.StandardContextValve
| 171 | invoke . . . . . . . . . . . . in org.apache.catalina.core.StandardHostValve
| 103 | invoke in org.apache.catalina.valves.ErrorReportValve
| 116 | invoke . . . . . . . . . . . . in org.apache.catalina.core.StandardEngineValve
| 408 | service in org.apache.catalina.connector.CoyoteAdapter
| 1070 | process . . . . . . . . . . . . in org.apache.coyote.http11.AbstractHttp11Processor
| 611 | process in org.apache.coyote.AbstractProtocol$AbstractConnectionHandler
| 316 | run . . . . . . . . . . . . . . in org.apache.tomcat.util.net.JIoEndpoint$SocketProcessor
| 1145 | runWorker in java.util.concurrent.ThreadPoolExecutor
| 615 | run . . . . . . . . . . . . . . in java.util.concurrent.ThreadPoolExecutor$Worker
| 61 | run in org.apache.tomcat.util.threads.TaskThread$WrappingRunnable
^ 745 | run . . . . . . . . . . . . . . in java.lang.Thread