使用RestTemplate远程调用实战案例
程序员文章站
2022-03-02 17:17:25
...
1、pom.xml配置
<!--测试类依赖-->
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-test</artifactId>
<scope>test</scope>
</dependency>
<!--web功能的起步依赖-->
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
2、启动类
package com.module.assets;
import com.ctrip.framework.apollo.spring.boot.ApolloAutoConfiguration;
import com.haier.iot.paas.module.commons.http.OkHttpFactory;
import org.mybatis.spring.boot.autoconfigure.MybatisAutoConfiguration;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.boot.autoconfigure.security.servlet.UserDetailsServiceAutoConfiguration;
import org.springframework.context.annotation.Bean;
import org.springframework.http.client.OkHttp3ClientHttpRequestFactory;
import org.springframework.scheduling.annotation.EnableAsync;
import org.springframework.stereotype.Repository;
import org.springframework.web.client.RestTemplate;
import tk.mybatis.spring.annotation.MapperScan;
import java.util.concurrent.TimeUnit;
@EnableAsync
@SpringBootApplication(exclude = {MybatisAutoConfiguration.class, ApolloAutoConfiguration.class, UserDetailsServiceAutoConfiguration.class})
@MapperScan(basePackageClasses = {AssetsApplication.class}, annotationClass = Repository.class)
public class AssetsApplication {
public static void main(String[] args) {
SpringApplication.run(AssetsApplication.class, args);
}
@Bean
public RestTemplate nacosRestTemplate() {
return new RestTemplate(new OkHttp3ClientHttpRequestFactory(OkHttpFactory.createHttpsClientBuilder()
.readTimeout(60, TimeUnit.SECONDS)
.writeTimeout(60, TimeUnit.SECONDS)
.connectTimeout(60, TimeUnit.SECONDS)
.build()));
}
}
3、测试类
package com.module.assets;
import org.apache.commons.lang3.StringUtils;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.test.context.SpringBootTest;
import org.springframework.http.*;
import org.springframework.test.context.junit4.SpringJUnit4ClassRunner;
import org.springframework.util.LinkedMultiValueMap;
import org.springframework.util.MultiValueMap;
import org.springframework.web.client.RestTemplate;
@RunWith(value = SpringJUnit4ClassRunner.class)
@SpringBootTest(webEnvironment = SpringBootTest.WebEnvironment.RANDOM_PORT)
public class Tester {
@Autowired
private RestTemplate nacosRestTemplate;
@Test
public void password(){
//nacos发布接口
String urlConfig="http:192.168.110.1:8448/nacos/v1/cs/configs";
HttpHeaders headersConfig = new HttpHeaders();
headersConfig.setContentType(MediaType.APPLICATION_FORM_URLENCODED);
MultiValueMap<String, String> params= new LinkedMultiValueMap<>();
params.add("tenant", namespaceId);
params.add("group", group);
params.add("dataId", dataId);
params.add("content", gatewayPublishDto.getPublishContent());
params.add("type", "string");
if(StringUtils.isNotBlank(accessToken)){
params.add("accessToken", accessToken);
}
ResponseEntity<Boolean> responseEntityConfig =nacosRestTemplate.exchange(urlConfig, HttpMethod.POST,new HttpEntity<>(params,headersConfig), Boolean.class);
}
}
推荐阅读
-
使用Spring Cloud Feign作为HTTP客户端调用远程HTTP服务的方法(推荐)
-
使用Spring Cloud Feign作为HTTP客户端调用远程HTTP服务的方法(推荐)
-
Laravel 中使用 swoole 项目实战开发案例一 (建立 swoole 和前端通信)
-
Laravel 中使用 swoole 项目实战开发案例二 (后端主动分场景给界面推送消息)
-
MIT6.824 远程过程调用RPC使用
-
使用Feign做远程调用的注意点
-
Flink Window类型及使用原理案例实战-Flink牛刀小试
-
DolphinDB使用案例8:SQL调用分析函数
-
支持多文件、多格式远程上传的FTP实战案例
-
Elasticsearch结构化搜索_在案例中实战使用term filter来搜索数据