Win a copy of Murach's Python Programming this week in the Jython/Python forum!
  • Post Reply Bookmark Topic Watch Topic
  • New Topic

Consuming a RESTful web service from a batch Java program  RSS feed

 
Tim Bee
Ranch Hand
Posts: 91
1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
What is the current best practice way to call a RESTful web service from a batch program? I have a simple service that just returns some basic JSON information.
How do I call it from a Java program and then have the returning data marshalled/unmarshalled into a Java object.
Does anyone have any links to an up-to-date example or can someone do a cut and paste here so I can see what I am supposed to use?

Thanks,
Tim
 
Stephan van Hulst
Saloon Keeper
Posts: 6980
110
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi Tim,

As REST is usually built on top of HTTP, you can use a HttpURLConnection to send a request and then handle the content. I imagine there are third party libraries that make this easier though. These look promising:

https://developers.google.com/api-client-library/java/google-http-java-client/
http://unirest.io/java.html
 
Tim Bee
Ranch Hand
Posts: 91
1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I can call the web service but I get a JSON response.
Is there any standardized way of converting that to an object of some kind or parsing it?
Jackson or something?
Are there any examples I anyone knows of so I can look at the syntax?

Thanks
 
Stephan van Hulst
Saloon Keeper
Posts: 6980
110
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
There are plenty of libraries out there that can map JSON to Java objects. I believe both of the libraries I linked to above provide this functionality. Have you gone through their code-samples and documentation?
 
Tim Bee
Ranch Hand
Posts: 91
1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Stephan van Hulst wrote:There are plenty of libraries out there that can map JSON to Java objects. I believe both of the libraries I linked to above provide this functionality. Have you gone through their code-samples and documentation?


What I am asking is - are there any libraries that are used by most big corporations. I am just trying to learn what technologies and frameworks would be used for that.
If you are saying that both of these are the most common way of doing it, I will look at them.
 
Tim Holloway
Bartender
Posts: 18531
61
Android Eclipse IDE Linux
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Speaking as an occasional employee of big corporations, I google for libraries. Anything belonging to the Apache or CodeHaus projects is favored, although since I do a lot of Spring Framework stuff, if there's a Sping library, it may get first choice.

When none of the above seem suitable, I just try different ones to see what works best for me. I think last time I needed one, it was called "serpent" or something like that.
 
Tim Bee
Ranch Hand
Posts: 91
1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Tim Holloway wrote:Speaking as an occasional employee of big corporations, I google for libraries. Anything belonging to the Apache or CodeHaus projects is favored, although since I do a lot of Spring Framework stuff, if there's a Sping library, it may get first choice.

When none of the above seem suitable, I just try different ones to see what works best for me. I think last time I needed one, it was called "serpent" or something like that.


I'm thinking what I am doing must be fairly common and I would think that there are some libraries that are favoured for what I am doing.

Am I incorrect in thinking that calling RESTful web services from Java programs and then manipulating the returning JSON data is a common thing?
 
Tim Holloway
Bartender
Posts: 18531
61
Android Eclipse IDE Linux
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Beats SOAP/XML for most cases.
 
Bear Bibeault
Author and ninkuma
Marshal
Posts: 65833
134
IntelliJ IDE Java jQuery Mac Mac OS X
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
It's a very common thing. Very.

If a framework doesn't already have JSON capability built in, what I've seen used most often is Gson or Jackson.
 
Tim Bee
Ranch Hand
Posts: 91
1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Bear Bibeault wrote:It's a very common thing. Very.

If a framework doesn't already have JSON capability built in, what I've seen used most often is Gson or Jackson.


Thank you.
 
Ron McLeod
Saloon Keeper
Posts: 1431
207
Android Angular Framework Eclipse IDE Java Linux MySQL Database Redhat TypeScript
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Bear Bibeault wrote:If a framework doesn't already have JSON capability built in, what I've seen used most often is Gson or Jackson.

Those two are my go-to libraries for JSON - Java SE/EE and Android, client or server side, with or without frameworks.
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!