两种类型的接口

软件开发中,常见的接口调用类型有两种,分别是:WebService、WebAPI,它们两种类型的接口,对于调用的方式,到底有哪些区别呢?

如何调用接口

先看一下要调用这两种接口类型,以Java代码为例,需要怎么实现。

Java调用WebAPI

下面这段Java代码示例,表示调用一个WebAPI接口,获取token的过程。

 private final RestTemplate restTemplate = new RestTemplate();   

 public void getToken() {
        String url = "www.baidu.com/api/getToken";

        // 构建请求体
        JSONObject requestBody = new JSONObject();
        requestBody.put("appId", "123456");
        requestBody.put("appSecret", "abcdef");
    
        // 构建请求头
        HttpHeaders headers = new HttpHeaders();
        headers.setContentType("application/json");
    
        // 创建请求实体
        HttpEntity<String> requestEntity = new HttpEntity<>(requestBody.toJSONString(), headers);
        
        try {
            // 发送POST请求
            ResponseEntity<String> response = restTemplate.exchange(
                    url,
                    HttpMethod.POST,
                    requestEntity,
                    String.class
            );
        } catch (RestClientException e) {
            throw new RuntimeException(e);
        }
    }

Java调用WebService

private final RestTemplate restTemplate = new RestTemplate();

 String url = "www.baidu.com/api/gettoken";

        // 构建请求体
        JSONObject requestBody = new JSONObject();
        requestBody.put("appId", "123456");
        requestBody.put("appSecret", "abcdef");


private void getToken() {
            String soapRequest = ""<?xml version=\"1.0\" encoding=\"utf-8\"?>\n" +
                "<soap:Envelope xmlns:xsi=\"http://www.w3.org/2001/XMLSchema-instance\" " +
                "xmlns:xsd=\"http://www.w3.org/2001/XMLSchema\" " +
                "xmlns:soap=\"http://schemas.xmlsoap.org/soap/envelope/\">\n" +
                "  <soap:Body>\n" +
                "    <getToken xmlns=\"http://tempuri.org/\">\n" +
                "      <UserName>" + interfaceSGConfig.getUsername() + "</UserName>\n" +
                "      <PassWord>" + interfaceSGConfig.getPassword() + "</PassWord>\n" +
                "      <appId>123456</appId>\n" +
                "      <appSecret>abcdef</appSecret>\n" +
                "    </getToken>\n" +
                "  </soap:Body>\n" +
                "</soap:Envelope>";"

            HttpHeaders headers = new HttpHeaders();
            headers.add("Content-Type", "text/xml;charset=utf-8");
            HttpEntity<String> requestEntity = new HttpEntity<>(soapRequest, headers);

            ResponseEntity<String> response = restTemplate.exchange(
                    "www.baidu.com/getToken.asmx",
                    HttpMethod.POST,
                    requestEntity,
                    String.class
            );
    }

以上两段代码,表达了类似的逻辑,都是调用接口,获取token。区别在于一个是调用的WebAPI,另一个是调用的WebService。

两种类型区别

从上面的示例代码中,可以总结一下,两种接口类型,在调用方式上,有以下区别:

要点 WebService WebAPI
消息格式 XML(强制) JSON 主流(也可以是 XML/二进制)
HTTP 方法 全部 POST(URL 里看不出动作) GET/POST/PUT/DELETE 按需选用
动作/资源 动作写在 SOAP Body 节点名 动作由 HTTP 方法+资源路径表达
Content-Type text/xml application/json

现在用的比较多的是webapi的方式定义接口,调用也相对简单。

还有一点就是,无论是Java或是.net,都可以定义WebService或者WebAPI。

Logo

火山引擎开发者社区是火山引擎打造的AI技术生态平台,聚焦Agent与大模型开发,提供豆包系列模型(图像/视频/视觉)、智能分析与会话工具,并配套评测集、动手实验室及行业案例库。社区通过技术沙龙、挑战赛等活动促进开发者成长,新用户可领50万Tokens权益,助力构建智能应用。

更多推荐