• 基于webservice 使用HttpClient调用


    1. webservice 的服务端

    - 配置类

    1. @Configuration
    2. public class CxfConfig {
    3. @Autowired
    4. CatalogServiceImpl catalogService;
    5. @Autowired
    6. private Bus bus;
    7. @Bean
    8. public Endpoint endpoint() {
    9. EndpointImpl endpoint = new EndpointImpl(bus, catalogService);
    10. endpoint.publish("/addCatalog");
    11. return endpoint;
    12. }
    13. }

    webservice的接口: 

    1. /**
    2. * @Auther tjk
    3. * @Date 2022 06 15
    4. **/
    5. @WebService(targetNamespace = "http://service.com")
    6. public interface CatalogService {
    7. /**
    8. * 目录下发
    9. */
    10. @WebMethod(action = "creatFolder")
    11. public AvicFilePathModel creatFolder(@WebParam(name = "model") AvicFilePathModel model) ;
    12. }

    这个action 后面调用的时候会用到 

    webserivice的实现类:

    1. /**
    2. * @Auther tjk
    3. * @Date 2022 06 14
    4. * 目录下发的webservice
    5. **/
    6. @WebService(serviceName = "CatalogServiceImpl",
    7. targetNamespace = "http://service.com",
    8. endpointInterface = "com.webservice.CatalogService")
    9. @Service
    10. public class CatalogServiceImpl implements CatalogService {
    11. @Value("${avic.file.store.path}\\")
    12. private String path;
    13. @Autowired
    14. private AvicFilePathService avicFilePathService;
    15. //创建文件夹
    16. @WebMethod
    17. @WebResult(name = "creatFolder")
    18. public AvicFilePathModel creatFolder(@WebParam(name = "model") AvicFilePathModel model) {
    19. //判断工业网的数据库中是否存在该目录地址
    20. AvicFilePathModel avicModel = avicFilePathService.queryAvicFilePath(model.getPath());
    21. //不存在
    22. if(avicModel==null){
    23. model.setRecDate(new Date());
    24. model.setSequenceNbr(model.getSequenceNbr().replace(",",""));
    25. AvicFilePathModel withModel = avicFilePathService.createWithModel(model);
    26. //在本地创建目录
    27. File dir = new File(String.format("%s%s%s", path, model.getPath().replace(":", ""), File.separator));
    28. if (!dir.exists()) {
    29. dir.mkdirs();
    30. }
    31. return withModel;
    32. }
    33. return avicModel;
    34. }
    35. }

    2. webservice的客户端

    HttpClient配置类

    1. /**
    2. * @Auther tjk
    3. * @Date 2022 06 21
    4. **/
    5. @Component
    6. public class HttpClientCallSoapUtil {
    7. static int socketTimeout = 30000;// 请求超时时间
    8. static int connectTimeout = 30000;// 传输超时时间
    9. static Logger logger = Logger.getLogger(HttpClientCallSoapUtil.class);
    10. /**
    11. * 使用SOAP1.2发送消息
    12. *
    13. * @param postUrl
    14. * @param soapXml
    15. * @param soapAction
    16. * @return
    17. */
    18. public static String doPostSoap1_2(String postUrl, String soapXml,
    19. String soapAction) {
    20. String retStr = "";
    21. // 创建HttpClientBuilder
    22. HttpClientBuilder httpClientBuilder = HttpClientBuilder.create();
    23. // HttpClient
    24. CloseableHttpClient closeableHttpClient = httpClientBuilder.build();
    25. HttpPost httpPost = new HttpPost(postUrl);
    26. // 设置请求和传输超时时间
    27. RequestConfig requestConfig = RequestConfig.custom()
    28. .setSocketTimeout(socketTimeout)
    29. .setConnectTimeout(connectTimeout).build();
    30. httpPost.setConfig(requestConfig);
    31. try {
    32. httpPost.setHeader("Content-Type",
    33. "application/soap+xml;charset=UTF-8");
    34. httpPost.setHeader("SOAPAction", soapAction);
    35. StringEntity data = new StringEntity(soapXml,
    36. Charset.forName("UTF-8"));
    37. httpPost.setEntity(data);
    38. CloseableHttpResponse response = closeableHttpClient
    39. .execute(httpPost);
    40. HttpEntity httpEntity = response.getEntity();
    41. if (httpEntity != null) {
    42. // 打印响应内容
    43. retStr = EntityUtils.toString(httpEntity, "UTF-8");
    44. logger.info("response:" + retStr);
    45. }
    46. // 释放资源
    47. closeableHttpClient.close();
    48. } catch (Exception e) {
    49. logger.error("exception in doPostSoap1_2", e);
    50. }
    51. return retStr;
    52. }
    53. }

    1. @Component
    2. public class CatalogServiceImpl {
    3. /**
    4. * 调用目录下发服务
    5. */
    6. @Autowired
    7. HttpClientCallSoapUtil httpClientCallSoapUtil;
    8. /**
    9. * 调用目录下发
    10. * @param model
    11. * @param ftlFile
    12. * @param postUrl
    13. * @param soapAction
    14. * @throws IOException
    15. */
    16. public void catalogCreate(AvicFilePathModel model, String ftlFile,String postUrl, String soapAction) throws IOException {
    17. Writer w = null;
    18. try {
    19. //获取xmlTemplate文件夹的当前路径
    20. URL url = Thread.currentThread().getContextClassLoader().getResource("templates");
    21. String path = url.getPath();
    22. Configuration configuration = new Configuration();
    23. configuration.setDefaultEncoding("utf-8");
    24. configuration.setDirectoryForTemplateLoading(new File(path));
    25. //解决写入到xml文件出现乱码问题
    26. Template template = configuration.getTemplate(ftlFile,"utf-8");
    27. Map<String, Object> responseMap = new HashMap<String, Object>();
    28. responseMap.put("id", model.getId());
    29. responseMap.put("agencyCode", model.getAgencyCode());
    30. responseMap.put("pathName", model.getPathName());
    31. responseMap.put("path", model.getPath());
    32. responseMap.put("pathDesc",model.getPathDesc());
    33. responseMap.put("sequenceNbr",model.getSequenceNbr());
    34. //responseMap.put("recDate",new Date());
    35. responseMap.put("recUserId", model.getRecUserId());
    36. File file = new File("E:\\xml\\"+"xmltemp.xml");
    37. FileOutputStream f = new FileOutputStream(file);
    38. w = new OutputStreamWriter(f,"utf-8");
    39. template.process(responseMap, w);
    40. String s = FreeMarkerTemplateUtils.processTemplateIntoString(template, responseMap);
    41. String orderSoapXml = template.toString();
    42. System.out.println(orderSoapXml);
    43. //采用SOAP1.2调用服务端,这种方式只能调用服务端为soap1.2的服务
    44. httpClientCallSoapUtil.doPostSoap1_2(postUrl, s, soapAction);
    45. } catch (Exception e) {
    46. // TODO Auto-generated catch block
    47. e.printStackTrace();
    48. }finally{
    49. w.close();
    50. }
    51. }
    52. }

    ftl文件

    这个ftl文件需要通过saopUI软件自己copy

    1. <?xml version="1.0" encoding="UTF-8"?>
    2. <soapenv:Envelope xmlns:soapenv="http://schemas.xmlsoap.org/soap/envelope/" xmlns:ser="http://service.avic.amos.yeejoin.com">
    3. <soapenv:Header/>
    4. <soapenv:Body>
    5. <ser:transferFile>
    6. <!--Optional:-->
    7. <fileName>${fileName}</fileName>
    8. <file>${file}</file>
    9. <path>${path}</path>
    10. </ser:transferFile>
    11. </soapenv:Body>
    12. </soapenv:Envelope>

    注:ftl文件需要放到resources/templates目录下
    控制类:url也是需要用soapui copy

     catalogService.catalogCreate(model,"catalog.ftl","http://127.0.0.1:9000/indust/services/addCatalog","creatFolder");

    这个调用的url 也需要自己通过soapui查找

  • 相关阅读:
    【强化学习】《动手学强化学习》动态规划算法
    外卖项目(项目优化1)10---缓存优化
    可升级合约的原理-DelegateCall
    .Net8的AOT引导程序BootStrap
    6000字|22张图 带你彻底弄懂Zookeeper分布式锁
    平时健身买什么耳机好、分享五款最好的运动耳机推荐
    STL priority_queue
    go操作数据库(插入、更新、删除、查找、事务)
    关键词优化-关键词优化工具-关键词优化软件免费
    xxl-job学习
  • 原文地址:https://blog.csdn.net/qq_39759664/article/details/125484627