Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to redirect Spring request to another application?

I have an application that entertains a POST request. To request, I need to define some headers, boundaries and hyphens. In other words, I need to craft the complete request. Which I did successfully using HttpURLConnection. Now, I want to request the application from my Spring application.

Say, I have three applications A(sensor), B(spring) and C(server).

In this case, B will act as a bridge which receives the request from A authenticate it and send it to the C.

I don't want to craft the same request again in Spring it just redirect the request to the server. Is there any mechanism in Spring for this?

like image 600
Root Avatar asked Apr 26 '16 06:04

Root


People also ask

How do I forward a spring request?

A request can be basically processed in three ways: a) resolved by Spring in a controller action, b) forwarded to a different controller action, c) redirected to client to fetch another URL. Forward: performed internally by Spring. the browser is completely unaware of forward, so its original URL remains intact.

How do I redirect to another page in spring boot?

Try a URL http://localhost:8080/HelloWeb/index and you should see the following result if everything is fine with your Spring Web Application. Click the "Redirect Page" button to submit the form and to get the final redirected page.

How we can redirect to another page or controller in Spring MVC?

We can use a name such as a redirect: http://localhost:8080/spring-redirect-and-forward/redirectedUrl if we need to redirect to an absolute URL.

How do I redirect a spring rest controller?

@RestController public class RedirectController { @RequestMapping("/redirect") public String redirect() { return "redirect:/other/controller/"; } } and if we will try to access that url curl localhost:8080/redirect we will simply see redirect:/other/controller/ string as result.


1 Answers

There are 3 mechanismes of redirection:

  • forward: you pass the request (and associated response) to another servlet in the same context, meaning in the same web application - AFAIK that's not what you need
  • redirect: you give the client (browser) the location (an URL) where it should re-send a request. You can redirect to any URL, but it supposes that the redirected app will directly accept the client request - as you said B acts as a bridge, I think it is still not what you need
  • proxy: the bridge sends a new request to next hop and will resend the response to its own client. As there is no coupling between the original request and the new one, you can even use a different protocol, for example pipes or a Unix socket if you are on same server. Or you could use Json that is simple to produce and decode in HTML requests and responses. In Windows world, you could even use DCOM or .NET to directly execute methods on the remote server. But even if it is hidden by those tools, you still have to build a new request on the bridge and decode it on the server (except with DCOM/.NET on same server)
like image 160
Serge Ballesta Avatar answered Nov 15 '22 01:11

Serge Ballesta