本地应用注入iDME注解示例
示例场景
对于拥有本地应用的用户,在本地安装SDK后,只需要在启动类中注入@ComponentScan(basePackages = {"com.huawei.innovation"})注解,运行启动类即可。
操作步骤
package com.singzai; import org.springframework.boot.SpringApplication; import org.springframework.boot.autoconfigure.SpringBootApplication; import org.springframework.context.annotation.CompinentScan; @SpringBootApplication @ComponentScan(basePackages = {"com.huawei.innovation"}) public class MainApplication{ public static void main(String[] args) { SpringApplication.run(MainApplication.class, args); } }
异常处理
在启动类注入iDME注解并运行启动类时,本地调试可能会提示如下错误:
java.security.cert.CertificateException: No subject alternative DNS name matching xxx found
您可以参考如下操作进行修复。
- 在项目中创建一个类,并添加RestTemplateBuilder和RestTemplate方法。
- 分别在RestTemplateBuilder和RestTemplate方法上注入bean。
例如,您在“src/main/java/com/xdm/configuration/”路径下创建“RestTemplateConfiguration.java”,其示例代码如下:
package com.xdm.configuration; import org.apache.http.conn.ssl.NoopHostnameVerifier; import org.apache.http.impl.client.CloseableHttpClient; import org.apache.http.impl.client.HttpClients; import org.springframework.boot.web.client.RestTemplateBuilder; 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; import java.security.KeyManagementException; import java.security.NoSuchAlgorithmException; import java.security.cert.X509Certificate; @Configuration public class RestTemplateConfiguration { @Bean public RestTemplateBuilder restTemplateBuilder() { return new RestTemplateBuilder(); } @Bean public RestTemplate restTemplate(RestTemplateBuilder builder) throws NoSuchAlgorithmException, KeyManagementException { TrustManager[] trustAllCerts = new TrustManager[] { new X509TrustManager() { public java.security.cert.X509Certificate[] getAcceptedIssuers() { return new X509Certificate[0]; } public void checkClientTrusted( java.security.cert.X509Certificate[] certs, String authType) { } 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 builder.requestFactory(() -> customRequestFactory).build(); } }
- 在操作步骤添加的@ComponentScan注解中,添加1创建的类的扫描路径。
package com.singzai; import org.springframework.boot.SpringApplication; import org.springframework.boot.autoconfigure.SpringBootApplication; import org.springframework.context.annotation.CompinentScan; @SpringBootApplication @ComponentScan(basePackages = {"com.huawei.innovation","com.xdm"}) public class MainApplication{ public static void main(String[] args) { SpringApplication.run(MainApplication.class, args); } }