I need to post the same request to a different webhook URL what I am getting in my spring controller so that the same operation can be performed on the both server. I am trying to do this for some integration purpose.
Please help.
Thanks,
Vidyakar Sharma.
A browser submit usually goes only to one server. You could write a java script that does an ajax call to the second server.
A second option could be that the first server calls you second server, so your spring controller creates the second http request.
Related
I have a Java JAX-RS REST service which has many endpoints, let's say:
...
POST /api/ops/create
GET /api/items
...
Both of them served under same war file, so if I want to reach them from a client I am using following URLs:
POST http://server.domain.com:8080/the_war_file/api/ops/create
POST http://server.domain.com:8080/the_war_file/api/items
Well, the question begins here. I want to access the GET endpoint from the code behind the POST endpoint.
In other words, I want to send a GET request when I am operating the POST request.
Of course, I can send the request to the full URL with server, port and name of the war file.
However, it seems wrong, because I am already in the war file.
Or, I know I can call the method of the other endpoint directly. Is it rational?
So, are there any efficient methods/solutions to call different endpoint in the same war file? (Like routing etc.)
I know I can call the method of the other endpoint directly. Is it
rational?
Yes, this is perfectly fine where in your code behind one request you will call a method of some class that is used for serving some other request. There is no need to send extra HTTP request unless you have a specific need for invocation of that method to be triggered by HTTP request
I want to implement such logic: Client sends request to a server that has single controller. That server parses url and according to endpoints chooses another server to process the request. And the second server sends response back the same way.
I want to use Spring Controllers (MVC). I also can have several tomcat servers.
So: Client -> Main Controller that can determine the necessary server with micro services included -> the end server that process request, goes Database etc. -> Main Controller -> Client.
How to make tomcat knows about another tomcat?
How to make tomcat knows about another tomcat?
If that url(url containing another server details) coming from client, then you need to extract it from request and create another httpRequest and send it to another server(it will be server to server server1>server2) or initiate the redirect request(it will be server1>client>server2) depending upon the requirement.
See example here to send server to server http request
I also can have several tomcat servers.
You can also consider having multiple webapp with in same tomcat but that depend upon your requirements. Difference will be in this case there will be single jvm process . with multiple tomcat there will be as many number of jvm process.
Is there any way to achieve multiple responses from a single request using Spring Boot ? If yes, please provide me a link.
HTTP does not work this way. An HTTP request has exactly one response. Spring Boot will not let you send more than one response, because that would be against the HTTP specification and no HTTP clients would be able to figure out what was going on.
There may be another way to accomplish your underlying goals, but whatever it is, it won't involve sending more than one HTTP response to a single HTTP request.
I am working on a project that uses Spring MVC, and one of the requirement involves sending request to an external services to handle some transactions.
The problem now is the external services only takes POST method, and I've looked everywhere and could not find a way to forward user to external site as a POST request(similar to form submit).
What we are trying to do:
1) My Controller will receives request and execute some backend thing and construct some parameters to pass on.
2) *Send request to external service via HTTP POST method with some parameters.
(note. User's browser will show URL of the external site.)
I've looked into different return types for Spring MVC and could not find anything that fits.
Any advice would be greatly appreciated.
Thanks!
You can use the Apache HttpComponents library to send HTTP requests from you controller. It's easy to use, and there's plenty of documentation and examples.
Supposed that this is not an issue related the Spring MVC. That's the same solution Which you try to implement such operations in Servlet I think.
First of all, this external site should be an async service and you neednt get the response from it, right? If yes, i think the simplest solution is that you can new URLConnection within your Servlet or Controller, construct the params and set the POST method, and then send out the request. After that, you can response to your local page as you like.
The second solution, a dummy JSP page is needed. The page will post a form to external service and redirect to the target page as you needed. No matter you use a javascript or not, I think the user will see a refresh on the browser.
You won't be able to redirect as that implies a GET.
Since your client is using a web browser, it should be possible to return an HTML document with a form, and then to submit that form (with a method attribute of POST) when the page has loaded.
My application neeeds to access a third part web service.
Of late, they have introduced a load balancer, which redirects to the server.
Because of this the webservice gets a 302 - Redirect error as response.
In the SOAPUI, I was able to enable a property called "Follow Redirect", and because of this service followed the redirect and served by the server.
Now is there a similar propety that can be turned on in the code, which would make the webservice follow the request?
(The calling code is java and the webservice is in .net)
This needs to be supported by the library implementing the actual call to the web service, and there is not a generic way of setting this.