当前位置 : 主页 > 编程语言 > java >

二十九、Springboot整合Web services,构建SOAP服务

来源:互联网 收集:自由互联 发布时间:2022-06-23
(一)添加依赖 dependency groupIdorg.springframework.boot/groupId artifactIdspring-boot-starter-web-services/artifactId /dependency dependency groupIdwsdl4j/groupId artifactIdwsdl4j/artifactId version1.6.3/version /dependency (二)

(一)添加依赖

<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web-services</artifactId>
</dependency>

<dependency>
<groupId>wsdl4j</groupId>
<artifactId>wsdl4j</artifactId>
<version>1.6.3</version>
</dependency>

(二)创建SOAP服务端

1、编写Schema文件

<?xml version="1.0"?>
<xs:schema xmlns:xs="http://www.w3.org/2001/XMLSchema"
elementFormDefault="qualified"
targetNamespace="
xmlns=
<!--用户对象:Id,Name, Age-->
<xs:complexType name="User">
<xs:sequence>
<!--
name:必须定义
type:类型
maxOccurs/minOccurs:都为1,表示必须有;maxOccurs为1,minOccurs为0,表示该对象是可选的
-->
<xs:element name="id" type="xs:string" maxOccurs="1" minOccurs="1"/>
<xs:element name="name" type="xs:string" maxOccurs="1" minOccurs="1"/>
<xs:element name="age" type="xs:int" maxOccurs="1" minOccurs="0"/>
</xs:sequence>
</xs:complexType>

<xs:element name="UserIdRequest">
<xs:complexType>
<xs:sequence>
<xs:element name="userId" type="xs:string"/>
<xs:element name="timeStamp" type="xs:long"/>
</xs:sequence>
</xs:complexType>
</xs:element>

<xs:element name="UserResponse">
<xs:complexType>
<xs:sequence>
<xs:element name="user" type="User"/>
<xs:element name="timeStamp" type="xs:long"/>
</xs:sequence>
</xs:complexType>
</xs:element>
</xs:schema>

2、将其转换成java文件

    转换办法参见​​十五、根据编写的schema(*.xsd)来生成对应的java实体​​

3、创建仓储

@Repository
public class UserRepository {
private Map<String, User> cachedUsers = new HashMap<>();
@PostConstruct
public void init(){
User user1 = createUser("1", "潘畅", 25);
User user2 = createUser("2", "刘德华", 26);
User user3 = createUser("3", "黄晓明", 27);
/**
* 知识点:
*/
cachedUsers.put("1", user1);
cachedUsers.put("2", user2);
cachedUsers.put("3", user3);
}
private User createUser(String id, String name, Integer age){
User user = new User();
user.setId(id);
user.setName(name);
user.setAge(age);

return user;
}

public User findById(String id){
return cachedUsers.get(id);
}
}

4、创建endpoint

/**
* 用户服务的Endpoint(端点)
*/
@Endpoint
public class UserServiceEndpoint {
private UserRepository userRepository;
@Autowired
public UserServiceEndpoint(UserRepository userRepository) {
this.userRepository = userRepository;
}

/**
* namespace = ":同user.xsd文件中的targetNamespace属性
* @ResponsePayload类似于@ResponseBody注解
* @RequestPayload类似于@RequestBody注解
*/
@PayloadRoot(namespace = "", localPart = "UserIdRequest")
@ResponsePayload
public UserResponse getUser(@RequestPayload UserIdRequest userIdRequest){
String userId = userIdRequest.getUserId();
long timeStamp = userIdRequest.getTimeStamp();
/**
* JDK1.8出品:线程安全
*/
Instant instant = Instant.ofEpochMilli(timeStamp);
//转换成本地时区
ZonedDateTime zonedDateTime = instant.atZone(ZoneId.systemDefault());

System.out.println("web service 用户ID:" + userId + ",请求的时间:" + zonedDateTime);
User user = userRepository.findById(userId);
UserResponse userResponse = new UserResponse();
userResponse.setUser(user);
userResponse.setTimeStamp(Instant.now().toEpochMilli());
return userResponse;
}
}

5、创建Web services配置类

/**
* web service 配置类
*/
@Configuration
public class WebServicesConfiguration extends WsConfigurerAdapter {
/**
* 设置访问路径:@Bean("my"),案例:http://localhost:8080/test/ws/my.wsdl,
* 其中,wsdl是MessageDispatcherServlet中规定的结尾
* @param userXsdSchema @Autowired自动装配下方的userXsdSchema
* @return Wsdl11Definition对象
*/
@Bean("my")
@Autowired
public Wsdl11Definition userWsdl11Definition(XsdSchema userXsdSchema){
DefaultWsdl11Definition defaultWsdl11Definition = new DefaultWsdl11Definition();

defaultWsdl11Definition.setPortTypeName("UserServicePort");
//设置访问路径:@Bean("my"),案例:http://localhost:8080/test/ws/my.wsdl
defaultWsdl11Definition.setLocationUri("/ws");
//user.xsd中的targetNamespace属性
defaultWsdl11Definition.setTargetNamespace();
defaultWsdl11Definition.setSchema(userXsdSchema);

return defaultWsdl11Definition;
}

/**
* 注册user.xsd(Schema文件)对应的java对象
*/
@Bean
public XsdSchema userXsdSchema(){
return new SimpleXsdSchema(new ClassPathResource("META-INF/schemas/user.xsd"));
}
}

6、配置Web servicesfang访问根路径

spring:
webservices:
#自定义访问路径前缀,比如这里的访问实例:http://localhost:8080/test/ws/my.wsdl
path: /test

        到这里,服务端就创建完毕了!

(三)创建SOAP客户端

/**
* Web Service 客户端(Spring 实现)
*/
public class WebServicesClient {
public static void main(String[] args) {
WebServiceTemplate webServiceTemplate = new WebServiceTemplate();

Jaxb2Marshaller jaxb2Marshaller = new Jaxb2Marshaller();
jaxb2Marshaller.setClassesToBeBound(UserIdRequest.class, UserResponse.class, User.class);

webServiceTemplate.setMarshaller(jaxb2Marshaller);
webServiceTemplate.setUnmarshaller(jaxb2Marshaller);

//构造 SOAP 请求
UserIdRequest userIdRequest = new UserIdRequest();
userIdRequest.setUserId("1");
userIdRequest.setTimeStamp(Instant.now().toEpochMilli());

/**
* http://localhost:8080/test/ws/my:访问请求地址
* http://localhost:8080/test/ws/my.wsdl:这里出现的是xml文件内容
*/
UserResponse userResponse = (UserResponse) webServiceTemplate.marshalSendAndReceive(
"http://localhost:8080/test/ws/my", userIdRequest);

System.out.println(userResponse);
}
}

 


上一篇:十七、实现Windows中的文件名排序
下一篇:没有了
网友评论