KnowledgeHub
Questions
Tags
Users
Search
Alex Rivera
|
Logout
Edit Question
Title
Body
I've got following classes: public class Container { private String name; private Data data; } public class Data { private Long id; } When I serialize Container class using Jackson I get {"name":"Some name","data":{"id":1}} But I need the result to be: {"name":"Some name","id":1} Is it possible (without adding Container.getDataId() method)? If so, how to do it? update I've tried to create custom JsonSerializer<Data> but the result was same as before public class JsonDataSerializer extends JsonSerializer<Data> { private static Logger logger = Logger.getLogger(JsonDataSerializer.class); @Override public void serialize(Data value, JsonGenerator jgen, SerializerProvider provider) throws IOException,JsonProcessingException { Long id = (value.getId() == null) ? 0l : value.getId(); jgen.writeStartObject(); jgen.writeNumberField("id", id); jgen.writeEndObject(); logger.debug("Data id " + id + " serialized to JSON."); } } I've also tried to add @JsonSerialize annotation above Data class, then above getter in Container class. As I mentioned before without any success. My serializer is used, logger logs message. update 2 When I remove writeStartObject() and writeEndObject() then no JSON is returnesd, only HTTP Status 500 error and no exception is thrown except of what I found in debug output. DEBUG: org.springframework.web.servlet.mvc.annotation.AnnotationMethodHandlerExceptionResolver - Resolving exception from handler [com.example.DataController@16be8a0]: org.springframework.http.converter.HttpMessageNotWritableExcept
Tags (comma-separated)
Save Edits
Cancel