springboot 2.x RestTemplate配置config

代码配置如下,可解决中文乱码问题


import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.http.client.ClientHttpRequestFactory;
import org.springframework.http.client.SimpleClientHttpRequestFactory;
import org.springframework.http.converter.StringHttpMessageConverter;
import org.springframework.web.client.RestTemplate;

import java.nio.charset.Charset;

/**
 * @program: micro-cloud
 * @description: RestTemplate配置
 * @author: PeterW
 * @create: 2020-12-10
 */

@Configuration
public class RestConfig {

    /**
     * 创建HTTP客户端工厂
     *
     * @throws Exception
     */
    @Bean(name = "clientHttpRequestFactory")
    public ClientHttpRequestFactory clientHttpRequestFactory() throws Exception {
        SimpleClientHttpRequestFactory factory = new SimpleClientHttpRequestFactory();
        // 数据读取超时时间,即SocketTimeout
        factory.setReadTimeout(180000);
        // 连接超时
        factory.setConnectTimeout(5000);
        return factory;
    }

    /**
     * 初始化RestTemplate,并加入spring的Bean工厂,由spring统一管理
     */
    @Bean(name = "restTemplate")
    public RestTemplate restTemplate(ClientHttpRequestFactory clientHttpRequestFactory) {
        RestTemplate restTemplate =  new RestTemplate(clientHttpRequestFactory);
        restTemplate.getMessageConverters().set(1, new StringHttpMessageConverter(Charset.forName("UTF-8")));
        return restTemplate;
    }

}

 

 

在 Spring Boot 3.x 中继承 OAuth 客户端模式,你可以按照以下步骤进行操作: 1. 首先,确保你已经在你的 Spring Boot 3.x 项目中添加了 Spring Security 和 OAuth2 的依赖。 2. 创建一个配置类,用于配置 OAuth2 的客户端信息。你可以在该类中配置客户端的 ID、密钥、授权类型等信息。示例代码如下: ```java @Configuration @EnableOAuth2Client public class OAuth2ClientConfig { @Value("${oauth2.client.client-id}") private String clientId; @Value("${oauth2.client.client-secret}") private String clientSecret; @Value("${oauth2.client.access-token-uri}") private String accessTokenUri; @Value("${oauth2.client.grant-type}") private String grantType; @Bean public OAuth2ProtectedResourceDetails oauth2ClientCredentials() { ClientCredentialsResourceDetails details = new ClientCredentialsResourceDetails(); details.setClientId(clientId); details.setClientSecret(clientSecret); details.setAccessTokenUri(accessTokenUri); details.setGrantType(grantType); return details; } @Bean public OAuth2RestTemplate oauth2RestTemplate(OAuth2ProtectedResourceDetails resourceDetails) { return new OAuth2RestTemplate(resourceDetails); } } ``` 3. 配置 OAuth2 的授权服务器信息。在 application.properties 或 application.yml 文件中添加以下属性: ``` oauth2.client.client-id=your-client-id oauth2.client.client-secret=your-client-secret oauth2.client.access-token-uri=your-access-token-uri oauth2.client.grant-type=client_credentials ``` 确保将上述属性值替换为你实际的授权服务器信息。 4. 创建一个服务类,用于调用受保护资源。你可以使用 `OAuth2RestTemplate` 类来发送 HTTP 请求并获取受保护资源的响应。示例代码如下: ```java @Service public class ProtectedResourceService { @Autowired private OAuth2RestTemplate restTemplate; public String getProtectedResource() { ResponseEntity<String> response = restTemplate.exchange( "https://api.example.com/resource", HttpMethod.GET, null, String.class); return response.getBody(); } } ``` 在上述代码中,`https://api.example.com/resource` 是你想要获取的受保护资源的 URL。 现在,你可以在其他组件中注入 `ProtectedResourceService` 并调用 `getProtectedResource()` 方法来获取受保护资源了。 请注意,以上代码仅为示例,你需要根据你的实际需求进行相应的配置和修改。希望这能帮助到你!如果有任何疑问,请随时提问。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值