-
使用 JDK 自帶的 wsimport 工具生成實體類
1.1 創建身份驗證文件(用於 Webservice 身份驗證—auth.txt
# 格式 http://賬號:密碼@wsdl地址 # 案例 http://userName:password@192.168.1.3/ReportServer/reportservice2010.asmx?wsdl
1.2 創建文件夾(用於保存生成的WebService實體類—wsdl
1.3 在 JDK 安裝目錄下的 bin 目錄下,輸入 cmd 進入黑窗口
1.4 輸入命令,生成客戶端實體類
wsimport -s 實體類保存文件夾路徑 -p 包路徑 -encoding utf-8 -keep -verbose -Xauthfile 驗權文件路徑 wsdl地址 # 案例 wsimport -s d:\wsdl -p cn.lqdev.webservice -encoding utf-8 -keep -verbose -Xauthfile d:\auth.txt http://192.168.x.x/ReportServer/reportservice2010.asmx?wsdl
-
將實體類導入到項目中
SOPA方式
需要依賴
<!--WebService 所需依賴--> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-web-services</artifactId> </dependency>
創建身份驗證類
import org.springframework.beans.factory.annotation.Value; import org.springframework.stereotype.Component; import java.net.Authenticator; import java.net.PasswordAuthentication; /** * <p>描述: [返回鑒權信息] </p> * <p>創建時間: 2020/4/7 </p> * * @author <a href="mailto:chenys@highzap.com" rel="nofollow">Chenys</a> * @version v1.0 */ @Component public class SsrsAuthenticator extends Authenticator { @Value("${report.ssrs.confirm.userName}") private String ssrsUserName; @Value("${report.ssrs.confirm.password}") private String ssrsPassword; /** * 描述: [重寫父類方法 返回鑒權信息] * * @param * @return * @throws * @date 2020/4/7 16:29 */ @Override public PasswordAuthentication getPasswordAuthentication() { return new PasswordAuthentication(ssrsUserName, ssrsPassword.toCharArray()); } }
創建配置類(返回SOAP接口的實現類
import com.highzap.report.webservice.reportingservice.ReportingService2010; import com.highzap.report.webservice.reportingservice.ReportingService2010Soap; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.beans.factory.annotation.Value; import org.springframework.context.annotation.Bean; import org.springframework.context.annotation.Configuration; import java.net.Authenticator; import java.net.MalformedURLException; import java.net.URL; /** * <p>描述: [ReportingService 配置類] </p> * <p>創建時間: 2020/4/2 </p> * * @author <a href="mailto:chenys@highzap.com" rel="nofollow">Chenys</a> * @version v1.0 */ @Configuration public class SsrsConfig { @Autowired private SsrsAuthenticator ssrsAuthenticator; @Value("${report.ssrs.wsdl}") private String wsdl; /** * 描述: [獲取一個實例對象] * * @param * @return reportingService2010Soap * @date 2020/4/3 14:55 */ @Bean public ReportingService2010Soap getReportingService2010Soap() throws MalformedURLException { // SOAP鑒權 Authenticator.setDefault(ssrsAuthenticator); // 返回一個SOAP接口實現類 return new ReportingService2010(new URL(wsdl)).getReportingService2010Soap(); } }
CXF 方式(兩種方式效果一樣
需要依賴
<!--WebService 所需依賴--> <dependency> </dependency>
創建配置類(返回SOAP接口的實現類
/** * <p>描述: [ReportingService 配置類] </p> * <p>創建時間: 2020/4/2 </p> * * @author <a href="mailto:chenys@highzap.com" rel="nofollow">Chenys</a> * @version v1.0 */ @Configuration public class SsrsConfig { @Value("${report.ssrs.wsdl}") private String wsdl; /** * 描述: [獲取一個實例對象] * * @param * @return reportingService2010Soap * @date 2020/4/3 14:55 */ @Bean public ReportingService2010Soap getReportingService2010Soap() throws MalformedURLException { // 代理工廠 JaxWsProxyFactoryBean jaxWsProxyFactoryBean = new JaxWsProxyFactoryBean(); // 認證用戶、密碼 jaxWsProxyFactoryBean.setUsername("userName"); jaxWsProxyFactoryBean.setPassword("password"); // 設置代理地址 jaxWsProxyFactoryBean.setAddress(wsdl); // 設置接口類型 jaxWsProxyFactoryBean.setServiceClass(ReportingService2010Soap.class); // 創建一個代理接口實現 ReportingService2010Soap us = (ReportingService2010Soap) jaxWsProxyFactoryBean.create(); return us; } }
-
調用
package com.highzap.report.proxy.ssrs; import com.highzap.report.dto.ssrs.GetReportDTO; import com.highzap.report.dto.ssrs.UploadReportDTO; import com.highzap.report.exception.ReportBusinessException; import com.highzap.report.webservice.reportingservice.ArrayOfCatalogItem; import com.highzap.report.webservice.reportingservice.ArrayOfProperty; import com.highzap.report.webservice.reportingservice.CatalogItem; import com.highzap.report.webservice.reportingservice.ReportingService2010Soap; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.stereotype.Component; import java.util.List; /** * <p>描述: [封裝 ReportingService 官方代理類] </p> * <p>創建時間: 2020/4/2 </p> * * @author <a href="mailto:chenys@highzap.com" rel="nofollow">Chenys</a> * @version v1.0 */ @Component public class SsrsProxy { @Autowired private ReportingService2010Soap reportingService2010Soap; private String notExist = "Unknown"; /** * 描述: [報表上傳] * * @param uploadReportDTO [上傳報表方法 參數封裝] * @return * @date 2020/4/2 15:02 */ public void uploadReport(UploadReportDTO uploadReportDTO) { reportingService2010Soap .createCatalogItem( uploadReportDTO.getItemType(), uploadReportDTO.getName(), uploadReportDTO.getParent(), uploadReportDTO.getOverwrite(), uploadReportDTO.getDefinition(), uploadReportDTO.getProperties(), uploadReportDTO.getItemInfo(), uploadReportDTO.getWarning()); } }
常見問題
-
原因:JDK放置在了 C盤,導致JDK的安全策略,會禁止proxy使用用戶名密碼這種鑒權方式;
解決辦法:修改JDK路徑下的文件(jdk1.8.0_111/jre/lib/net.properties)
-
報錯信息:缺少xxxxImpl類
報錯原因:JDK版本問題
解決辦法:切換JDK版本為 JDK1.8