0

I am trying to share data from one server(8081) to another(8082) in Spring Boot using ResponseEntity, but I am not able to built the body.

Here is the code which I have written,

Server 1-

    @GetMapping("/redirect")
    public ResponseEntity<Void> redirectPgServer(@RequestParam("txnId") String txnId, 
    @RequestParam("amount") String amount) {

    // Redirect to server 2
    ProductDetails ref=new ProductDetails();
    ref.setTxnId("txnId");
    ref.setAmount("amount);
    HttpHeaders head = new HttpHeaders();
    String url = "http://localhost:8082/redirect-server";                                                                                                       
    System.out.println(url);
    head.setLocation(URI.create(url));
    return new ResponseEntity<Void>(ref, head, HttpStatus.FOUND);
}

Server2-

 @GetMapping(value = "/redirect-server")
 public String validateData(@RequestBody ProductDetails ref)
{
    //Server 2 receive data
      System.out.println(ref.getAmount());  

 //Does not gives any output since it throws error Request Body Missing, also if I mention 
  post mapping above it throws error Request method 'GET' not supported]

     return "index";   //Using Thymeleaf for displaying html page.
}   

I am able to share the data using only header by manipulating the url and using PathVariable but I want to use Post Mapping as I want to hide the parameters in the Url which are visible to User at Browser. Also I have tried using RestTemplate which brings me back to Server 1 and it throws error- Spring rest template cannot convert from text/html utf8.

  • This question is incomplete. There is no communication going on here. Just one defined endpoint, and another endpoint that doesn't seem to do anything. The missing piece is likely where you're having problems. – Christopher Schneider Aug 10 '21 at 16:00
  • Most likely you need to change `ResponseEntity` to `ResponseEntity` – Ivan Aug 10 '21 at 17:40

1 Answers1

0

My suggestion is to annotate both methods with @PostMapping, in both remove just the @RequestBody Annotation. In the first server you should respond with a 307 HTTP Code (which is the temporary redirect) basically this tells to the browser, to retry the call to another endpoint(which is given into the Location header).

Something like this :

Server 1

 @PostMapping(value = "/redirect", consumes = MediaType.APPLICATION_FORM_URLENCODED_VALUE)
    public ResponseEntity redirect(Product product) {
        HttpHeaders httpHeaders = new HttpHeaders();
        httpHeaders.setLocation(URI.create("http://localhost:8082/product"));
        return new ResponseEntity(httpHeaders, HttpStatus.TEMPORARY_REDIRECT);
    }

Server 2

@PostMapping(value = "/product", consumes = MediaType.APPLICATION_FORM_URLENCODED_VALUE)
public void redirect(Product prod) {
    System.out.println(prod.getId());
    //do your stuff
}

HTML Page

<html>
    <body>
        <form action="http://localhost:8081/redirect" method="POST">
            <input name="id">
            <input name="name">
            <button type="submit">Send</button>
         </form>
    </body>
</html>

If you don't use a form, instead you use some kind of async call you MUST specify the url form encoding. Otherwise you will get a 405.

I've tried the code with postman and with a html page it does work correctly. (Tried with Firefox)

If something it's not clear don't esitate to ask.

I have found a similar question where i took inspiration from A good way to redirect with a POST request?

xTheDoctah
  • 276
  • 1
  • 11