gpt4 book ai didi

json - 当没有要解析的内容时,UrlMappings parseRequest:true导致JSON异常

转载 作者:行者123 更新时间:2023-12-02 15:38:19 24 4
gpt4 key购买 nike

更新:刚刚发现this bug看起来像我的问题。

我正在研究可用于纯JSON的客户端框架。我有一个网址映射为:

"/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

这是Bug还是有办法告诉Grails如果不存在JSON,就不要尝试解析任何内容?

最佳答案

However, since there is no JSON to parse I'm getting the following exception:



我不确定为什么没有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

关于json - 当没有要解析的内容时,UrlMappings parseRequest:true导致JSON异常,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/6267097/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com