使用spring将responseEntity转换为httpServletResponse

时间:2013-10-23 11:21:04

标签: spring rest spring-mvc

我的控制器方法如下所示:

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

我想要这样做

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

response = responseEntity;

或类似的,基本上做一个restcall并返回HttpReponseEntity作为它的enitirety的响应

1 个答案:

答案 0 :(得分:1)

从更新后的评论中我假设您想要从控制器返回restTemplate.postForEntity()来电的结果。

Spring MVC documentation所示,ResponseEntity是Controller方法的有效返回类型。因此,您只需从restTemplate.postForEntity()方法返回doLogin()来电的结果即可。举个例子:

@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将负责使用ResponseEntityHTTPMessageConverter编组到HTML响应中。