• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Paul Clapham
  • Ron McLeod
  • Jeanne Boyarsky
  • Tim Cooke
Sheriffs:
  • Bear Bibeault
  • Henry Wong
  • Devaka Cooray
Saloon Keepers:
  • salvin francis
  • Tim Moores
  • Tim Holloway
  • Stephan van Hulst
  • Frits Walraven
Bartenders:
  • Jj Roberts
  • Carey Brown
  • Scott Selikoff

Java Spring Boot | Adding additional data to response model

 
Ranch Hand
Posts: 41
Python Angular Framework Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi,

I am building an endpoint that fetches Quizzes object list.

The Quiz model looks like this:





And here is an example of the endpoint response:

[
   {
       "createdAt": "2019-08-17T12:00:43.522+0000",
       "updatedAt": "2019-08-17T12:00:43.522+0000",
       "id": "bf013209-9e00-4890-8c74-a40298b51e12",
       "title": "quiz v",
       "description": "This quiz is called quiz v and it is just an example, this text should be the description of the quiz",
       "imageUrl": "https://images.immediate.co.uk/production/volatile/sites/3/2018/04/Screen-Shot-2018-04-05-at-09.20.50-96984e5.png",
       "isPublic": true,
       "assignedUsers": null,
       "creatorId": "620a1b29-94f3-4f55-b55c-51b173d35113",
       "answers": [
           {
               "createdAt": "2019-08-17T12:00:43.522+0000",
               "updatedAt": "2019-08-17T12:00:43.522+0000",
               "id": "69d3ed74-4382-44f4-b874-f9fc9c2f2181",
               "content": "answer x",
               "isCorrect": true,
               "userAnswers": [
                   {
                       "createdAt": "2019-08-17T12:02:31.070+0000",
                       "updatedAt": "2019-08-17T12:02:31.070+0000",
                       "id": "2e749bff-08a8-4f20-8709-7bf3a76cbafe"
                   }
               ]
           }
       ],
       "userAnswers": [
           {
               "createdAt": "2019-08-17T12:02:31.070+0000",
               "updatedAt": "2019-08-17T12:02:31.070+0000",
               "id": "2e749bff-08a8-4f20-8709-7bf3a76cbafe"
           }
       ]
   },
   {
       "createdAt": "2019-08-17T12:00:43.514+0000",
       "updatedAt": "2019-08-17T12:00:43.514+0000",
       "id": "a2c8e185-2bd2-4f2a-b6b5-f5f6139b377d",
       "title": "quiz o",
       "description": "This quiz is called quiz o and it is just an example, this text should be the description of the quiz",
       "imageUrl": "https://images.immediate.co.uk/production/volatile/sites/3/2018/04/Screen-Shot-2018-04-05-at-09.20.50-96984e5.png",
       "isPublic": true,
       "assignedUsers": null,
       "creatorId": "620a1b29-94f3-4f55-b55c-51b173d35113",
       "answers": [
           {
               "createdAt": "2019-08-17T12:00:43.514+0000",
               "updatedAt": "2019-08-17T12:00:43.514+0000",
               "id": "53a398f7-f4ec-48e7-9551-54f2b6f90159",
               "content": "answer x",
               "isCorrect": true,
               "userAnswers": []
           }
       ],
       "userAnswers": []
   }
]




I want to have the ability to add additional information in each item of the response array,
for example, i want to add the "number-of-correct-answers" for each quiz object

But without adding an attribute to the "Quiz" class because it represents the table on DB


How can I achieve this?

My goal is to be able to have an additional information in the JSON response without altering my table


Thanks in advance,
Redan


 
Ranch Hand
Posts: 652
2
Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Adding an answer for future users.
Yes you can as many fields as you want into your response. There are 2 situations for returning a response for a request:
1. The client wants the exact similar data as the DB table has, or 2 3 extra fields.
2. There need to a lot of extra fields and their are some additional fields which require some calculation or some other entity data. Like Post can have a PostType json field.

Solutions for 1:
Simply return the fetched entity. If you want to hide some field (e.g id), use @JsonIgnore on it.
If you want 2 3 additional fields, create those fields into your Entity class and mark them as @Transient. @Transient mark that field to not be included in SQLs generated by ORM

Solution for 2:
Use Dtos(Data Transfer Object). Add as many field as you want to them. Then either use setters to feed data into that DTO or use some library like MapStruct to automap data from Entity to DTO.

And that's it, you have your own flavor of response.

Now if you even wants to take advantage of HATEOAS, you can easily do that with your DTO.
 
The harder I work, the luckier I get. -Sam Goldwyn So tiny. - this ad:
the value of filler advertising in 2021
https://coderanch.com/t/730886/filler-advertising
reply
    Bookmark Topic Watch Topic
  • New Topic