I have a Swing application that is done except for the web api part. They have a REST api, but when I was looking at example of using a REST api in java, they all use a java web application, and I can't find any for a desktop swing application. So is it still possible to do so?
The code for REST APIs can be written in multiple languages but Java Programming Language due to its “write once run anywhere” quality is preferred for creating these APIs. This article will introduce you to the Java Programming Language and REST APIs along with their key features.
Webswing is a web server that allows you to run any Java Swing application inside your web browser, using only pure HTML5.
Swing is a GUI widget toolkit for Java. It is part of Oracle's Java Foundation Classes (JFC) – an API for providing a graphical user interface (GUI) for Java programs.
Swing is a newer way of making a GUI. All objects in Swing are derived from AWT, and most objects in Swing start with the letter J. Hint: Oracle has developed JavaFX to replace both Swing and AWT. Since Java SE 7, update 6 it is bundled with Java SE.
Yes. It is possible.
You can Consume (access and read) a REST web service in Swing desktop application.
You can achieve it using HTTPClient
.
An example - http://www.mkyong.com/webservices/jax-rs/restful-java-client-with-apache-httpclient/
Use Spring RestTemplate an example here.
RestTemplate rest = new RestTemplate()
rest.postForObject("http://localhost:8080/WebApp/ServiceName", requestBean, Response.class);
This should return an object of type Response. For other HTTP operations supported by RestTemplate see the Spring documentation.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With