nbro
nbro

Reputation: 15837

How to do requests to a server-side Spring app from a client-side desktop app?

We're going to create a client-server side application, where the client side was initially requested to be a desktop app. We have no experience in creating these applications using Java, but we have already started thinking about tools that we're going to use.

For example, we decided that we're going to use Spring as the framework that will receive the requests, process them, by eventually fetching data from the data base (MySQL), and then return a response.

I'm not sure yet what will be the format of these responses (and requests from the client-side app), but it should be as much independent as possible from the client-side, so that if the customer changes opinion and decides that he/she wants a web or a mobile app, we don't need to change the server-side code that processes all the requests.

By looking at a project's source code provided by of one of us, who had already have some experience in this field, apparently he had used HttpServletRequest and HttpServletResponse objects as parameters of all methods of the controllers. I think it's a server-side Spring app. In this case, specifically, it would be a controller class, which processes requests to a certain URL.

The code is easier to understand, because it's similar to how I had worked with Javascript, Node, Express and Mongo sometime ago. It's all based on the MVC pattern. In that environment, requests were made either by writing the URL on the browser or by using XMLHttpRequest (AJAX).

Now, my doubts and problems are:

  1. If we really need to create a desktop app, for example with Swing, how are we going to do requests to the Spring controllers?

  2. What's the best way to decouple the server-side code from the client-side code? Is JSON useful here? If yes, how roughly would it be used?

  3. Should we use a framework also on the client-side that has the specific job of doing requests to the Spring controllers when necessary, or is it enough to somehow hard-code the requests? And how would this framework eventually deal with Swing?

  4. Where would JDBC fit here? In the server-side code, I suppose. But when should we use it exactly?

Sorry, these are a lot of questions, but we're not introduced to these environments before, so we're somehow lost.

Upvotes: 1

Views: 2234

Answers (1)

Utku Özdemir
Utku Özdemir

Reputation: 7725

If you are going to use Spring MVC and REST controllers on your server side, you need a REST client library which will send HTTP requests to your backend. It doesn't necessarily need to be connected to Spring in any way.

  1. You can check the REST clients from this answer, and pick the one you like. UniRest might be a good choice for the desktop app.

  2. Decoupling is done by providing a Restful API, means any client which can send HTTP requests can use your backend services. You can write your client app (desktop app in your case) in any programming language, with any framework you want. Communication is done over HTTP, and that's a great example of decoupling (unlike RMI for example). And yes, JSON might be a good choice, most of the rest APIs use JSON as the data-interchange format. But you don't need to, you can use XML or any other format as well (but I'd strongly recommend JSON and Jackson as the library)

  3. You can also follow MVC approach on your client app, but it depends on the framework you use etc. but basically what you need is just send requests to the backend, you can do it any way you want. To have a nice structure, you can hide your data endpoints behind an interface, and the specific implementation of the interface makes calls to the backend and provides the data. You can also externalize the endpoint URLs to a configuration file to keep a well-organized structure. Or if it's just a simple project, then yes, you can hardcode and do everything inline. It's totally up to you.

  4. In Java JDBC is just used to connect to a database. Therefore if you do not have a (relational) database, you don't need it at all. Since you mentioned you will use MySQL, you need it on the server-side. Just grab the MySQL JDBC driver (if you use Maven get the dependency from here), it is the implementation of the JDBC API for MySQL connectivity.

And one last thing: instead of using the old &outdated Swing, consider using JavaFX. It is the new preferred way of doing desktop app user interfaces in Java.

Upvotes: 1

Related Questions