0

I have debate with my team, I have a rest ws that return something in certian format The format look something like that

{  
    Name:{  
        Inner:[  
            {  
                inner:[  
                    {  
                        inner:"hdjdjd"
                    }
                ]
            }
        ]
    }
}

In the ws I return data in this format only once.

My teams think that I should create the json using jacksons object node in the response method. And I think that I should model the format in pojo class and then return the the class json represantation (using jackson) What is the right option?

Andrew Tobilko
  • 48,120
  • 14
  • 91
  • 142
Amit Mizrahi
  • 67
  • 1
  • 7

1 Answers1

0

That's a matter of taste.

If this JSON format uses not once, I advise you to create DTO or POJO* object and return it.

Otherwise, return a jacksons object node, why not?

* All DTOs are POJOs, but not all POJOs are DTOs. An example of POJO that is not a DTO is a business class that contains state and behavior (business logic).

Community
  • 1
  • 1
Andrew Tobilko
  • 48,120
  • 14
  • 91
  • 142
  • Becasue the code is much less readable, instead of creating object of the return format I creates json object which is not native to java and I think- new Format(.....) instead of new ObjectNode().put.put..... etc.. is more readble, isnt it? – Amit Mizrahi Mar 10 '16 at 11:08
  • @user5723395, yes, it will be more readable and compactly in the response method, but you need to create a new class that doesn't contain any logic and keeps only data – Andrew Tobilko Mar 10 '16 at 11:33
  • This is the idea of DTO design pattern – Amit Mizrahi Mar 10 '16 at 14:42
  • I see the following advantages in using a POJO 1) Type checking, 2) Clarity in the response structure, 3) Selective Deserialization ability. 4) Code reusability when the pojos are externalized by the serving service. Why not a POJO then? – Prasanna Feb 07 '19 at 23:45
  • @Prasanna I agree. It was a while ago, I need to revise this answer. – Andrew Tobilko Feb 08 '19 at 00:30
  • @Prasanna The Jacksons representation is the only option when the structure of JSON is unknown or its format is changing dynamically and unpredictably. In such cases, it's better to have a `JSONNode` instead of a `POJO` with abstract `Object` or `Map` fields – Andrew Tobilko Feb 08 '19 at 00:37
  • @AndrewTobilko I totally agree. – Prasanna Feb 08 '19 at 15:19