InvalidDefinitionException No serializer found in java for generic class - java

I have a generic class
#NoArgsConstructor
public class CustomData<T> {
}
Below is the class it is used.
#Data
#AllArgsConstructor
#NoArgsConstructor
#Builder
public class Recipient {
#NotEmpty(message = "Please provide email details of recipient")
public String email;
public CustomData<?> custom_data;
}
Below is the payload I'm trying to use
"recipients": [
{
"custom_data": {},
"email": "string"
}
]
However I get an error saying com.fasterxml.jackson.databind.exc.InvalidDefinitionException: No serializer found for class
Can someone please help? Thank you for your time

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

Jackson deserialization feature which fails if not all properties are present?

Let's say I have this POJO:
#Data
public class MyPojo {
private boolean isEnabled;
private int timeout;
}
Deserialization from JSON to the above POJO is done with Jackson 2.9. Now, additionally let's say a client sends an "invalid" request. In this context "invalid" means that one of the POJO properties is null or empty. Here, timeout is missing.
{
"isEnabled": true
}
So far I could not find any Jackson deserialization feature which would make this case fail. Does Jackson provide something like "fail if not all properties are present"?
This one won't work:
#JsonProperty(required = true)
You can use constructor-based annotation - it works as a workaround for this unimplemented Jackson feature:
#JsonCreator
public Foo(#JsonProperty(value = "val", required = true) int val) {
this.val = val;
}

Jackson's BeanDeserializerModifier does not work with final fields

I wrote a Custom Serializer and Custom Deserializer to serialize properties marked with #Confidential annotation.
#Data
public class Person {
private String name;
#Confidential
private String address;
}
The Custom Serializer serializes a POJO with following values:
{ "name": "John Doe", "address": "Kearney St"}
as follows:
{"name":"John Doe", "address": {"value":"IjIwMzEwIDU4dGggTG4gTkUi"}}
The Custom Deserializer is also able to deserialize the JSON back to the Person POJO fine.
However, when I make the fields in the Person POJO final, serialization continues to work, but deserialization fails.
#Data
public class Person {
private final String name;
#Confidential
private final String address;
}
Here's the BeanSerializerModifier implementation:
#AllArgsConstructor
public class CustomDeserializerModifier extends BeanDeserializerModifier {
private final ObjectMapper objectMapper;
#Override
public BeanDeserializerBuilder updateBuilder(final DeserializationConfig config,
final BeanDescription beanDesc,
final BeanDeserializerBuilder builder) {
Iterator<SettableBeanProperty> beanPropertyIterator = builder.getProperties();
beanPropertyIterator.forEachRemaining(settableBeanProperty -> {
final Confidential annotation = settableBeanProperty.getAnnotation(Confidential.class);
if (encryptedProperty != null) {
JsonDeserializer<Object> current = settableBeanProperty.getValueDeserializer();
final SettableBeanProperty newSettableBeanProperty =
settableBeanProperty.withValueDeserializer(
new CustomDeserializer(annotation, current, objectMapper)
);
builder.addOrReplaceProperty(newSettableBeanProperty, true);
}
});
return builder;
}
}
I found that CustomDeserializer, never gets called when the Person POJO fields are final.
Here's the error message:
com.fasterxml.jackson.databind.JsonMappingException: Can not deserialize instance of java.lang.String out of START_OBJECT token
at [Source: {"name":"John Doe","address":{"value":"IjIwMzEwIDU4dGggTG4gTkUi"}}; line: 1, column: 30] (through reference chain: com.custom.model.Person["address"])
Can a Jackson expert please tell me why my CustomDeserializer isn't getting invoked when the POJO fields are final.
Thank you!
As mentioned, serialization will perfectly work for both mutable and immutable fields. Deserialization issue will only occur when using immutable fields as the BeanDeserializerModifier won't work in such a case.
In the Jackson terminology, immutable fields are named creator properties, meaning they are initialized using creators. See BeanDeserializerBase#resolve.
To correctly handle this use case, the ObjectMapper may be created with a custom DeserializationContext (an extended implementation of the ObjectMapper may also set the protected field related to the deserialization context).
Then, through an override of the method DeserializationContext#handleSecondaryContextualization, it will be possible to change the deserialization to make it work.
There is maybe other possibilities but this one is working fine with encryption.

Jackson: Use default (de)serializer

