Win a copy of Practical SVG this week in the HTML/CSS/JavaScript forum!
  • Post Reply Bookmark Topic Watch Topic
  • New Topic

Response Formatting of Spring REST API

 
Partheban Udayakumar
Ranch Hand
Posts: 496
AngularJS Java Spring
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi all,

I am developing RESTful WebServices using Spring. I have this thing bugging me. Say I have a custom object as below:



As you can see this is a hibernate model class and it has many parameters. Most of the time I won't add the Set's or the createdBy/updatedBy and createdTime/updatedTime to the response. So when I am returning this class as response it will convert to a JSON object and will return many null fields. How can this be avoided? Currently we are creating a new non Hibernate model class and we add the params to it. I don't think this would be the correct way. Also this increases the number of files used in the project. And if I have to send the date and time, it would be returned as Long from this class but we will have to format it to our required format and add it to the new class and send it. This is OK for many dates but for one date, I don't think we should create a new class just to return it in the required format. If my current method is wrong, kindly guide me to the correct way.
 
K. Tsang
Bartender
Posts: 3610
16
Firefox Browser Java Mac OS X
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
The date formatting can be done by JSON itself. Don't know what JSON library you are using but I've been using Jackson and it's quite handy.

For date format, since your variable is a Date object, you can use this:


This approach will mean you can't declare multiple variables on one line.

As for the null values in response, what's wrong with it? You can always give it a default like empty string for Strings (acceptable) or 1900-01-01 for Dates (not recommended).
 
Partheban Udayakumar
Ranch Hand
Posts: 496
AngularJS Java Spring
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
K.Tsang,

Thanks for the reply. I am sorry for the late reply as I was a little busy with my work in office. Thanks for the date formatting in json, it would help me a lot. I too use jackson. As for null values, Say I am sending only id back then all other variables in the User class would be null. As I said earlier, this is OK if it is 2 or 3 variables but for a whole bunch of variables, it is not in the pretty format. I have a doubt too. Does returning these null variables affect the performance? AFAIK I didn't observe anything as such until now.
 
K. Tsang
Bartender
Posts: 3610
16
Firefox Browser Java Mac OS X
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
If you are just sending ID back (in your response), why use a User return type, make it long or int or BigInteger (just match with the User class).

 
Partheban Udayakumar
Ranch Hand
Posts: 496
AngularJS Java Spring
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
K.Tsang,

For single variable what you are suggesting is right. Let's take an example, say I am returning 4 variables of User class,
1) id(Integer)
2) fullName(String)
3) status(String)
4) createdDate(Date)
Then the whole bunch of other variables will be null. How should I proceed with this?

 
Dave Tolls
Ranch Hand
Posts: 2295
25
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
What is the client expecting?

If your service is defined as returning a User object then I would expect to see a User object, even if that meant a load of the fields were null.

If the contract is for an ID (eg getUserIdFromName()) I would expect only an ID.

So what is the contract?
 
Partheban Udayakumar
Ranch Hand
Posts: 496
AngularJS Java Spring
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Dave Tolls,

It is expecting a User object. The contract is something like getUserBasicDetails(). Just for showing a minimal profile page. I am writing this for an Android application. I am not against returning null values but just want to make my response pretty.
 
Dave Tolls
Ranch Hand
Posts: 2295
25
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Why does your response have to be pretty?
It's going to be read by a computer, that really (really) doesn't care so long as it's formatted correctly and has the data it's expecting.

If the contract says to return a User object then return a User object even if bits of it are null.

I will say that "UserBasicDetails" implies (to me) something different to a User object, though.
 
Partheban Udayakumar
Ranch Hand
Posts: 496
AngularJS Java Spring
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Dave Tolls,

Thanks for the reply. So User object can be returned with null values and also that doesn't affect the response timing. So we don't have to be worried about making our output pretty.
 
Dave Tolls
Ranch Hand
Posts: 2295
25
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
No need at all.
I can't see it having a noticeable effect on anything.

I've seen the same thing with XML responses where people complained they weren't easy for a human to read, and my reply has been the same. This isn't intended for a human to read.
 
You can't expect to wield supreme executive power just because
the new thread boost feature: great for the advertiser and smooth for the coderanch user
https://coderanch.com/t/674455/Thread-Boost-feature
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!