Is there a well-typed Scala (or Java) library to consume JSON Web APIs? [closed]
Asked Answered
B

1

6

I want a simple rest client library for Java (or Scala) that let's me easily do GETs/PUTs/POSTs/DELETEs etc on a JSON REST API and deserialize the JSON responses into Java objects in a type-safe way e.g.

RestClient client = new RestClient("http://api.mycompany.com").withAuth(Auth.Basic, username, password);
// This basically deserializes the JSON response into a POJO    
MyDocument[] result = client.get("/document?limit=10", MyDocument[].class);
MyFriend friend = client.post("/friend/Joe", body, MyFriend.class); 

Basically I want the generic signature to be something like this for get() e.g. public <T> T get(String path, Class<T> responseClass) which would do a GET request and deserialize the JSON response into a POJO of type responseClass

I did find a library that is pretty close to what I want called sitebricks but it is severely limited in its scope e.g. it does not allow me to do more uncommon HTTP verbs like PUT/PATCH/DELETE and it has no way of setting headers or even the body of a request.

Another library I found has the opposite problem - it has no way of doing basicauth and it does not serialize JSONs back into objects for you.

Bootless answered 19/9, 2012 at 4:2 Comment(4)
Avoid the use of "good" and "best" in titles if possible. Also, Scala is the target, not the fall-back ..Protium
So your goal is to make http requests? Have you looked into dispatch or reboot? github.com/dispatch/dispatch. If your goal is to process and respond to http requests, then unfiltered might be worth a look. github.com/unfiltered/unfilteredDetinue
Dispatch or reboot (which is kind of the new version of dispatch written in an async http client) will handle the http verbs well. Any library that claims to handle http requests without handling different http verbs shouldn't be considered a library for handling making http requests. I think the logic here is that http verbs are the basis for http requests, therefore any library should support them.Detinue
My goal is to have a thin wrapper Java/Scala client library over a JSON REST API. To do that, I not only have to make http requests but also translate the JSON responses into model Java/Scala objects.Bootless
R
1

Did you check this? Instead of SJSON you can use Salat for serializing

Roye answered 19/9, 2012 at 4:26 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.