I'm trying to (de)serialize an object that has a property with a type that comes from a maven dependency, so I can't change the class of this type.
The class of this type has a #JsonSerialize and #JsonDeserialize annotation.
However, I want to use the default serializer and deserialzer, because the custom serializer writes an array instead of an object. Is there a way, using annotations, to tell jackson to use the default (de)serializer?
You can disable the annotations using Jackson's mixins feature.
In the following example, any attempt at deserializing to a CustomerObj will result in an exception due to its defective Builder:
#JsonDeserialize(builder = CustomerObj.class)
public class CustomerObj {
public String name;
public int age;
public CustomerObj build() {
throw new RuntimeException("JsonDeserializer invoked");
}
}
Create a mixin with a JsonDeserialize annotation that disables the broken builder:
#JsonDeserialize(builder = java.lang.Void.class)
public static abstract class CustomerMixin { }
Register the mixin on the ObjectMapper instance:
ObjectMapper om = new ObjectMapper();
om.addMixIn(CustomerObj.class, CustomerMixin.class);
Enjoy working deserialization:
final String json = "{\"name\":\"Brian\",\"age\":41}";
CustomerObj customer = om.readValue(json, CustomerObj.class);

Jersey - JSON marshall only specific fields

My REST service returns following JSON
{
"name": "John",
"id" : 10
}
Can I use Jersey to marshall it into following Bean:
public class User{
private String name;
//getter & setter
}
I wanted to do this with following code but it doesn't work
WebResource webResource = client.resource(url);
webResource.accept(MediaType.APPLICATION_JSON_TYPE);
User user = webResource.get(User.class);
Is this even possible or I have to implement full JSON structure in Java Beans to get it work?
I know that I can parse this JSON with Jackson and any other methods.
With Jackson, easiest way is to configure ObjectMapper like so:
objectMapper.configure(DeserializationConfig.Feature.FAIL_ON_UNKNOWN_PROPERTIES,
false);
Check this sample provider
package com.company.rest.jersey;
#Provider
#Component
#Produces({MediaType.APPLICATION_JSON})
public class JacksonMapperProvider implements ContextResolver<ObjectMapper> {
ObjectMapper mapper;
public JacksonMapperProvider(){
mapper = new ObjectMapper();
mapper.configure(Feature.INDENT_OUTPUT, true);
// Serialize dates using ISO8601 format
// Jackson uses timestamps by default, so use StdDateFormat to get ISO8601
mapper.getSerializationConfig().setDateFormat(new StdDateFormat());
// Deserialize dates using ISO8601 format
// MilliDateFormat simply adds milliseconds to string if missing so it will parse
mapper.getDeserializationConfig().setDateFormat(new MilliDateFormat());
// Prevent exceptions from being thrown for unknown properties
mapper.configure(
DeserializationConfig.Feature.FAIL_ON_UNKNOWN_PROPERTIES,false);
}
#Override
public ObjectMapper getContext(Class<?> aClass) {
return mapper;
}
}
With Jackson :
You have two options:
Jackson works on setters-getters of fields. So, you can just remove getter of field which you want to omit in JSON. ( If you don't need getter at other place.)
Or, you can use the #JsonIgnore annotation of Jackson on getter method of that field and you see there in no such key-value pair in resulted JSON.
#JsonIgnore
public int getSecurityCode(){
return securityCode;
}
In your bean, add the annotation #JsonIgnoreProperties(ignoreUnknown = true) at the class level and it should skip the id property in the JSON since it's not present in the bean.
#JsonIgnoreProperties(ignoreUnknown = true)
public class User{
private String name;
//getter & setter
}
(See http://wiki.fasterxml.com/JacksonAnnotations for details)

De-serializing JSON to polymorphic object model using Spring and JsonTypeInfo annotation

I have the following object model in my Spring MVC (v3.2.0.RELEASE) web application:
public class Order {
private Payment payment;
}
#JsonTypeInfo(use = JsonTypeInfo.Id.NAME, include = As.WRAPPER_OBJECT)
#JsonSubTypes.Type(name = "creditCardPayment", value = CreditCardPayment.class)
public interface Payment {}
#JsonTypeName("creditCardPayment")
public class CreditCardPayment implements Payment {}
When I serialise the Order class to JSON, I get the following result (which is exactly what I want):
{
"payment" : {
"creditCardPayment": {
...
}
}
Unfortunately, if I take the above JSON and try to de-serialise it back into my object model, I get the following exception:
Could not read JSON: Could not resolve type id 'creditCardPayment'
into a subtype of [simple type, class Payment] at [Source:
org.apache.catalina.connector.CoyoteInputStream#19629355; line: 1,
column: 58] (through reference chain: Order["payment"]); nested
exception is com.fasterxml.jackson.databind.JsonMappingException:
Could not resolve type id 'creditCardPayment' into a subtype of
[simple type, class Payment] at [Source:
org.apache.catalina.connector.CoyoteInputStream#19629355; line: 1,
column: 58] (through reference chain: Order["payment"])
My application is configured via Spring JavaConf, as follows:
#Configuration
#EnableWebMvc
public class AppWebConf extends WebMvcConfigurerAdapter {
#Bean
public ObjectMapper objectMapper() {
ObjectMapper objectMapper = new ObjectMapper();
objectMapper.setSerializationInclusion(Include.NON_NULL);
objectMapper.configure(MapperFeature.DEFAULT_VIEW_INCLUSION, false);
return objectMapper;
}
#Bean
public MappingJackson2HttpMessageConverter mappingJacksonMessageConverter() {
MappingJackson2HttpMessageConverter converter = new MappingJackson2HttpMessageConverter();
converter.setObjectMapper(objectMapper());
return converter;
}
#Bean
public Jaxb2RootElementHttpMessageConverter jaxbMessageConverter() {
return new Jaxb2RootElementHttpMessageConverter();
}
#Override
public void configureMessageConverters(List<HttpMessageConverter<?>> converters) {
converters.add(jaxbMessageConverter());
converters.add(mappingJacksonMessageConverter());
}
}
For testing, I have a controller with 2 methods, one returns an Order for HTTP GET request (this one works) and one that accepts an Order via a HTTP POST (this one fails), e.g.
#Controller
public class TestController {
#ResponseBody
#RequestMapping(value = "/test", method = RequestMethod.GET)
public Order getTest() {}
#RequestMapping(value = "/test", method = RequestMethod.POST)
public void postTest(#RequestBody order) {}
}
I have tried all suggestions from the various discussions on SO but so far had no luck. Can anyone spot what I'm doing wrong?
Try to register subtype using ObjectMapper.registerSubtypes instead of using annotations
The method registerSubtypes() works!
#JsonTypeInfo(use=JsonTypeInfo.Id.NAME, include=JsonTypeInfo.As.PROPERTY, property="type")
public interface Geometry {
//...
}
public class Point implements Geometry{
//...
}
public class Polygon implements Geometry{
//...
}
public class LineString implements Geometry{
//...
}
GeoJson geojson= null;
ObjectMapper mapper = new ObjectMapper();
mapper.disable(DeserializationConfig.Feature.FAIL_ON_UNKNOWN_PROPERTIES);
mapper.registerSubtypes(Polygon.class,LineString.class,Point.class);
try {
geojson=mapper.readValue(source, GeoJson.class);
} catch (IOException e) {
e.printStackTrace();
}
Note1: We use the Interface and the implementing classes. I fyou want jackson to de-serialize the classes as per their implementing classes, you have to register all of them using ObjectMapper's "registerSubtypes" method.
Note2: In addition you use, " #JsonTypeInfo(use=JsonTypeInfo.Id.NAME, include=JsonTypeInfo.As.PROPERTY, property="type")" as annotation with your Interface.
You can also define the order of properties when mapper writes a value of your POJO as a json.
This you can do using below annotation.
#JsonPropertyOrder({"type","crs","version","features"})
public class GeoJson {
private String type="FeatureCollection";
private List<Feature> features;
private String version="1.0.0";
private CRS crs = new CRS();
........
}
Hope this helps!
I had a similar issue while working on a dropwizard based service. I don't fully understand why things didn't work for me in the same way that the dropwizard code works, but I know why the code in the original post doesn't work. #JsonSubTypes wants an array of sub types, not a single value. So if you replace the line...
#JsonSubTypes.Type(name = "creditCardPayment", value = CreditCardPayment.class)
with...
#JsonSubTypes({ #JsonSubTypes.Type(name = "creditCardPayment", value = CreditCardPayment.class) })
I believe your code will work.
For those that are having this same error message pop up, you may be having an issue with the subtypes being discovered. Try adding a line like the one above or looking for issue with the discovery of the classes that have the #JsonTypeName tag in them.
Rashmin's answer worked, and I found an alternative way to avoid the com.fasterxml.jackson.databind.JsonMappingException: Could not resolve type id into a subtype of Blah issue without needing to use registerSubtypes. What you can do is add the following annotation to the parent class:
#JsonTypeInfo(use = JsonTypeInfo.Id.CLASS, include = JsonTypeInfo.As.PROPERTY, property = "type")
Note that the difference is JsonTypeInfo.Id.CLASS instead of JsonTypeInfo.Id.NAME. The downside is that the created JSON will contain the entire class name including the full namespace. The upside is that you don't have to worry about registering subtypes.
In my case I had added defaultImpl = SomeClass.class to #JsonTypeInfo and was trying to convert it SomeClass2.class
Encountered the same error and used the equivalent of the below JSON (instead of CreditCardPayment used my class name) as the input for deserializer and it worked:
{
"type": "CreditCardPayment",
...
}
In my case it was an old version of jackson-databind. I solved with version 2.11.0

Categories

Resources