Start the RESTful server from java code - java

I have created a RESTful web service (using jersey in Eclipse) with Postgres database access. Both are running on my local machine. I have tested the service via the Postman client application sending some requests and getting responses as JSON to ensure that my server responds appropriately, all works fine. Now I have created a GUI (client application) using Swing for the management of the server (someone sits in front of it and manages the DB content). In addition, I want to do the following:
Start (not call) the RESTful web service from GUI (the person starts the GUI application to do his work, and with that the server starts, too, automatically). How can I do this is there any method I have to call?.
To query the server I have found these two methods:
RESTful Java client with Apache HttpClient and
RESTful Java client with Jersey client. Which of them is the better one?.
The overall structure of my communication processes using an Android client (in future) will be the following: Java client with GUI -- JSON --> RESTful web server <-- Android client
Use case: An Android client sends some data (text, pictures) in JSON format to the RESTful service which saves them in the Postgres DB. The Java client reads this data and displays them onto the GUI.

Mike, You will have to host your service on some server. Build a WAR file and deploy it on to a hosting server. Then you can call your API with the designated IP or masked name instead of localhost:8080 which I presume you are using. Also, I would recommend using a Jersy client.

Related

Azure Service Fabric Java SSL/HTTPS server

I have been looking into the Azure Service Fabric development and I have successfully created some test HTTP Stateless-Service applications in Java. Now I'd like to use encrypted api server communication in form of HTTPS but I cannot find any information how such server could be set up in Service Fabric environment.
In my HTTP applications I have been using com.sun.net.httpserver.HttpServer class and when I checked into the HttpsServer class I could not figure out how I could get the certificate saved into the application so that it could be used with the HttpsServer class. All the example applications using HttpsServer seem to read the certificate file from disk and that is not possible in ServiceFabric environment as far as I know.
So what is the proper way to set up HTTPS server in Java Service Fabric Stateless-Service application?
You can deploy certificates to cluster nodes using the ARM template.
Alternatively:
You can also use dotnet core and Kestrel for this. Here are some links to get started.
Github
Channel9 Video
Blog
You can also choose to run a web server like NodeJS or Flask as a container on Service Fabric.

Communication between Client and RESTful web services

I have a RESTful web Service that provide function of returning some data whenever a client send GET requests to ask for it:
#GET
#Path("/{deviceId}")
#Produces(MediaType.TEXT_PLAIN)
public String getDataResource(#PathParam("deviceId") long id){
return dataService.getData(id);
}
And the flow for this case would be the client sends request -> the web service returns value. But I want to ask that is it possible that the web service will automatically send response to the client when ever it has new data change inside of it? That means it not need to wait for the client to send request to ask for it. Because I would like to establish a communication between a client and some services running on an Application Server so that the client can always receives the newest data from the Application Server, so I think RESTful web Service can be a solution for it. And in oder to be ensure that the newest data will be transfered to the client side, so the server has to send to the client, not wait for the client to ask for it. Is RESTful web service provides any function like this?
Thank you all!
Is RESTful web service provides any function like this?
No. Not in the context you're asking for.
As answered before, the client could periodically poll for updates on the server. This is usually common option.
Another option - the original server posting updated on the "client". The client then becomes server itself. Viable, if you can expose services on the "client" side.
Maybe what are you looking for are web sockets. It is a long-lasting connection from client, where the server could keep returning data as they come.
There are some books around but you could search the net for more resources depending on the framework you use
You can implement notification system(observer pattern), so that client will poll the server in certain interval and any state change, it can get the result.
You may use the Schedulers to push the data to the client in a certain intervals.

Receiving a differnet HttpServletRequest from the same client?

I am currently trying Java Jersey 2.23.2 for my restful service
When both of the web client and tomcat web service runs on the same device, The session was handled perfectly.
However, when the an external client (another website) that runs on a different machine, there will always be new session introduced after an API call instead of sticking to just one session.
Is there anyway I could let the external client to call the webservice just like the local client does?
I can assume that your remote client does not care about JSESSIONID cookie. When session created, server sends HTTP header Set-Cookie with it.
Client must take it and then send it back with each subsequent requests.
All browsers do it automatically.
But as long as your client is another service it needs to care about it.

how to create a Server in PHP and client in JAVA

I like to implement server as backend server service and front as a regular website using php..Is it possible to create like this.Also how can integrate this by using a java swing application as client (client is on localhost)?
You may use web services. So you can create server in php and client in java.

Consuming a WCF service in a Java Client using wsHttpBinding

I'm trying to get a Java Client to communicate with a WCF wshttpbinding WebService. But I've been unsucesful so far. The call either hangs, or I get: “SOAPFaultException: The message could not be processed. This is most likely because the action 'http://tempuri.org/ISampleService/GetServiceName' is incorrect or because the message contains an invalid or expired security context token or because there is a mismatch between bindings. The security context token would be invalid if the service aborted the channel due to inactivity. To prevent the service from aborting idle sessions prematurely increase the Receive timeout on the service endpoint's binding.”
My Web Service is just the default Visual Studio 2010 generated "WCF Service Library Template".
My java client is generated in netBeans 7.0.1 and stubs have been generated using new Other --> Web Services --> Web Service Client and I’m referencing a local WSDL.
I've tried adding WebServiceFeature wsAddressing = new AddressingFeature(true); in the stub generated but it just throws the exception above.
I see other people with similar problems; however, I don't see any true resolution. Any suggestions would be greatly appreciated.
It sounds like you have a soap formatting issue. The java client isn't generating soap XML that makes sense to the WCF service. To get an idea of what the soap XML the service is expecting use the WcfTestClient command line app to call the service. This app dynamically creates a service proxy in a WinForm app. In the app, call the service operation and click the XML tab (next to the Formatted tab at the bottom of the right pane). You'll see both the request & response soap in this tab.
Next, configure the WCF service for message tracing and call it from the java client to see the soap XML it is being sent. Now you can compare the two soap messages to see what is different. The java client will need be configured to generate the soap format the WCF is expecting.
The WCF team recently released some WCF interop bindings specifically for java. These may not apply to your specific situation but they're probably worth reviewing.

Categories