0

I have a RestController method which returns

// The method which builds custom JSON response from retrieved data
public List<HashMap<String, Object>> queryTasks() {
        return buildResponse(...);
}

In course of time the response became bigger, additional features like search, filtering was required and operating with hashmap become harder. I want to use DTO for this response now, but there was one feature with hashmap:

AbstractProcessEntity parentDomainEntity = domainEntity.getParent();
do {
      if (parentDomainEntity != null) {
          taskMap.put(parentDomainEntity.getClass().getSimpleName() + "Id", parentDomainEntity.getId());                       parentDomainEntity = parentDomainEntity.getParent();
      } else {
          taskMap.put("parentDomainEntityId", null);
      }
} while (parentDomainEntity != null);

JSON response was dynamically build tree for domain entities with not null parents.

Doing it in DTO will lead to creating variable for each parent entity and populating them with null (It's possible to have 5 levels of parent-child entities).

How I can dynamically build response like in my first case with HashMap?

2
  • can you explain better ? so this List<HashMap<String, Object>> will be your JSON, would be like ["one": "one", "one":"oneandhalf", "two": "two", "three": "three", "four": ""] ? Commented Feb 9, 2017 at 15:25
  • Exactly. Response is the same with List<HashMap<String, Object>> and List<DTOobject> except that I can't 'hide' some JSON fields in response which are not available. Commented Feb 9, 2017 at 17:25

1 Answer 1

1

You can use a custom Jackson serializer for this, writing either a null value or the actual parent object using the correct property name:

public class ProcessEntitySerializer extends StdSerializer<ProcessEntity> {


   ...

    @Override
    public void serialize(ProcessEntity entity, JsonGenerator jgen,
        SerializerProvider provider) throws IOException, JsonProcessingException {
        gen.writeStartObject();

        if (entity.getParentDomainEntity() == null) {
            // No parent, write null value for ID
            jgen.writeNullField("parentDomainEntityId");
        } else {
            // Parent is known, write it as an object
            jgen.writeObjectField(entity.getParentDomainEntity().getClass().getSimpleName(),
            entity.getParentDomainEntity());
        }

        // TODO: since you're using a custom serializer, you'll need to serialize any additional properties of the entity manually 
        // or use the Schema exposed by the base class StdSerializer

        jgen.writeEndObject();
    }
}
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.