首页 > 其他分享 >Spring RestTemplate redirect 302

Spring RestTemplate redirect 302

时间:2024-01-23 11:58:32浏览次数:25  
标签:redirect 重定向 Spring 302 RestTemplate new final

 

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.
https://stackoverflow.com/questions/32392634/spring-resttemplate-redirect-302

Spring的RestTemplate自动重定向,如何拿到重定向后的地址?

第一种解决方案

既然知道了spring会设置自动重定向,那么要禁止自动重定向就很简单了。我们写一个类来继承SimpleClientHttpRequestFactory,然后复写prepareConnection方法,把该属性设置为false即可,代码如下:

public class NoRedirectSimpleClientHttpRequestFactory extends SimpleClientHttpRequestFactory {

    @Override
    protected void prepareConnection(HttpURLConnection connection, String httpMethod) throws IOException {
        super.prepareConnection(connection, httpMethod);
        connection.setInstanceFollowRedirects(false);
    }
}

那么该如何在创建的时候使用这个ClientHttpRequestFactory 呢,一种简单的方法直接在创建RestTemplate的时候传入参数即可

    private static final RestTemplate REST_TEMPLATE = new RestTemplate(new NoRedirectSimpleClientHttpRequestFactory());

这样的话就返回的结果就是302了,可以在header中的"Location"拿到重定向的地址,再使用另外的RestTemplate来请求下载就可以了。但是spring既然提供了功能强大的RestTemplateBuilder,我们就要学会使用它,如下,可以顺便设置下超时时间之类的属性:

    private static final RestTemplate REST_TEMPLATE = new RestTemplateBuilder()
            .requestFactory(NoRedirectSimpleClientHttpRequestFactory.class)
            .setConnectTimeout(Duration.ofMillis(3000))
            .setConnectTimeout(Duration.ofMillis(5000))
            .build();

https://blog.csdn.net/m0_37657841/article/details/107391699

 

 

feign 怎么处理302的响应?【测试未通过,copy过来留一个思路】

1.如果直接使用Feign,可以通过修改Options来实现是否自动跳转,默认是true,也就是自动重定向。

Request.Options customerOptions = new Request.Options(10_000, 20_000, false);
Feign.builder().options(customerOptions).build();

2.如果使用Spring的open-feign,则可以通过修改配置文件来实现。

feign:
  httpclient:
    follow-redirects: false

https://segmentfault.com/q/1010000014239099

 

 https://wenku.csdn.net/answer/e54f3d1b7a9a4a40a04ee4150811ff8b

 

标签:redirect,重定向,Spring,302,RestTemplate,new,final
From: https://www.cnblogs.com/softidea/p/17981991

相关文章

  • 关于springboot 域认证
    最近项目,客户要求实现域认证,然后登录。网上资料自己整理一下,以备后续使用;springboot域认证,我采用的是ldap方式认证。1.引入插件:<dependency><groupId>org.springframework.boot</groupId><artifactId>spring-boot-starter-data-ldap</artifactId......
  • shardingsphere springboot application.yml配置
    shardingsphere springbootapplication.yml配置 spring:sharding-sphere:datasource:names:mastermaster:type:com.zaxxer.hikari.HikariDataSourcedriver-class-name:com.mysql.cj.jdbc.Driverjdbc-url:jdbc:mysql:......
  • springboot+mybtais+mysql
    一、通过maven引入相应的包pom.xml<?xmlversion="1.0"encoding="UTF-8"?><projectxmlns="http://maven.apache.org/POM/4.0.0"xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"xsi:schemaLocation="http......
  • Springboot学习第二天
    今天的学习内容是如何在项目中设计统一响应接口返回值,达到统一的格式1.响应实体我们首先要定义一个公共的接口响应实体,以后所有的接口返回值,都是返回的这个公共响应实体。这样做的好处是可以统一返回值的风格,编译接口的维护。需要包含3个关键的成员变量:状态码返回信息数......
  • SpringMVC - 谈谈你对SpringMVC的理解
     谈谈你对SpringMVC的理解?普通人:SpringMVC它是一个MVC框架吧,就是,我们可以使用SpringMVC来开发Web应用...呃它是基于Servlet上的一个扩展,就是它里面我记得好像有一个核心控制器,叫DispatcherServlet,然后扩展了之后,就是所有请求都会经过那个...DispatcherServlet然后再做一......
  • 在 SpringBoot 项目中使用 Mybatis 打印 SQL 日志
    前言我们在项目中使用的持久层框架大部分都是mybatis,如果在日志中能打印sql的话,对于我们排查问题会更加方便。第一种方式:修改mybatis配置修改配置mybatis:configuration:log-impl:org.apache.ibatis.logging.slf4j.Slf4jImpllogging:level:com.imooc.p......
  • spring自动装配的原理解析
    前言学习SpringBoot,绝对避不开自动装配这个概念,这也是SpringBoot的关键之一本人也是SpringBoot的初学者,下面的一些总结都是结合个人理解和实践得出的,如果有错误或者疏漏,请一定一定一定(不是欢迎,是一定)帮我指出,在评论区回复即可,一起学习!篇幅较长,希望你可以有耐心.如果只关心SpringBoo......
  • Spring Cloud 系列:Seata 中TCC模式具体实现
    概述https://seata.io/zh-cn/docs/dev/mode/tcc-modehttps://seata.io/zh-cn/docs/user/mode/tccTCC模式与AT模式非常相似,每阶段都是独立事务,不同的是TCC通过人工编码来实现数据恢复。需要实现三个方法:Try:资源的检测和预留;Confirm:完成资源操作业务;要求Try成功Confirm一......
  • springboot+vue--注册
    ***在UserController中声明一个(/register),接口中包括两个功能://用户名是否已被占用//注册**​publicResultregister(Stringusername,Stringpassword){}***在UserService(接口)中,实现两个方法:**​publicUserfindByUsername(Stringusername){}//根据用户......
  • Java21 + SpringBoot3集成easy-captcha实现验证码显示和登录校验
    目录前言相关技术简介easy-captcha实现步骤引入maven依赖定义实体类定义登录服务类定义登录控制器前端登录页面实现测试和验证总结附录使用Session缓存验证码前端登录页面实现代码前言近日心血来潮想做一个开源项目,目标是做一款可以适配多端、功能完备的模板工程,包含后台管理系......