简体   繁体   中英

Grails : getting exception while using Jackson API

Getting exception below while using Jackson api.See attached image.

在此处输入图片说明

class BlogSwiftJSONUtil {

static String parseToJSON(Object object){
        ObjectMapper objectMapper = new ObjectMapper()
        return objectMapper.writerWithDefaultPrettyPrinter().writeValueAsString(object)
    }
 }

Line below I have used to parse output json on all actions.

render contentType: 'application/json', text:BlogSwiftJSONUtil.parseToJSON(listAllResources(params))

Added jackson library in BuildConfig.groovy is as below:

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'
    }

Anyone why am I getting this exception.

Below are some findings from me:

  1. if I pass map like object.properties rather than object itself it works.
  2. It seems that it's also trying to serialize validation errors as well.

Any helps would be worth.

Please let me know if I could share any other details.

Thanks!

Inorder for jackson to marshall your response, you need a bean which has private field with public getter/setter or define a field with public visibility. From the screenshot you pasted, it seems somehow your api call failed which redirected for spring to handle the exception which jackson couldn't serialize.

You need to overcome this by adding following:

objectMapper.setVisibility(JsonMethod.FIELD, Visibility.ANY);

As you are using it for rest-api and would most probably serialise domains, enums and some custom read-only pojos. This issue is because of failure in serialising the validation errors which are injected to domains. You may customise domains to choose fields for serialisations and deserialisations.

See this .

To be more flexible manually add your own serialiser and give your own definitions as below:

Below is the way to add a custom serialiser

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)
        }
    }

Below is a sample custom serialiser.

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 is an abstract class which gives base implementations to help leverage focus on custom serialisation logic than exception handling and any other thing.

Hope it helps!

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM