Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Converting a responseEntity to httpServletResponse with spring

My controller method looks like this :

public void doLogin(HttpServletRequest request, HttpServletResponse response) throws IOException {

and I want to do this

ResponseEntity<String> responseEntity = restTemplate.postForEntity(testPrefix + "/login", map, String.class);

response = responseEntity;

or similar, basically make a restcall and return the HttpReponseEntity as the response n its enitirety

like image 386
NimChimpsky Avatar asked Jun 25 '26 09:06

NimChimpsky


1 Answers

From updated comments I assume that you are wanting to return the result of the restTemplate.postForEntity() call from your Controller.

As shown by the Spring MVC documentation, ResponseEntity is a valid return type from a Controller method. So you can simply return the result of your restTemplate.postForEntity() call from the doLogin() method. As an example:

@Controller
public class MyController
{
     @AutoWired
     private RestTemplate restTemplate;

     @RequestMapping("/yourPath")
     public ResponseEntity<String> doLogin(HttpServletRequest request) throws IOException
     {
          return restTemplate.postForEntity(testPrefix + "/login", map, String.class);
     }
}

Spring MVC will take care of marshalling the ResponseEntity into the HTML response using a HTTPMessageConverter.

like image 69
Rob Blake Avatar answered Jun 27 '26 01:06

Rob Blake



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!