当前位置: 首页 > news >正文

直接用http请求调用别人的webservice服务 并且包含账号密码 解决使用Apache CXF 生产代码无法使用的问题 不使用

使用 SoapUI 调用别人提供的wsdl 文件调用成功没问题以后 根据apache cxf 生成的代码发起调用报错

刚开始报错找不到什么东西 然后我使用
System.setProperty(“javax.xml.bind.JAXBContext”, “com.sun.xml.internal.bind.v2.ContextFactory”);
加到代码里面解决此问题

然后又报错
com.sun.xml.internal.ws.fault.ServerSOAPFaultException: Client received SOAP Fault from server: Server Error Please see the server log to find more detail regarding exact cause of the failure.

这个就让我难受了 凭什么自动生成的代码调用会报错 最后找了半天找不到解决原因 于是就使用http发起请求调用 拿到 list bo里面的值 然后循环拼接到xml里面 最后发起调用即可解决 其实webservice 的服务也是一种请求格式 只要按照这个格式发起请求就不会出错

当然也可以加上对方的webservice 的账号密码 调用 以下是我的代码 直接异步调用 不需要管

免费公开 还希望您可以

点赞 评论 加关注


    public static void main(String[] args) {
        ExecutorService executor = Executors.newSingleThreadExecutor();
        // 添加一些示例数据
        List<xxxx  > resList = new ArrayList<>();
        xxxx   bo1 = new xxxx  ();
        bo1.setId("1");
        bo1.setCode("ok");
        resList.add(bo1);
        xxxx  bo2 = new xxxx  ();
        bo2.setId("2");
        bo2.setCode("ok");
        resList.add(bo2);

        executor.submit(() -> {
            try{
                getallpust(resList);
            }catch (Exception e){
                System.out.println("报错");
                System.out.println(e);
            }
        });
        executor.shutdown();
    }




    public static void getallpust(List<xxxxxx> resultTableList){
        String url = "http://127.0.0.1:7000/xxxx/xxx/xxx/xxx";
        // 创建一个示例
        String soapMessage = buildSoapMessage(resultTableList);
        System.out.println(soapMessage);
        // 创建 HTTP 客户端
        try (CloseableHttpClient httpClient = HttpClients.createDefault()) {
            HttpPost httpPost = new HttpPost(url);
            // 设置请求头
            httpPost.setHeader("Content-Type", "text/xml;charset=UTF-8");
            httpPost.setHeader("SOAPAction", "http://sap.com/xi/WebService/soap1.1");
            // 添加 Basic Auth
            String auth = "xxxxx:xxxxx"; // 替换为你的账号和密码
            byte[] encodedAuth = Base64.getEncoder().encode(auth.getBytes(StandardCharsets.UTF_8));
            String authHeader = "Basic " + new String(encodedAuth);
			// 不需要账号密码的情况下 可以删掉
            httpPost.setHeader("Authorization", authHeader);
            // 设置请求体
            httpPost.setEntity(new StringEntity(soapMessage, StandardCharsets.UTF_8));

            // 发送请求
            try (CloseableHttpResponse response = httpClient.execute(httpPost)) {
                HttpEntity responseEntity = response.getEntity();
                if (responseEntity != null) {
                    String responseString = EntityUtils.toString(responseEntity, StandardCharsets.UTF_8);
                    System.out.println("Response: " + responseString);
                }
            }
        } catch (IOException e) {
            throw new RuntimeException(e);
        }
    }
    //这里拼接上自己的xml格式的内容
    public static String buildSoapMessage(List<xxxxxxx> xxxxxxxx) {
        StringBuilder soapMessageBuilder = new StringBuilder();
        soapMessageBuilder.append("<xxxxxxx>\n");
        soapMessageBuilder.append("   <xxxxxxx>\n");
        soapMessageBuilder.append("      <xxxxxxx>\n");
        soapMessageBuilder.append("      </xxxxxx>\n");
        soapMessageBuilder.append("   </xxxxxx>\n");
        soapMessageBuilder.append("</xxxxxxx>");

        return soapMessageBuilder.toString();
    }

    // 用于转义 XML 特殊字符
    public static String escapeXml(String input) {
        if (input == null) {
            return "";
        }
        return input.replace("&", "&amp;")
                .replace("<", "&lt;")
                .replace(">", "&gt;")
                .replace("\"", "&quot;")
                .replace("'", "&apos;");
    }

这个里面的url地址是别人提供给你的wsdl文件在soapUI里面导入以后发起调用的地址

在这里插入图片描述

1,打开soap 选择wsdl文件

2,找到你需要的请求 点击request 1

复制3的地址到你的url里面

注意!!!! url地址如果报错 我记得是 218 还是 208 就是有特殊字符 你自己转一下即可

相关文章:

  • 【Java】IO流
  • 1358. 包含所有三种字符的子字符串数目
  • 复试准备日常
  • 什么是SYN洪范攻击?
  • 抖音DP代运营服务——品融电商赋能品牌增长
  • Python 调试pdb和-i模式
  • NodePort+LoadBalancer+Ingress+MetalLB+HostNetwork+Istio Gateway
  • 【神经网络】python实现神经网络(一)——数据集获取
  • 通俗的方式解释“零钱兑换”问题
  • 利用Python爬取中国气象局天气预报数据
  • ctf网络安全赛题
  • tsconfig.json 配置清单
  • C语言运算符优先级,简单样例理解
  • 从芯片到光网络:解密平面光波导技术(PLC)核心优势
  • 【IEEE/EI/Scopus检索】2025年3-4月传感器与信息技术、通信、软件、信号处理领域国际学术会议征稿开启!
  • 【0001】初识Java
  • 新豪轩荣登国货之光计划领航中国定制门窗发展新征程
  • 【练习】【链表】力扣热题100 19. 删除链表的倒数第 N 个结点
  • 扩散语言模型:从图像生成到文本创造的范式跃迁
  • redis事务详解
  • 网站主页 优帮云/今日的头条新闻
  • 网站导航条/上海网站建设费用
  • 用html做的零食网站/网站优化排名易下拉霸屏
  • 报社网站开发做什么/网络推广推广
  • 烟台市最好的专业做网站的公司/win10最强性能优化设置
  • 深圳哪家做网站最好/南昌百度快速排名提升