WebService–基础–03–实践
代码地址
https://gitee.com/DanShenGuiZu/learnDemo/tree/master/webservice-learn
1、搭建 服务端
1.1、结构
1.2、代码
User
import lombok.AllArgsConstructor;
import lombok.Data;
import lombok.NoArgsConstructor;
import lombok.ToString;
@Data
@AllArgsConstructor
@ToString
@NoArgsConstructor
public class User {
private Integer id;
private String userId;
private String userName;
}
WebServiceConfig
@Configuration
public class WebServiceConfig
{
@Bean(name = Bus.DEFAULT_BUS_ID)
public SpringBus springBus() {
return new SpringBus();
}
@Bean(name = "wsBean")
public ServletRegistrationBean dispatcherServlet() {
ServletRegistrationBean wbsServlet = new ServletRegistrationBean(new CXFServlet(), "/webservice/*");
return wbsServlet;
}
@Bean
public UserService userService() {
return new UserServiceImpl();
}
@Bean
public Endpoint endpointPurchase() {
EndpointImpl endpoint = new EndpointImpl(springBus(), userService());
endpoint.publish("/api");
System.out.println("服务发布成功!地址为:http://localhost:9898/server/webservice/api?wsdl");
return endpoint;
}
}
UserService
@WebService(name = "UserService", // 暴露服务名称
targetNamespace = "http://webService.business.server.zhou.fei"// 命名空间,一般是接口的包名倒序
)
@SOAPBinding(style = SOAPBinding.Style.RPC)
public interface UserService {
int add(User user);
User getById(Integer id);
@WebMethod
User getById2(@WebParam(name="id") String id);
}
UserServiceImpl
import fei.zhou.server.business.bean.User;
import javax.jws.WebService;
// 与接口中指定的name一致
@WebService(serviceName = "UserService",
// 与接口中的命名空间一致,一般是接口的包名倒
targetNamespace = "http://webService.business.server.zhou.fei",
// 接口地址
endpointInterface = "fei.zhou.server.business.webService.UserService"
)
public class UserServiceImpl implements UserService {
@Override
public int add(User user) {
System.out.println("-------------add-------------");
System.out.println(user.toString());
return 1;
}
@Override
public User getById(Integer id) {
System.out.println("-------------getById-------------");
System.out.println("queryUser" + " " + id);
User user = new User();
user.setId(id);
user.setUserId("xiaomi");
user.setUserName("小米");
System.out.println(user.toString());
return user;
}
@Override
public User getById2(String id) {
System.out.println("-------------getById2-------------");
System.out.println("queryUser" + " " + id);
User user = new User();
user.setId(Integer.valueOf(id));
user.setUserId("xiaomi2");
user.setUserName("小米2");
System.out.println(user.toString());
return user;
}
}
application.yml
server:
port: 9898
servlet:
context-path: /server
pom.xml
<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 https://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>2.4.9</version>
<relativePath/> <!-- lookup parent from repository -->
</parent>
<groupId>fei.zhou</groupId>
<artifactId>server</artifactId>
<version>0.0.1-SNAPSHOT</version>
<name>server</name>
<description>Demo project for Spring Boot</description>
<properties>
<java.version>1.8</java.version>
</properties>
<dependencies>
<!-- webService begin-->
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web-services</artifactId>
</dependency>
<dependency>
<groupId>org.apache.cxf</groupId>
<artifactId>cxf-rt-frontend-jaxws</artifactId>
<version>3.2.0</version>
</dependency>
<dependency>
<groupId>org.apache.cxf</groupId>
<artifactId>cxf-rt-transports-http</artifactId>
<version>3.2.4</version>
</dependency>
<dependency>
<groupId>org.apache.cxf</groupId>
<artifactId>cxf-core</artifactId>
<version>3.3.5</version>
</dependency>
<dependency>
<groupId>org.apache.cxf</groupId>
<artifactId>cxf-rt-transports-http</artifactId>
<version>3.2.4</version>
</dependency>
<!-- webService end-->
<!-- springboot begin-->
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-test</artifactId>
</dependency>
<!-- springboot end -->
<dependency>
<groupId>org.projectlombok</groupId>
<artifactId>lombok</artifactId>
</dependency>
</dependencies>
<build>
<plugins>
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
</plugin>
</plugins>
</build>
</project>
1.3、测试
http://localhost:9898/server/webservice/api?wsdl
出现以上数据,表示成功
2、搭建 client01
2.1、结构
- 使用服务端 的POM
2.2、打包
服务端把 以下2个class文件打成jar包,提供给客户端使用
2.3、代码
public class Client {
public static void main(String[] args) throws MalformedURLException {
System.setProperty("javax.xml.bind.JAXBContext", "com.sun.xml.internal.bind.v2.ContextFactory");
//创建WSDL的URL
URL url = new URL("http://localhost:9898/server/webservice/api?wsdl");
// 指定命名空间和服务名称
QName qName = new QName("http://webService.business.server.zhou.fei", "UserService");
Service service = Service.create(url, qName);
// 通过getPort方法返回指定接口
// 方法1
UserService myServer = service.getPort(UserService.class);
// 方法2
// UserService myServer = service.getPort(new QName("http://serviceImpl.service.usts.edu.cn/", "UserServiceImplPort"), UserService.class);
System.out.println("----------------------调用add方法----------------------");
User user1 = new User(2, "tom", "汤姆猫");
myServer.add(user1);
System.out.println("----------------------调用getById方法----------------------");
User user = myServer.getById(2);
System.out.println(user.toString());
}
}
2.4、测试
3、搭建 client02
3.1、结构
- 使用服务端 的POM
3.2、使用WSDL生成本地客户端代码
进入到 webServiceSrc目录下,使用wsimport 命令,生成客户端代码
wsimport -keep -extension http://localhost:9898/server/webservice/api?wsdl
然后就可以在目录中看到生成的客户端代码
3.3、代码
public class Client {
public static void main(String[] args) throws MalformedURLException {
//创建服务访问点集合的对象
fei.zhou.server.business.webservice.UserService_Service has = new fei.zhou.server.business.webservice.UserService_Service();
//获取服务实现类
//根据服务访问点的集合中的服务访问点的绑定对象来获得绑定的服务类
fei.zhou.server.business.webservice.UserService soap = has.getUserServiceImplPort();
//调用服务
fei.zhou.server.business.webservice.User user = soap.getById(2);
System.out.println("-------结果-------");
System.out.println(user.getId());
System.out.println(user.getUserId());
System.out.println(user.getUserName());
}
}
3.4、测试
4、使用htpp + XMl文本 访问服务端
4.1、请求
http://localhost:9898/server/webservice/api?wsdl
body内容
<soapenv:Envelope xmlns:soapenv="http://schemas.xmlsoap.org/soap/envelope/" xmlns:pm="http://webService.business.server.zhou.fei">
<soapenv:Header/>
<soapenv:Body>
<pm:getById2>
<id>11111</id>
</pm:getById2>
</soapenv:Body>
</soapenv:Envelope>