There is a hosting for the site from reg.ru , managed by ISP Manager 's, CentOS 6.8 is installed. How, using Java , can I handle JSON requests that come to the server? That is, directly to its IP address on a specific port, or on some page. I do not really know how to approach this issue.


More details :
1. Server side:
I have a server on Linux, I need to teach him to handle incoming requests. On php, everything is done very simply (we specify the address for receiving requests, then file_get_contents('php://input') and json_decode($json) ), I know that, but I need to implement it in Java . How to approach this, I do not know. I asked a similar question some time ago, which I deleted later, but there was a slightly different point.

2. As for the requests themselves:
I work with VK API, that is, every time there are updates in the community, new messages or something else, VK will send POST requests to the address that I will indicate:
enter image description here
I’ll probably be able to specify the IP: PORT address, but it would be better if I could process requests sent, for example, to the address of the same php file.

I would be grateful for any help. I do not know exactly how to solve this issue, so I decided to ask. If the only option is to write the server and listen on the ports, then I will dig in this direction. Then the question after this is: is Java cross platform enough for me to write the above application on a poppy, and does it work quietly under Linux?

  • "IP address type: PORT I can probably specify . " Unfortunately, you can not: VC does not allow to specify the port in the address of the callback. - mymedia

1 answer 1

For a simple start, I can offer these libraries:

This is a small framework for running a web server out of the box. If satisfied with the default settings, it will be sufficient to specify only the methods for processing requests.

Here is an example of a RestAPI application.

 public class HelloWorld { public static void main(String[] args) { get("/hello", (req, res) -> { return "Hello World"; }); } } 

This library is indicated in the examples of the previous framework and is probably better to use it.

 // Serialization Gson gson = new Gson(); gson.toJson(1); // ==> 1 gson.toJson("abcd"); // ==> "abcd" gson.toJson(new Long(10)); // ==> 10 int[] values = { 1 }; gson.toJson(values); // ==> [1] // Deserialization int one = gson.fromJson("1", int.class); Integer one = gson.fromJson("1", Integer.class); Long one = gson.fromJson("1", Long.class); Boolean false = gson.fromJson("false", Boolean.class); String str = gson.fromJson("\"abc\"", String.class); String[] anotherStr = gson.fromJson("[\"abc\"]", String[].class); 
  • I apologize for the possibly stupid question: I am not familiar with Linux, and can the application work normally there? I can write it without any problems. And also, in the examples on this site I read, the same "/ hello" and "localhost: port / hello" is indicated there, and in the case of a dedicated server, what should I do? As I noticed above, I can not specify ip: port, I just need to specify the address. - Peter Samokhin
  • 1) The main goal of this language is to hide the differences between platforms to the maximum. Therefore, this should be the least disturbing. 2) If my memory serves me, port 80 is used for the web by default. Or you need to find out which port VK is accessing and specify it. (Spark.port (8080);) - Riĥard Brugekĥaim
  • that is, in VK I need to specify a link to the directory that I will create, for example server.ru/callback, and how can I specify this, localhost: 80 / callback? The server will listen to requests addressed to the directory or how? - Peter Samokhin
  • This is not php, folders are not affected here. In the example that I have specified, the first parameter is the internal address of the site (what comes after localhost: 80), where you can specify either an arbitrary or root "/". Added in response a link to the example with the RestAPI application on the pairing. - Riĥard Brugekĥaim