Repository navigation
Closed
Description
When the content of EntityModel is a class with no defined fields, the serialization fails. As can be seen from the example below, the ObjectMapper does not have a problem with this object when sent directly.
The expectation is that if the ObjectMapper does not have a problem with the content when sent directly, then it should not have a problem when it is in the content field of the EntityModel.
@Bean
public Jackson2ObjectMapperBuilder objectMapperBuilder() {
Jackson2ObjectMapperBuilder builder = new Jackson2ObjectMapperBuilder()
.serializationInclusion(JsonInclude.Include.NON_NULL)
.failOnUnknownProperties(false)
.failOnEmptyBeans(false)
.indentOutput(false);
return builder;
}public class Widget {}@SpringBootTest
public class MapperTest {
@Autowired
ObjectMapper objectMapper;
@Test
public void widget() throws JsonProcessingException {
System.out.println("=======================");
System.out.println(objectMapper.writeValueAsString(new Widget()));
System.out.println(objectMapper.writeValueAsString(EntityModel.of(new Widget())));
System.out.println("=======================");
}
}=======================
{}
Can not start an object, expecting field name (context: Object)
com.fasterxml.jackson.core.JsonGenerationException: Can not start an object, expecting field name (context: Object)
at app//com.fasterxml.jackson.core.JsonGenerator._reportError(JsonGenerator.java:2657)
at app//com.fasterxml.jackson.core.json.JsonGeneratorImpl._reportCantWriteValueExpectName(JsonGeneratorImpl.java:248)
at app//com.fasterxml.jackson.core.json.WriterBasedJsonGenerator._verifyValueWrite(WriterBasedJsonGenerator.java:950)
at app//com.fasterxml.jackson.core.json.WriterBasedJsonGenerator.writeStartObject(WriterBasedJsonGenerator.java:311)
at app//com.fasterxml.jackson.core.JsonGenerator.writeStartObject(JsonGenerator.java:1008)
at app//com.fasterxml.jackson.databind.ser.impl.UnknownSerializer.serialize(UnknownSerializer.java:36)
at app//org.springframework.hateoas.EntityModel$MapSuppressingUnwrappingSerializer.serialize(EntityModel.java:226)
at app//com.fasterxml.jackson.databind.ser.impl.UnwrappingBeanPropertyWriter.serializeAsField(UnwrappingBeanPropertyWriter.java:127)
at app//com.fasterxml.jackson.databind.ser.std.BeanSerializerBase.serializeFields(BeanSerializerBase.java:770)
at app//com.fasterxml.jackson.databind.ser.BeanSerializer.serialize(BeanSerializer.java:178)
at app//com.fasterxml.jackson.databind.ser.DefaultSerializerProvider._serialize(DefaultSerializerProvider.java:480)
at app//com.fasterxml.jackson.databind.ser.DefaultSerializerProvider.serializeValue(DefaultSerializerProvider.java:319)
at app//com.fasterxml.jackson.databind.ObjectMapper._writeValueAndClose(ObjectMapper.java:4487)
at app//com.fasterxml.jackson.databind.ObjectMapper.writeValueAsString(ObjectMapper.java:3742)
...
Activity
[-]Error Serializing EntityModel When Content Object Contains No Fields[/-][+]Error serializing `EntityModel` when content is an empty bean and the ObjectMapper is configured to not to fail on those[/+]#1686 - Avoid serializing empty beans in EntityModel.