我有一个要求,使用Spring MVC重定向到一个外部服务,使用POST来构建一个对象。Spring MVC重定向与来自HTTP POST的响应
从阅读this previous question据我所知,通过Spring MVC重定向是不可能的,所以需要通过Java代码中的HTTP客户端完成POST请求,我可以通过一个例子一个发现here。
我需要能够将浏览器重定向到POST请求的响应,但我不知道如何使用Spring MVC。
控制器
@RequestMapping(method = RequestMethod.GET, value = "/handoff")
public HttpEntity dispatch(HttpServletRequest request,
@RequestParam(value = "referralURL", required = false) String referralUrl) {
String redirectURL = "http://desinationURL.com/post";
HttpClientHelper httpHelper = new HttpClientHelper();
HttpEntity entity = httpHelper.httpClientPost(redirectURL, null);
// Redirect
return entity;
}
HttpClientHelper.java
public HttpEntity httpClientPost(String url, List<NameValuePair> params) throws ClientProtocolException, IOException {
HttpClient httpclient = HttpClients.createDefault();
HttpPost httppost = new HttpPost(url);
// Request parameters and other properties.
if (params != null) {
httppost.setEntity(new UrlEncodedFormEntity(params, Constants.UTF_8_ENCODING));
}
// Execute and get the response.
HttpResponse response = httpclient.execute(httppost);
HttpEntity entity = response.getEntity();
return entity;
}
我可以看到,目标服务是由POST请求命中成功,我需要读取响应作为返回的输出因为它现在刚刚返回一个404.
我已经包括HttpServletResponse的并置和首部值的建议,但它不重定向,还是返回404我是否需要改变方法返回响应而不是HttpEntity? –