使用RestTemplate远程调用实战案例

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);

    }
}

 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
你好!关于使用RestTemplate进行远程调用,我可以给你一些帮助。RestTemplate是Spring框架提供的一个用于发送HTTP请求的客户端工具。你可以使用它来调用其他系统的RESTful API。 首先,你需要在你的项目中导入RestTemplate的依赖。如果你使用的是Maven,可以在pom.xml文件中添加以下依赖: ```xml <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-web</artifactId> </dependency> ``` 接下来,你可以在你的代码中使用RestTemplate来发送HTTP请求。下面是一个简单的示例: ```java import org.springframework.http.HttpMethod; import org.springframework.http.ResponseEntity; import org.springframework.web.client.RestTemplate; public class RemoteServiceClient { public static void main(String[] args) { RestTemplate restTemplate = new RestTemplate(); String url = "http://example.com/api/endpoint"; ResponseEntity<String> response = restTemplate.exchange(url, HttpMethod.GET, null, String.class); String responseBody = response.getBody(); System.out.println(responseBody); } } ``` 在上面的示例中,我们创建了一个RestTemplate实例,并使用exchange()方法发送了一个GET请求。你可以根据需要选择合适的HTTP方法(如GET、POST、PUT等)和请求体内容。 当然,在实际应用中,你可能还需要处理错误、设置请求头、传递URL参数等。RestTemplate提供了一些其他的方法和功能来满足这些需求。如果你需要更多细节或更复杂的使用场景,可以查阅Spring官方文档或其他相关资源。 希望这些信息对你有所帮助!如果你有任何其他问题,请随时提问。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值