3

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

4
  • Can you please clarify. Are you saying that within the doLogin() method, you wish to call restTemplate.postForEntity() and then return that as the response from doLogin()? Commented Oct 23, 2013 at 11:35
  • An interesting question, but also my question is that let's say you have this converter from ResponseEntity to HttpServletResponse; what's the use case? Commented Oct 23, 2013 at 12:09
  • @nobeh don't ask, its not my decision. But I am returning ResponseEntity as the responsebody, seems to do the job Commented Oct 23, 2013 at 12:17
  • There is a use case when overriding certain spring handlers. For example when setting a custom exception handler in WebSecurityConfig with HttpSecurity.exceptionHandling().accessDeniedHandler(...) I have to override such a method. It would be nice to have some kind of translator for this instead of doing it manually. Commented Sep 1, 2017 at 9:59

1 Answer 1

2

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.

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.