Java Grails:在使用Jackson API时获取异常
使用Jackson api时出现以下异常。请参阅所附图片 下面的一行我已经用来解析所有操作的输出jsonJava Grails:在使用Jackson API时获取异常,java,json,grails,jackson,grails-2.5,Java,Json,Grails,Jackson,Grails 2.5,使用Jackson api时出现以下异常。请参阅所附图片 下面的一行我已经用来解析所有操作的输出json render contentType: 'application/json', text:BlogSwiftJSONUtil.parseToJSON(listAllResources(params)) 在BuildConfig.groovy中添加的jackson库如下: dependencies { // specify dependencies here under e
render contentType: 'application/json', text:BlogSwiftJSONUtil.parseToJSON(listAllResources(params))
在BuildConfig.groovy中添加的jackson库如下:
dependencies {
// specify dependencies here under either 'build', 'compile', 'runtime', 'test' or 'provided' scopes e.g.
// runtime 'mysql:mysql-connector-java:5.1.29'
// runtime 'org.postgresql:postgresql:9.3-1101-jdbc41'
test "org.grails:grails-datastore-test-support:1.0.2-grails-2.4"
runtime 'com.fasterxml.jackson.core:jackson-core:2.0.4'
runtime 'com.fasterxml.jackson.core:jackson-databind:2.0.4'
runtime 'com.fasterxml.jackson.core:jackson-annotations:2.0.4'
}
任何人为什么我会得到这个例外
以下是我的一些发现:
谢谢 为了让jackson整理您的响应,您需要一个bean,它具有公共getter/setter的私有字段,或者定义具有公共可见性的字段。从您粘贴的屏幕截图来看,您的api调用似乎以某种方式失败,从而重定向到spring以处理jackson无法序列化的异常 您需要通过添加以下内容来克服此问题:
objectMapper.setVisibility(JsonMethod.FIELD, Visibility.ANY);
当您将其用于RESTAPI时,很可能会序列化域、枚举和一些自定义只读POJO。此问题是因为未能序列化注入域的验证错误。您可以自定义域以选择用于序列化和反序列化的字段 看 为了更加灵活,请手动添加您自己的serialiser,并给出您自己的定义,如下所示:
dependencies {
// specify dependencies here under either 'build', 'compile', 'runtime', 'test' or 'provided' scopes e.g.
// runtime 'mysql:mysql-connector-java:5.1.29'
// runtime 'org.postgresql:postgresql:9.3-1101-jdbc41'
test "org.grails:grails-datastore-test-support:1.0.2-grails-2.4"
runtime 'com.fasterxml.jackson.core:jackson-core:2.0.4'
runtime 'com.fasterxml.jackson.core:jackson-databind:2.0.4'
runtime 'com.fasterxml.jackson.core:jackson-annotations:2.0.4'
}
下面是添加自定义序列化程序的方法
import com.blog.swift.marshaller.JacksonSerializer
import com.fasterxml.jackson.databind.Module
import com.fasterxml.jackson.databind.ObjectMapper
import com.fasterxml.jackson.databind.module.SimpleModule
class JSONUtil{
static String parsetoJSON(Object object){
ObjectMapper objectMapper = new ObjectMapper()
Module testModule = new SimpleModule()
testModule.addSerializer(new JacksonSerializer(object.getClass()));
objectMapper.registerModule(testModule)
return objectMapper.writerWithDefaultPrettyPrinter().writeValueAsString(object)
}
}
下面是一个示例自定义序列化程序
class JacksonSerializer extends StdSerializer{
protected BSJacksonSerializer(Class t) {
super(t)
}
@Override
void serialize(Object o, JsonGenerator jsonGenerator, SerializerProvider serializerProvider) throws IOException, JsonGenerationException {
jsonGenerator.writeStartObject()
jsonGenerator.writeStringField("test","test")
jsonGenerator.writeEndObject()
}
}
StdSerializer是一个抽象类,它提供了基本实现,以帮助利用对自定义序列化逻辑的关注,而不是异常处理和其他任何事情
希望有帮助 没有什么能比得上可视性。我使用的是更快的json 2.0,它没有这样的枚举。然后使用
objectMapper.setVisibilityChecker(mapper.getSerializationConfig().getDefaultVisibilityChecker().withFieldVisibility(JsonAutoDetect.Visibility.ANY)。WithGetServiceability(JsonAutoDetect.Visibility.NONE)。WithTestervisibility(jsonautodect.Visibility.NONE)。带有创建者可见性(jsonautodect.Visibility.NONE))
它对我有效。你能把它添加到回答中而不是注释中吗。@SMA我相信如果你想跳过一些字段(如grails中的字段),它将显示gorm内部添加的版本字段。在这种情况下,你需要将域属性标记为忽略,而在第e域。@SMA在stackoverflow上没有选择两个正确答案的选项,但softi venture的答案与我更相关。因此,选择它作为答案。虽然我只能看到{“test”:“test”},但它仍然有效.这意味着需要为此做很多工作,但我相信这是进行定制而不是添加域的正确地方。