UrlMappings parseRequest:true在无需解析时导致JSON异常

时间:2011-06-07 14:50:19

标签: json grails groovy

更新:刚刚找到this bug看起来像我的问题。

我正在使用一个适用于纯JSON的客户端框架。我有一个UrlMapping:

"/books/$id?"(controller:'book', parseRequest:true) {
    action = [GET:'show',PUT:'update', POST:'save']
}

问题在于'show'操作,如果没有id传递给请求,我只返回所有书籍的JSON列表:

def show = {
    if (params.id) {
      def bookInstance = Book.get(params.id)
      render bookInstance as JSON
    }else{
      render Book.list() as JSON
    }
}

但是,由于没有要解析的JSON,我得到以下异常:

2011-06-07 09:45:20,373 [http-8080-1] ERROR converters.JSONParsingParameterCreationListener  - Error parsing incoming JSON request: Error parsing JSON
org.codehaus.groovy.grails.web.converters.exceptions.ConverterException: Error parsing JSON
    at grails.converters.JSON.parse(JSON.java:289)
    at grails.converters.JSON.parse(JSON.java:311)
    at grails.converters.JSON.parse(JSON.java:334)
    at grails.converters.JSON$parse.call(Unknown Source)
    at org.codehaus.groovy.runtime.callsite.CallSiteArray.defaultCall(CallSiteArray.java:40)
    at org.codehaus.groovy.runtime.callsite.AbstractCallSite.call(AbstractCallSite.java:116)
    at org.codehaus.groovy.runtime.callsite.AbstractCallSite.call(AbstractCallSite.java:124)
    at org.codehaus.groovy.grails.web.converters.JSONParsingParameterCreationListener.paramsCreated(JSONParsingParameterCreationListener.groovy:42)
    at org.codehaus.groovy.grails.web.servlet.mvc.GrailsWebRequest.informParameterCreationListeners(GrailsWebRequest.java:171)
    at org.codehaus.groovy.grails.web.mapping.filter.UrlMappingsFilter.doFilterInternal(UrlMappingsFilter.java:181)
    at org.springframework.web.filter.OncePerRequestFilter.doFilter(OncePerRequestFilter.java:76)
    at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:235)
    at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:206)
    at org.codehaus.groovy.grails.web.sitemesh.GrailsPageFilter.obtainContent(GrailsPageFilter.java:245)
    at org.codehaus.groovy.grails.web.sitemesh.GrailsPageFilter.doFilter(GrailsPageFilter.java:134)
    at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:235)
    at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:206)
    at org.codehaus.groovy.grails.web.servlet.filter.GrailsReloadServletFilter.doFilterInternal(GrailsReloadServletFilter.java:104)
    at org.springframework.web.filter.OncePerRequestFilter.doFilter(OncePerRequestFilter.java:76)
    at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:235)
    at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:206)
    at org.codehaus.groovy.grails.web.servlet.mvc.GrailsWebRequestFilter.doFilterInternal(GrailsWebRequestFilter.java:69)
    at org.springframework.web.filter.OncePerRequestFilter.doFilter(OncePerRequestFilter.java:76)
    at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:235)
    at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:206)
    at org.codehaus.groovy.grails.web.filters.HiddenHttpMethodFilter.doFilterInternal(HiddenHttpMethodFilter.java:69)
    at org.springframework.web.filter.OncePerRequestFilter.doFilter(OncePerRequestFilter.java:76)
    at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:235)
    at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:206)
    at org.springframework.web.filter.CharacterEncodingFilter.doFilterInternal(CharacterEncodingFilter.java:88)
    at org.springframework.web.filter.OncePerRequestFilter.doFilter(OncePerRequestFilter.java:76)
    at org.springframework.web.filter.DelegatingFilterProxy.invokeDelegate(DelegatingFilterProxy.java:237)
    at org.springframework.web.filter.DelegatingFilterProxy.doFilter(DelegatingFilterProxy.java:167)
    at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:235)
    at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:206)
    at org.apache.catalina.core.StandardWrapperValve.invoke(StandardWrapperValve.java:233)
    at org.apache.catalina.core.StandardContextValve.invoke(StandardContextValve.java:191)
    at org.apache.catalina.core.StandardHostValve.invoke(StandardHostValve.java:128)
    at org.apache.catalina.valves.ErrorReportValve.invoke(ErrorReportValve.java:102)
    at org.apache.catalina.core.StandardEngineValve.invoke(StandardEngineValve.java:109)
    at org.apache.catalina.connector.CoyoteAdapter.service(CoyoteAdapter.java:293)
    at org.apache.coyote.http11.Http11Processor.process(Http11Processor.java:849)
    at org.apache.coyote.http11.Http11Protocol$Http11ConnectionHandler.process(Http11Protocol.java:583)
    at org.apache.tomcat.util.net.JIoEndpoint$Worker.run(JIoEndpoint.java:454)
    at java.lang.Thread.run(Thread.java:680)
Caused by: org.codehaus.groovy.grails.web.json.JSONException: Missing value. at character 0 of 
    at org.codehaus.groovy.grails.web.json.JSONTokener.syntaxError(JSONTokener.java:473)
    at org.codehaus.groovy.grails.web.json.JSONTokener.nextValue(JSONTokener.java:358)
    at grails.converters.JSON.parse(JSON.java:280)
    ... 44 more

这是一个错误还是有人告诉Grails如果不存在JSON,就不要尝试解析任何东西?

1 个答案:

答案 0 :(得分:1)

  

但是,由于没有要解析的JSON,我得到以下异常:

我不确定为什么没有要解析的JSON。如果Book.list()在没有书籍时返回空列表,则应将其转换为空(JavaScript)数组。您可以通过在Grails控制台中运行以下代码来验证这一点

import grails.converters.*

String json = [] as JSON
assert json == '[]'

如果Book.list()返回null,则可以通过更改

轻松地将其替换为空列表
render Book.list() as JSON

// If you like micro-optimizations, replace [] with Collections.emptyList()
def allBooks = Book.list() ?: []
render allBooks as JSON