简体   繁体   English

如何从客户端桌面应用程序向服务器端Spring应用程序发送请求?

[英]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. 我们没有使用Java创建这些应用程序的经验,但是我们已经开始考虑将要使用的工具。

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. 例如,我们决定将Spring作为框架来接收请求,处理请求,最终从数据库(MySQL)中获取数据,然后返回响应。

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. 我不确定这些响应(以及来自客户端应用程序的请求)的格式是什么,但是应该与客户端尽可能地独立,以便客户改变意见并决定他/她想要一个Web或一个移动应用程序,我们不需要更改处理所有请求的服务器端代码。

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. 通过查看我们其中一位已经在该领域有经验的人提供的项目源代码,显然他已经使用HttpServletRequestHttpServletResponse对象作为控制器所有方法的参数。 I think it's a server-side Spring app. 我认为这是服务器端的Spring应用程序。 In this case, specifically, it would be a controller class, which processes requests to a certain URL. 在这种情况下,具体地说,它将是一个控制器类,用于处理对某个URL的请求。

The code is easier to understand, because it's similar to how I had worked with Javascript, Node, Express and Mongo sometime ago. 该代码更易于理解,因为它类似于我之前使用Javascript,Node,Express和Mongo的方式。 It's all based on the MVC pattern. 这全部基于MVC模式。 In that environment, requests were made either by writing the URL on the browser or by using XMLHttpRequest (AJAX). 在那种环境中,通过在浏览器上写URL或使用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? 如果我们确实需要创建一个桌面应用程序(例如,使用Swing),我们将如何向Spring控制器发出请求?

  2. What's the best way to decouple the server-side code from the client-side code? 将服务器端代码与客户端代码分离的最佳方法是什么? Is JSON useful here? JSON在这里有用吗? 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? 我们是否应该在客户端上使用一个框架,该框架具有在必要时向Spring控制器发出请求的特定工作,还是足以以某种方式对请求进行硬编码? And how would this framework eventually deal with Swing? 这个框架最终将如何处理Swing?

  4. Where would JDBC fit here? JDBC在哪里适合? 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. 抱歉,这些问题很多,但是我们之前没有介绍过这些环境,因此我们迷失了方向。

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. 如果要在服务器端使用Spring MVC和REST控制器,则需要一个REST客户端库,该库会将HTTP请求发送到后端。 It doesn't necessarily need to be connected to Spring in any way. 它不一定需要以任何方式连接到Spring。

  1. You can check the REST clients from this answer , and pick the one you like. 您可以从此答案中检查REST客户端,然后选择您喜欢的客户端。 UniRest might be a good choice for the desktop app. UniRest对于桌面应用程序可能是一个不错的选择。

  2. Decoupling is done by providing a Restful API, means any client which can send HTTP requests can use your backend services. 通过提供一个Restful API来实现解耦,这意味着任何可以发送HTTP请求的客户端都可以使用您的后端服务。 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). 通信是通过HTTP完成的,这是一个很好的解耦示例(例如,不同于RMI)。 And yes, JSON might be a good choice, most of the rest APIs use JSON as the data-interchange format. 是的,JSON可能是一个不错的选择,大多数其他API都使用JSON作为数据交换格式。 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) 但是您不需要,也可以使用XML或任何其他格式(但是我强烈建议您使用JSON和Jackson作为库)

  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. 您也可以在客户端应用程序上遵循MVC方法,但这取决于您使用的框架等。但是基本上,您需要的只是将请求发送到后端,您可以按照任何方式进行操作。 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. 您还可以将端点URL外部化为配置文件,以保持结构良好的结构。 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. 在Java中,JDBC仅用于连接数据库。 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. 既然您提到要使用MySQL,则需要在服务器端使用它。 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. 只需抓住MySQL JDBC驱动程序(如果您使用Maven从此处获取依赖项),它就是用于MySQL连接的JDBC API的实现。

And one last thing: instead of using the old &outdated Swing, consider using JavaFX. 最后一件事:不要使用旧的过时的Swing,而应考虑使用JavaFX。 It is the new preferred way of doing desktop app user interfaces in Java. 这是在Java中执行桌面应用程序用户界面的首选新方法。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 从客户端应用程序保护服务器端输出 - Secure server-side output from a client-side app 如何从服务器端(Google App Engine,Cloud Endpoints)向我的客户端发送信息? - How can I send from the server-side (Google App Engine, Cloud Endpoints) the information to my client? 使用GWT将Excel文件从服务器端发送到客户端 - Send excel file from server-side to client-side using GWT 从客户端将JSON解析回服务器端的JSONObject的问题 - Issues parsing JSON from Client-Side back to JSONObject on Server-side RMI:如何将服务器端和客户端类放置到网络可访问的位置? - RMI: How to place server-side & client-side classes to network-accessible location? 如何使客户端Web浏览器可以使用服务器端域模型? - How to make server-side domain models available to client-side web browser? 如何从NetBeans_RPC下的客户端连接到Spring.httpInvoker下的服务器端? - How to connect from client under NetBeans_RPC to server-side under Spring.httpInvoker? 如何在服务器端消费Google In-App购买 - How to consume Google In-App purchases Server-side 在JAVA和Servlet中客户端和服务器端之间进行数据库调用的安全方法? - Safe method for database calls between client-side and server-side in JAVA & Servlet? JSON(客户端)&lt;-&gt; Java(服务器端)&lt;-&gt; JSON(NoSQL DB) - JSON (client-side) <-> Java (server-side) <-> JSON (NoSQL DB)
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM