Spring RestTemplate [removed]

后端 未结 1 470
北海茫月
北海茫月 2020-12-28 21:27

I\'m trying to use spring rest template to do a post request to login in.

RestTemplate restTemplate = new RestTemplate();

HttpHeaders headers = new HttpHead         


        
相关标签:
1条回答
  • 2020-12-28 21:59

    The redirection is followed automatically if the request is a GET request (see this answer). To make it happen on POST requests, one option might be to use a different request factory, like HttpComponentsClientHttpRequestFactory, and set it to use an HttpClient with the required settings to follow the redirect (see 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);
    

    I haven't tested, but this should work.

    0 讨论(0)
提交回复
热议问题