restful请求方式调用采用 https协议的接口

额外的注解说明:
@Autowired -------根据类型从spring容器里进行自动装配
@Qualifier -------@Qualifier(“XXX”) 中的 XX是某一处声明的Bean 的名称(@Bean(name=“XXX”)),所以 @Autowired 和 @Qualifier 结合使用时,自动注入的策略就从 byType 转变成 byName 了

调https协议的接口大都需要证书,这次我们用忽略证书的方式调接口首先直接上忽略https证书验证的代码:

package com.dic.dms.config;

import org.apache.http.conn.ssl.NoopHostnameVerifier;
import org.apache.http.impl.client.CloseableHttpClient;
import org.apache.http.impl.client.HttpClients;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.http.client.HttpComponentsClientHttpRequestFactory;
import org.springframework.web.client.RestTemplate;

import javax.net.ssl.SSLContext;
import javax.net.ssl.TrustManager;
import javax.net.ssl.X509TrustManager;

/**
 * @ClassName RestTemplateConfiguration
 * @Description TODO
 * @Author Duzh
 * @Date 2019/5/8 2:57 PM
 * @Version 1.0.0
 */
@Configuration
public class RestTemplateConfiguration {

    /**
     * 设置RetTemplate负载均衡策略,(轮询)
     * @return
     */
    @Bean
    public RestTemplate restTemplate() {
        return new RestTemplate();
    }


    /**
     * 忽略https证书验证
     * @return
     * @throws Exception
     */
    @Bean(name="restTemplateRemote")
    public RestTemplate restTemplateRemote() throws Exception {
        TrustManager[] trustAllCerts =new TrustManager[] {
                new X509TrustManager() {
                    @Override
                    public java.security.cert.X509Certificate[] getAcceptedIssuers() {
                        return new java.security.cert.X509Certificate[0];
                    }
                    @Override
                    public void checkClientTrusted(
                            java.security.cert.X509Certificate[] certs, String authType) {
                    }
                    @Override
                    public void checkServerTrusted(
                            java.security.cert.X509Certificate[] certs, String authType) {
                    }
                }
        };
        SSLContext sslContext =SSLContext.getInstance("SSL");
        sslContext.init(null, trustAllCerts, new java.security.SecureRandom());
        CloseableHttpClient httpClient = HttpClients.custom()
                .setSSLContext(sslContext)
                .setSSLHostnameVerifier(NoopHostnameVerifier.INSTANCE)
                .build();
        HttpComponentsClientHttpRequestFactory customRequestFactory =new HttpComponentsClientHttpRequestFactory();
        customRequestFactory.setHttpClient(httpClient);
        return new RestTemplate(customRequestFactory);
    }

}

然后上调用接口的方法(这里是在springboot的测试类中):

package com.dic.dms;

import org.junit.Test;
import org.junit.runner.RunWith;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.boot.test.context.SpringBootTest;
import org.springframework.http.ResponseEntity;
import org.springframework.test.context.junit4.SpringRunner;
import org.springframework.web.client.RestTemplate;

@RunWith(SpringRunner.class)
@SpringBootTest
public class DmsWebApplicationTests {

    @Autowired
    @Qualifier("restTemplateRemote")
    RestTemplate restTemplate;

    @Test
    public void contextLoads() {
        String url = "https://111.227.172.92:8089/rest/tokens?username=dkdeco&password=dkdeco654321";
        发送消息
        ResponseEntity<String> responseEntity = restTemplate.postForEntity(url, null, String.class);
        System.out.println("请求结果:" + responseEntity.getBody());
    }

}

深河家园西区37栋601

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值