我的控制器方法如下所示:
public void doLogin(HttpServletRequest request, HttpServletResponse response) throws IOException {
我想要这样做
ResponseEntity<String> responseEntity = restTemplate.postForEntity(testPrefix + "/login", map, String.class);
response = responseEntity;
或类似的,基本上做一个restcall并返回HttpReponseEntity作为它的enitirety的响应
答案 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将负责使用ResponseEntity
将HTTPMessageConverter
编组到HTML响应中。