Serializing with Jackson (JSON) - Getting "No Serializer Found"

Serializing with Jackson (JSON) - getting No serializer found?

As already described, the default configuration of an ObjectMapper instance is to only access properties that are public fields or have public getters/setters. An alternative to changing the class definition to make a field public or to provide a public getter/setter is to specify (to the underlying VisibilityChecker) a different property visibility rule. Jackson 1.9 provides the ObjectMapper.setVisibility() convenience method for doing so. For the example in the original question, I'd likely configure this as

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

For Jackson >2.0:

myObjectMapper.setVisibility(PropertyAccessor.FIELD, Visibility.ANY);

For more information and details on related configuration options, I recommend reviewing the JavaDocs on ObjectMapper.setVisibility().

Jackson: No serializer found for class ~~~~~ and no properties discovered to create BeanSerializer

Excerpt from here:

By default, Jackson 2 will only work with with fields that are either public, or have a public getter methods – serializing an entity that has all fields private or package private will fail:

Your Person has all fields package protected and without getters thus the error message. Disabling the message naturally does not fix the problem since the class is still empty from Jackson's point of view. That is why you see empty objects and it is better to leave the error on.

You need to either make all fields public, like:

public class Person {
public String name;
// rest of the stuff...
}

or create a public getter for each field (and preferably also set fields private), like:

public class Person {
private String name;

public String getName() {
return this.name;
}
// rest of the stuff...
}

No serializer found Problem returning ResponseEntity including exception

This problem comes because entities are loaded lazily whereas the serialization process is performed before entities get loaded fully. Jackson tries to serialize the nested object but it fails as it finds JavassistLazyInitializer instead of the normal object.

As per your stack trace, You can suppress this error by adding the following configuration to your application.properties file:-

spring.jackson.serialization.FAIL_ON_EMPTY_BEANS=false

This will only hide the error but won't solve the issue. To solve the issue you can use the add-on module for Jackson which handles Hibernate lazy-loading. See more here:- Jackson-datatype-hibernate, how to configure Jackson-datatype-hibernate.

No serializer found when serializing one Object

Maybe it's not a good idea to use your entity (User) to expose the data about user via REST? Can you create UserDTO for your user that will implement Serializable and send this DTO via REST? In this case it should be necessary to convert User object that you've retrieved from the db to UserDTO.

Jackson not serializing property

The issue was with my EntityIdentityDto class which implements the following interface

public interface MetaData {
String getId();
Map<String, Object> getProperties();
void setProperties(Map<String, Object> properties);

@JsonIgnore
EntityType getEntityType();
}

The JsonIgnore at the interface level is the reason why it was not being serialized. After dropping the JsonIgnore all works as expected now.

InvalidDefinitionException No serializer found in java for generic class

Please add the property SerializationFeature.FAIL_ON_EMPTY_BEANS= false to your object mapper like objectMapper.configure(SerializationFeature.FAIL_ON_EMPTY_BEANS, false);

//or you can add this code to your any class of springboot which have @Configuration annotation on it.
@Bean
public ObjectMapper getMapper() {
ObjectMapper objectMapper = new ObjectMapper();
objectMapper.configure(SerializationFeature.FAIL_ON_EMPTY_BEANS, false);
return objectMapper;
}

Send Payload as
{
"custom_data": {},
"email": "emailvalue"
}

//Now I am supposing your api is like this
@PostMapping(value = "/show/recipient") /* /preview-email-recipient*/
public ResponseEntity<?> showRecipient(@Valid @RequestBody Recipient recipient){
log.info(String.format("recipient received {%s} ",recipient.toString()));
return new ResponseEntity<>(recipient,HttpStatus.OK);
}

//curl call will be
endpoint will be post call : http://localhost:8080/show/recipient
with requestbody as : {
"customData": {},
"email": "emailvalue"
}

response : {
"customData": {},
"email": "emailvalue"
}

Reason for failure was ?
0

when you return your object i.e. (Recipient in this case) in response it is getting Serialized to json string using ObjectMapper which is used in spring's MessageConverter(i.e Jackson2HttpMessageConverter) bean. Now the error is caused due to how ObjectMapper serializes your class. Your class has 2 field, 1 of type String and 1 of type JSONObject/GenericType. ObjectMapper when serializing fields, tries to find the corresponding serializer based on the field type. There are some out-of-the-box implementation of serializer for known type like String but for your custom type you either need to provide serializer to ObjectMapper bean or have to disable serialization via configuration of set property SerializationFeature.FAIL_ON_EMPTY_BEANS to false.

Now what does SerializationFeature.FAIL_ON_EMPTY_BEANS do??

public static final SerializationFeature FAIL_ON_EMPTY_BEANS
Feature that determines what happens when no accessors are found for a type (and there are no annotations to indicate it is meant to be serialized). If enabled (default), an exception is thrown to indicate these as non-serializable types; if disabled, they are serialized as empty Objects, i.e. without any properties.
Note that empty types that this feature has only effect on those "empty" beans that do not have any recognized annotations (like @JsonSerialize): ones that do have annotations do not result in an exception being thrown.

Feature is enabled by default.

So
1 way was to disable serialization on empty beans.
2nd way you can annotate CustomData class with @JsonSerialize i.e. you
are provinding the mapper which serializer you have to used for this
param.
so Make CustomData class as---
@NoArgsConstructor
@JsonSerialize
public class CustomData<T> {

}


Related Topics



Leave a reply



Submit