After adding a custom Jackson
serializer based on the official documenation I've observed a slightly different json output format.
This example is based on spring-restbucks.
Extend org.springsource.restbucks.WebConfiguration
from RepositoryRestMvcConfiguration
and override configureJacksonObjectMapper
:
@Override
protected void configureJacksonObjectMapper(ObjectMapper objectMapper) {
final SimpleSerializers serializers = new SimpleSerializers();
serializers.addSerializer(Order.class, new OrderSerializer());
objectMapper.registerModule(new SimpleModule("CustomSerializerModule"){
@Override public void setupModule(SetupContext context) {
context.addSerializers(serializers);
}
});
}
Create class org.springsource.restbucks.order.OrderSerializer
. For the sake of brevity just write attribute paid
as JSON.
public class OrderSerializer extends JsonSerializer<Order> {
@Override
public void serialize(Order value, JsonGenerator jgen, SerializerProvider provider) throws IOException {
jgen.writeStartObject();
jgen.writeBooleanField("paid", value.isPaid());
jgen.writeEndObject();
}
}
Before adding OrderSerializer json response for http://localhost:8080/orders/1
looks like:
{
"location": "TAKE_AWAY",
"status": "PAYMENT_EXPECTED",
"orderedDate": "2014-03-24T15:05:09.988+01:00",
"items": [
{
"name": "Java Chip",
"quantity": 1,
"milk": "SEMI",
"size": "LARGE",
"price": {
"currency": "EUR",
"value": 4.2
}
}
],
"_links": {
...
}
}
After adding OrderSerializer json response for http://localhost:8080/orders/1
looks like
{
"content": {
"paid": false
},
"_links": {
...
}
}
The main pinpoint is that attribute paid is wrapped into another object content which is an attribute of org.springframework.hateoas.Resource. I've expected a response without this attribute:
{
"paid": false,
"_links": {
...
}
}
I've looked into Jackson code and found that UnwrappingBeanSerializer might be the solution I'm looking for. After looking at how to initialize UnwrappingBeanSerializer I think that this serializer is not meant to be subclassed for custom use.
I would like to know whether this deviating json format when using a custom serializer is a normal behaviour or a bug in Spring Data Rest. Any kind of help is appreciated.
jgen.writeStringField("paid", order.isPaid();
. You missed the closing parenthesis. – Oberg