|  |  |  | 
|---|
|  |  |  | @Autowired | 
|---|
|  |  |  | private RestTemplateWechatCertConfig restTemplateWechatCertConfig; | 
|---|
|  |  |  |  | 
|---|
|  |  |  | //@Bean | 
|---|
|  |  |  | //public RestTemplate restTemplate() { | 
|---|
|  |  |  | //    return new RestTemplate(); | 
|---|
|  |  |  | //} | 
|---|
|  |  |  |  | 
|---|
|  |  |  | //@Bean | 
|---|
|  |  |  | //public RestTemplate restTemplate() { | 
|---|
|  |  |  | //    String mchid = PayInfo.mchid; | 
|---|
|  |  |  | //    RestTemplate restTemplate = null; | 
|---|
|  |  |  | //    try { | 
|---|
|  |  |  | //        KeyStore keyStore = KeyStore.getInstance("PKCS12"); | 
|---|
|  |  |  | //        //InputStream cp = this.getClass().getResourceAsStream("apiclient_cert.p12"); | 
|---|
|  |  |  | //        FileInputStream instream = new FileInputStream(new File("C:\\webchat\\apiclient_cert.p12")); | 
|---|
|  |  |  | //        keyStore.load(instream, mchid.toCharArray()); | 
|---|
|  |  |  | //        // Trust own CA and all self-signed certs | 
|---|
|  |  |  | //        SSLContext sslcontext = SSLContextBuilder.create() | 
|---|
|  |  |  | //                .loadKeyMaterial(keyStore, mchid.toCharArray()) | 
|---|
|  |  |  | //                .build(); | 
|---|
|  |  |  | //         //Allow TLSv1 protocol only | 
|---|
|  |  |  | //        SSLConnectionSocketFactory sslsf = new SSLConnectionSocketFactory(sslcontext, new String[]{"TLSv1"}, null, NoopHostnameVerifier.INSTANCE); | 
|---|
|  |  |  | //        CloseableHttpClient httpClient = HttpClients.custom().setSSLSocketFactory(sslsf).build(); | 
|---|
|  |  |  | //        HttpComponentsClientHttpRequestFactory factory = new HttpComponentsClientHttpRequestFactory(httpClient); | 
|---|
|  |  |  | // | 
|---|
|  |  |  | //        restTemplate = new RestTemplate(factory); | 
|---|
|  |  |  | //        //将转换器的编码换成utf-8 | 
|---|
|  |  |  | //        restTemplate.getMessageConverters().set(1, new StringHttpMessageConverter(Charset.forName("utf-8"))); | 
|---|
|  |  |  | //        //System.out.println("restTemplate.hashCode():" + restTemplate.hashCode()); | 
|---|
|  |  |  | //    } catch (Exception e) { | 
|---|
|  |  |  | //        e.printStackTrace(); | 
|---|
|  |  |  | //    } | 
|---|
|  |  |  | //    return restTemplate; | 
|---|
|  |  |  | //} | 
|---|
|  |  |  |  | 
|---|
|  |  |  | //@Bean(name = "wechatRestTemplate") | 
|---|
|  |  |  | @Bean() | 
|---|
|  |  |  | public RestTemplate restTemplate() throws Exception { | 
|---|
|  |  |  | RestTemplate restTemplate = new RestTemplate(restTemplateWechatCertConfig.wechatHttpRequestFactory()); | 
|---|
|  |  |  | 
|---|
|  |  |  |  | 
|---|
|  |  |  | return restTemplate; | 
|---|
|  |  |  | } | 
|---|
|  |  |  |  | 
|---|
|  |  |  | //简单RestTemplate实例 | 
|---|
|  |  |  | @Bean | 
|---|
|  |  |  | public RestTemplate simpleRestTemplate() { | 
|---|
|  |  |  | return new RestTemplate(); | 
|---|
|  |  |  | } | 
|---|
|  |  |  | } | 
|---|