Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Is it possible to use a REST or web api in a java Swing application

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?

like image 234
rasen58 Avatar asked Aug 13 '14 13:08

rasen58


People also ask

Can I use REST API in Java?

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.

Can Java Swing be used in web applications?

Webswing is a web server that allows you to run any Java Swing application inside your web browser, using only pure HTML5.

Is Java Swing an API?

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.

What has replaced Java Swing?

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.


2 Answers

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/

like image 106
Ninad Pingale Avatar answered Dec 25 '22 09:12

Ninad Pingale


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.

like image 31
monim Avatar answered Dec 25 '22 07:12

monim