WebService接口开发demo
本文主要基于WebService/SOAP来编写一个简单的BlogService服务,代码使用SpringBoot框架,并且使用RestTemplate客户端来测试WebService接口的调用,下面将给出完整的demo示例。
主要步骤如下:
- 新建SpringBoot工程,
- 引入相关依赖
- 定义WebService接口
- 实现WebService接口
- 配置WebService服务
- 编写客户端测试代码
1. WebService服务端
1.1 新建SpringBoot工程
-
工程目录
-
Application主程序
package com.example.wsserver;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
@SpringBootApplication
public class WsServerApplication {
public static void main(String[] args) {
SpringApplication.run(WsServerApplication.class, args);
}
}
1.2 引入maven依赖
<parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>2.7.11</version>
<relativePath/>
</parent>
<dependencies>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web-services</artifactId>
</dependency>
<!--SAAJ相关依赖,用于处理SOAP消息-->
<dependency>
<groupId>com.sun.xml.messaging.saaj</groupId>
<artifactId>saaj-impl</artifactId>
<version>1.5.1</version>
</dependency>
<!--JAX-WS API相关依赖,通常用于开发和部署基于SOAP协议的Web服务-->
<dependency>
<groupId>javax.xml.ws</groupId>
<artifactId>jaxws-api</artifactId>
<version>2.3.1</version>
</dependency>
<!--JAXB API相关依赖,提供了Java类与XML数据映射相关的实现-->
<dependency>
<groupId>javax.xml.bind</groupId>
<artifactId>jaxb-api</artifactId>
<version>2.3.1</version>
</dependency>
</dependencies>
1.3 定义WebService接口
- @WebService注解:标记BlogService接口为Web服务端点
- @WebMethod注解:标注一个方法是一个Web服务的操作
- @WebParam注解:用于指定@WebMethod中方法参数的名称和部分元数据,以便在生成的WSDL文件中使用
package com.example.wsserver.service;
import javax.jws.WebMethod;
import javax.jws.WebParam;
import javax.jws.WebService;
@WebService
public interface BlogService {
@WebMethod
String getBlogById(@WebParam(name = "id") Long id);
@WebMethod
String createBlog(@WebParam(name = "title") String title,
@WebParam(name = "content") String content);
}
1.4 实现WebService接口
- serviceName:指定服务的名称。
- targetNamespace:指定服务的目标命名空间。目标命名空间是用来唯一标识服务的命名空间。它通常是一个 URL,用于表示服务的所属域或公司等信息。
package com.example.wsserver.service.impl;
import com.example.wsserver.service.BlogService;
import org.springframework.stereotype.Component;
import javax.jws.WebService;
@Component
@WebService(
serviceName = "BlogService",
targetNamespace = "http://www.example.com/blog"
)
public class BlogServiceImpl implements BlogService {
@Override
public String getBlogById(Long id) {
return "This is a blog!";
}
@Override
public String createBlog(String title, String content) {
return "This is a new blog, title is " + title + " and content is " + content;
}
}
1.5 配置WebService服务
SimpleJaxWsServiceExporter
是一个Spring Web Services的组件,用于将一个或多个@WebService
注解的类公开为Web服务- setBaseAddress设置基本地址为
http://localhost:8081/
,这意味着Web服务将通过该地址公开
package com.example.wsserver.config;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.remoting.jaxws.SimpleJaxWsServiceExporter;
@Configuration
public class WebServiceConfig {
@Bean
public SimpleJaxWsServiceExporter jaxWsServiceExporter() {
SimpleJaxWsServiceExporter exporter = new SimpleJaxWsServiceExporter();
exporter.setBaseAddress("http://localhost:8081/");
return exporter;
}
}
至此,WebService服务端实现完成,启动服务端,然后编写客户端测试代码即可进行测试。
2. WebService客户端
在test目录下,新建WsClientTest客户端测试代码如下:
package com.example.wsserver;
import org.springframework.http.*;
import org.springframework.web.client.RestTemplate;
import org.w3c.dom.Document;
import org.w3c.dom.NodeList;
import org.xml.sax.InputSource;
import org.xml.sax.SAXException;
import javax.xml.parsers.DocumentBuilder;
import javax.xml.parsers.DocumentBuilderFactory;
import javax.xml.parsers.ParserConfigurationException;
import java.io.IOException;
import java.io.StringReader;
public class HttpClientTest {
public static void main(String[] args) throws IOException, SAXException, ParserConfigurationException {
RestTemplate restTemplate = new RestTemplate();
String url = "http://localhost:8081/BlogService";
String soapAction = "http://www.example.com/blog/BlogService/getBlogById";
// 构建SOAP请求报文
String requestMessage = "<?xml version=\"1.0\" encoding=\"utf-8\"?>" +
"<soap:Envelope xmlns:soap=\"http://schemas.xmlsoap.org/soap/envelope/\">" +
"<soap:Body>" +
"<ns:getBlogById xmlns:ns=\"http://www.example.com/blog\">" +
"<ns:id>1</ns:id>" +
"</ns:getBlogById>" +
"</soap:Body>" +
"</soap:Envelope>";
// 构建HTTP头信息
HttpHeaders headers = new HttpHeaders();
headers.setContentType(MediaType.TEXT_XML);
headers.add("Content-Type", "application/xml");
headers.set("SOAPAction", soapAction);
HttpEntity<String> entity = new HttpEntity<>(requestMessage, headers);
ResponseEntity<String> response = restTemplate.exchange(url, HttpMethod.POST, entity, String.class);
// 解析SOAP响应报文
String responseBody = response.getBody();
// TODO: 解析SOAP响应报文
System.out.println("###:"+responseBody);
DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance();
DocumentBuilder builder = factory.newDocumentBuilder();
assert responseBody != null;
Document document = builder.parse(new InputSource(new StringReader(responseBody)));
NodeList nodeList = document.getElementsByTagName("return");
if (nodeList.getLength() > 0) {
String result = nodeList.item(0).getTextContent();
System.out.println("result is: " + result);
}
}
}