0

I want to create a library which will do the authentication stuff (sending some authentication parameters along the request) and return the json response from an API. The JSON will usually represent an array of dictionaries, something like this:

[
  {"a": 1, "b": 2},
  {"a": 3, "b": 1}
]

I have done the same thing in Python and there it was no-brainer, I just return a dict which is builtin in Python.

I was wondering what would be the best way in Java? Here are some of the approaches I consider:

  1. Returning a stream which will be handled by user. Specifically, I use java.net.HttpURLConnection and by this approach I will return connection.getInputStream() which will be handled by the user. The drawback of this approach is that the user would need to do great part of the job (reading the stream and converting it to json-like object or whatever). The good part is the users will have flexibility to process it in any way they like, creating custom objects from classes, using third party json libraries or something else. By this approach I don't force them to use some library, because the whole code will use Java builtin functionalities.
  2. Use third party library, like org.json or GSON and return that kind of object. The drawback here is that I'll force the user to use third party library.
  3. I could define classes for each kind of objects, there would be probably 5-6 type of classes. The drawbacks are: I will need more time to implement the solution and will force the user to use my objects.

What bothers me is that Python developers very often use dict, while in Java the developers most often prefer to work with objects of specific user defined classes. So, I can't conclude what would be the most appropriate way.

1
  • See the http-request Commented Nov 19, 2017 at 19:45

1 Answer 1

1

Any endpoint for an HTTP API has a defined response structure (or at least it should). The methods implemented by your library should return a POJO that represents the response payload. The end user should not need to be aware that the API formats the payload in JSON. If the API one day decides to use XML, that change should be transparent to the users of your library.

Edit: simply returning a dictionary, or map type is bad practice. Doing so forces users of your library to assume the key strings and value type of each property in the response. A POJO provides a clear definition of the response property names and their value types.

Sign up to request clarification or add additional context in comments.

5 Comments

So, in my example, I should return something like: List<MyEntity> where MyEntity is a POJO class with properties a and b?
@giliev exactly
Ok, in my scenario it seems doable because there aren't too many different POJO classes (less than 10), but what if there were 100 possible POJOs? I mean it's still doable, but I wonder if there is some approach which would provide a good user experience, but still let me get the job done with less code.
Thank you for the answer, now I saw the Edit, that's a good point. Still I would like to hear what you think about my remarks in the previous comment.
@giliev If the API is so large that it provides responses for over 100 different types, perhaps it might need separated into smaller APIs. However, in the event that 100 is necessary, then yes, your library would need a POJO for each. This is very common in OOP. However, an API should provide responses for business domain objects. If you have over 100 of those, then you have some work ahead of you.

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.