1

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.

nbro
  • 15,395
  • 32
  • 113
  • 196
  • You can use https://hc.apache.org/httpcomponents-client-ga/ to do request on client site. Just serialize you requests to JSON, send it to serve, and unsirialize JSON, that will come from server. – Ken Bekov Mar 05 '16 at 11:38

1 Answers1

1

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.

Community
  • 1
  • 1
Utku Özdemir
  • 7,390
  • 2
  • 52
  • 49
  • Point 3. "You can also follow MVC approach on your client app, but it depends on the framework you use etc". By framework that we use, you mean which REST library we use on the client-side, or what? Or if not, which frameworks would allow me to set up also a MVC approach on the client-side? Or is a rest library exactly this? – nbro Mar 05 '16 at 11:50
  • Could you please clarify better point 3 in general (if you have time and will :)? I think I don't understand well what you're saying... – nbro Mar 05 '16 at 11:52
  • Point 4. The more experienced team mate suggested to use MySQLWorkbench to deal with the database. Why do we need JDBC then at this point? Or if we still need it, where exactly would it fit? – nbro Mar 05 '16 at 11:54
  • @nbro No, rest library is not related to this at all. MVC is a pattern which separates the model, view and the controller means separating your business logic from your UI and its associated data. It depends on the UI framework you use. – Utku Özdemir Mar 05 '16 at 11:54
  • @nbro MySQL workbench is a graphical tool to manipulate the database, send queries etc. It's an user product, not programmable, does not provide an API etc. it's a totally different thing from JDBC. JDBC is an API, means you can use it on your code, call it's methods/interfaces from your Java code to communicate the database, get the data you want, update it etc. – Utku Özdemir Mar 05 '16 at 11:57
  • @nbro and JDBC fits to your backend (server-side) since only it will communicate with the database, it'll act like a bridge between the client (desktop) app and the database. So just add JDBC library to your backend project dependencies, and start using it in your code. – Utku Özdemir Mar 05 '16 at 12:00
  • So, it's still not clear why we need MySQL workbench, apart from the fact that we can create schemas for databases, ER diagrams for a certain schema and generate the tables (SQL code) automatically for us. So, would this tool only be useful to somehow create the initial database and tables for the project? JDBC would then be a API that allows us to query (select, insert, etc) stuff from the databases from the Java code depending on the request? – nbro Mar 05 '16 at 12:05
  • @nbro exactly. You can use workbench to create your database and tables etc, modify them, you can also use it for some manual operations, and for debugging. Simply, you will interact with MySQL Workbench, your code with interact with JDBC API. – Utku Özdemir Mar 05 '16 at 12:08
  • Ok. Sorry for the so many questions. So, I would need to work both with JDBC and Spring on the server-side. So, I suppose JDBC can be easily integrated in the Spring controllers, etc, to query stuff from the database, right? Maybe Spring already comes with JDBC? Another thing, do you think that our overall approach is the right one, e.g. using Spring is a good idea if we need to develop an app which we don't know exactly if in the future we're going to need to develop a front-end also for mobile and web? – nbro Mar 05 '16 at 12:14
  • @nbro yes, in my opinion Spring is a good choice on the backend, but it has a steep learning curve and hard to configure right at the beginning. But when you configure & get it running, it's pretty good. I think there's no problem with your general approach, but need to learn a bit more about the technologies you'll use. And Spring has multiple modules, and it even provides some APIs to even facilitate and abstract away JDBC. `JdbcTemplate` and `JPA` are two of them. You can check them out... – Utku Özdemir Mar 05 '16 at 12:19
  • Last question, then I stop (sorry). We need to use Maven as our building tool (we don't know almost nothing about it). We decided to use Eclipse as our IDE. I downloaded Spring from the Marketplace and I think I had already Maven installed. Now, should I first create a Maven project, right? And then somehow I need to integrate Spring in this project, maybe by writing to the dependencies? Any suggestions of tutorials for this purpose? – nbro Mar 05 '16 at 12:30
  • @nbro no problems. You are right, you can just create an empty maven project, and start configuring it by adding spring dependencies (to your `pom.xml`) etc. You can follow tutorials such as this one: http://javahash.com/spring-4-mvc-hello-world-tutorial-full-example/ . But there's new another way: you go to Spring Initializr (http://start.spring.io), choose the modules you want and let Initializr generate the base structure of your project, for maven. – Utku Özdemir Mar 05 '16 at 12:54