Spring RestTemplate重定向302

Ali*_*yon 8 rest redirect spring response http-status-code-302

我正在尝试使用spring rest模板来发布登录请求.

RestTemplate restTemplate = new RestTemplate();

HttpHeaders headers = new HttpHeaders();
headers.setContentType(MediaType.APPLICATION_FORM_URLENCODED);

LinkedMultiValueMap<String, Object> mvm = new LinkedMultiValueMap<String, Object>();
mvm.add("LoginForm_Login", "login");
mvm.add("LoginForm_Password", "password");

ResponseEntity<String> result = restTemplate.exchange(uriDWLogin, HttpMethod.POST, requestEntity, String.class);
Run Code Online (Sandbox Code Playgroud)

我的ResponseEntity状态是302,我想跟随此请求获取正文响应,因为我没有获得此请求的正文.

18:59:59.170 MAIN [http-nio-8080-exec-83] DEBUG c.d.s.c.DemandwareCtlr - loginToSandbox - StatusResponse - 302
18:59:59.170 MAIN [http-nio-8080-exec-83] DEBUG c.d.s.c.DemandwareCtlr - loginToSandbox - BodyResponse - 
Run Code Online (Sandbox Code Playgroud)

我该怎么做才能解决这个问题?

Nic*_*las 22

如果请求是GET请求,则会自动执行重定向(请参阅此答案).要在POST请求中实现,一个选项可能是使用不同的请求工厂,例如HttpComponentsClientHttpRequestFactory,并将其设置为使用HttpClient具有所需设置的a来跟随重定向(请参阅LaxRedirectStrategy):

final RestTemplate restTemplate = new RestTemplate();
final HttpComponentsClientHttpRequestFactory factory = new HttpComponentsClientHttpRequestFactory();
final HttpClient httpClient = HttpClientBuilder.create()
                                               .setRedirectStrategy(new LaxRedirectStrategy())
                                               .build();
factory.setHttpClient(httpClient);
restTemplate.setRequestFactory(factory);
Run Code Online (Sandbox Code Playgroud)

我没有测试,但这应该工作.