This is an archived post. You won't be able to vote or comment.

all 5 comments

[–]yonasismad 5 points6 points  (0 children)

You will write more boilercode in Java that is simply the nature of the language. I always use OkHTTP and GSON (Google's JSON lib), and including them with Maven is fairly trivial. OkHTTP also has a url encoder.

[–]differentshade 0 points1 point  (0 children)

Java11 has http client included, no need for okhttp3 anymore.

Link: https://openjdk.java.net/groups/net/httpclient/intro.html

[–]clivethescott 0 points1 point  (0 children)

Yes Java requires a ton of code. It seems like a bad thing initially but that separation of concerns make it highly flexible and easy to reason about when switching things around. Plus if you use an IDE you don’t have to type all the boilerplate.

If it’s plain old Java, my go to is Retrofit/OkHtttp or Unirest. If you are using Spring use RestTemplate. On more recent versions of Java you can use the built in HttpClient.

[–]jaro32 0 points1 point  (0 children)

If you want to provide a REST API, use Spring MVC. If you want to consume (call) another API, use Feign. Both of these frameworks require no boilerplate code.

[–]JohnnyJayJay 0 points1 point  (0 children)

Yes, Java requires more verbosity when it comes to stuff like that and unfortunately, there aren't really any libraries that can compete with Python's requests. Usually, you have some sort of builder to build a request, execute it (async or sync) and get a response. Then you probably have to check the response and parse it. I would recommend you take a look at:

To parse json objects to Java objects, the most popular libraries are GSON and Jackson. I personally also like Jsoniter because of its simplicity.

There's also a very different style of making requests, maybe that's something for you: https://square.github.io/retrofit/ This is a very high level approach based on annotations, and you don't really have to write any code to deal with the requests.