Multiple Response from Single Request using Spring Boot - java

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.

Related

How to check the connectivity of an endpoint before sending requests in spring boot

I am a bit new to spring boot.
I am asking if there is a way through which I can check if a particular endpoint is alive before sending any requests in spring boot.
Let's say I have an endpoint like 172.XX.XX.XX:1010/api/xxx/ and I want to check if that endpoint is alive before I send any request to it. Is there any way to that?
I am using spring boot.
You could use the spring boot actuator health check endpoint to check if your service is online, but in terms of checking if a particular endpoint is 'alive' you may as well just make the request as if the service is alive then you will be able to make the call to the endpoint
I don't think checking if an API endpoint exists before firing the actual call is beneficial. In the best case scenario, you will be making 2 api calls rather than one. Sure, for GET requests, you can fire a HEAD request first, but it doesn't seem to be worth it. Instead, you can have handlings in your client code based on the response code from the API (40x, 50x).

Intercept and filter HTTP request

I want to intercept/sniff incoming HTTP request and filter/modify their contents (before they reach the application).
"Fiddler" seems to have this functionality, but for the sake of integration and portability I would rather have some library in Java/C to do this. Like JPCAP, for example. It intercepts IP packets, but, as stated, I need to intercept the -higher level- HTTP requests.
Furthermore, how can SSL encrypted (HTTPS) requests be read/modified in the same way?
Thanks in advance.
Have you tried Servlet Filters?
They wrap the HTTP request and so can modify the request before it gets to the servlet, and can modify the response as well. They can (and are) used to wrap third-party servlets and JSPs.
Because they are in the servlet container, you have secure, unencrypted access to both the request and response.

Send HTTP POST request in Spring MVC

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.

It there any standard way to redirect SOAP request?

The problem is following:
Some client tries to access an SOAP webservce
Webservice implementation anylizes request and comes to conclusion that it is not able to proccess the request itself. However, the webservice knows what some additional webservice should be called instead of one.
???
Client calls "additional webservice" like he did at step 1
It there any specific standard or technique for this?
Use the proper HTTP status code for redirection. A status of 303 See Other comes to mind.

Request & response handling in java

Servlets are one way of handling requests/responses from a web client i.e. a client makes a request, server finds the appropriate servlet which can handle the request and generate the response which is then forwarded to the client.
Could someone please tell me ways of handling requests/responses other than Servlets. I know CGI is another option, what else can be used?
CGI is a poor choice for implementing HTTP protocol on the server-side. You can write the server manually (by opening a ServetSocket, parsing HTTP reauest headers and responding according to the spec, however this approach is very tedious and error-prone.
If you want to handle web client requests, servlets are basically the only reasonable choice. Of course there are tons of wrapping technologies and frameworks (web, SOAP, REST, AJAX...)

Categories