简体   繁体   中英

Redirect from Spring boot REST API to separated react frontend

I have two applications: Backend - Spring Boot 2 REST API and Frontend - React

In the backend application, one of my controllers receive a POST request from another application that I have no control over. From this controller I want to redirect the client to my react frontend application. Is it possible?

My controller has this method that will handle the redirect

@Controller
@RequestMapping(value="/api")
public class JumpController {

    @PostMapping(value = "/{redirect}")
    public String jump(@PathVariable("redirect") final String redirect) {
        return "redirect:http://localhost:8443/category/" + redirect;
    }
}

When I test the applications in debug mode in chrome the client is doing a GET request to http://localhost:8443/category/gallery . And I'm getting a 404 (Not Found) because of the GET request method.

How can I tell the client to visit the actual page at http://localhost:8443/category/gallery without doing a GET operation?

Unfortunately I cannot change behaviour of the other application. I must handle the POST request to my application.

An HTTP 301 or 302 redirect instructs the client to make a HTTP GET request to the indicated location.

This isn't specific to Java, Spring, React or anything, it's just what the HTTP client is expected to do when receiving a redirect response.

Perhaps you're wanting to proxy the client request to the http://localhost:8443/category/... endpoint, in which case you could setup a proxy server (like nginx), add an existing proxy servlet to your application, or you could manually try to proxy the request yourself.

Obviously manually proxying the request yourself would likely be the most work, as it would involve reading the request (headers + body) and then constructing a new request to your localhost:8443 service, handling piping any response (and errors) back to the client.

I would suggest you look at using a proxy server or servlet. A quick Google brings up a potential in HTTP-Proxy-Servlet

There's also at least on previous example on SO How to proxy HTTP requests in Spring MVC?

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM