I'm looking to implement a way of directly forwarding a POST request from one SpringBoot application to another. Forwarding GET requests has been easy, and the code below shows how I get a 405 error when I lazily try to re-use the same redirection pathway.
I have looked at existing examples on StackOverflow (most notably here) but I can't even get that solution to compile. Can anyone suggest amendments to the code below to allow me to just redirect the entire POST request through?
@RestController
public class Routing {
@Autowired
private RoutingDelegate routingDelegate;
@RequestMapping(value="/**", method=RequestMethod.GET, produces=MediaType.TEXT_PLAIN_VALUE)
public ResponseEntity<String> catchAll(HttpServletRequest request, HttpServletResponse response){
return routingDelegate.redirect(request, response);
}
@RequestMapping(value="/**", method=RequestMethod.POST, produces=MediaType.TEXT_PLAIN_VALUE)
public ResponseEntity<String> catchAllPost(HttpServletRequest request, HttpServletResponse response){
/**
* This returns a 405 error, which is understandable since I'm redirecting a POST request to a GET endpoint
*/
return routingDelegate.redirect(request, response);
}
}
@Service
public class RoutingDelegate {
private final String baseServerUrl = "http://localhost:8080";
public ResponseEntity<String> redirect(HttpServletRequest request, HttpServletResponse response){
try {
String queryString = request.getQueryString();
String redirectUrl = baseServerUrl + request.getRequestURI() +
(queryString != null ? "?" + queryString : "");
response.sendRedirect(redirectUrl);
}
catch (IOException e) {
return new ResponseEntity<String>("REDIRECT ERROR", HttpStatus.INTERNAL_SERVER_ERROR);
}
return new ResponseEntity<String>("", HttpStatus.OK);
}
}