Reputation: 7505
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:
Any helps would be worth.
Please let me know if I could share any other details.
Thanks!
Upvotes: 1
Views: 1128
Reputation: 109
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!
Upvotes: 0
Reputation: 37023
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);
Upvotes: 2