Follow 302 redirect using Spring restTemplate?

前端 未结 3 783
庸人自扰
庸人自扰 2020-12-06 16:19
  RestTemplate restTemplate = new RestTemplate();

  final MappingJackson2XmlHttpMessageConverter converter = new MappingJackson2XmlHttpMessageConverter();
  final L         


        
3条回答
  •  离开以前
    2020-12-06 16:51

    Using the default ClientHttpRequestFactory implementation - which is the SimpleClientHttpRequestFactory - the default behaviour is to follow the URL of the location header (for responses with status codes 3xx) - but only if the initial request was a GETrequest.

    Details can be found in this class - searching for the following method:

    protected void prepareConnection(HttpURLConnection connection, String httpMethod) throws IOException {
    
        ...
    
        if ("GET".equals(httpMethod)) {
            connection.setInstanceFollowRedirects(true);
        }
    

    Here the relevant doc comment of HttpURLConnection.setInstanceFollowRedirects method:

    Sets whether HTTP redirects (requests with response code 3xx) should be automatically followed by this {@code HttpURLConnection} instance.

    The default value comes from followRedirects, which defaults to true.

提交回复
热议问题