1、引入依赖
<dependency><groupId>org.springframework.ws</groupId><artifactId>spring-ws-core</artifactId></dependency><dependency><groupId>org.springframework</groupId><artifactId>spring-oxm</artifactId></dependency>
2、拿到地址,利用窗口命令生成Java文件。
wsimport -keep -p com.example.soapclient https://xxx.cn/tp_core/xxx/MsgWebService?wsdl
3、将文件放入我们的项目中
4、编写配置类,包是生成类的所在包
@Configuration
public class SoapClientConfig {@Beanpublic Jaxb2Marshaller marshaller() {Jaxb2Marshaller marshaller = new Jaxb2Marshaller();// 设置生成的SOAP类所在的包名,这里替换成实际的包路径marshaller.setContextPath("com.ly.cloud.common.soap");return marshaller;}@Beanpublic WebServiceTemplate webServiceTemplate(Jaxb2Marshaller marshaller) {WebServiceTemplate webServiceTemplate = new WebServiceTemplate();webServiceTemplate.setDefaultUri("https://xxx/xxx/service/MsgWebService");// 配置 marshaller 和 unmarshallerwebServiceTemplate.setMarshaller(marshaller);webServiceTemplate.setUnmarshaller(marshaller);return webServiceTemplate;}
}
5、然后编写service
@Service
public class MySoapService{@Autowiredprivate WebServiceTemplate webServiceTemplate;public String callSoapService(String message) {Map<String,Object> map = new HashMap<>();map.put("OTHER_PK_ID", "");String jsonString = JSONObject.toJSONString(map);// 入参json字符串AddMessage addMessage = new AddMessage();addMessage.setIn0(jsonString);System.out.println(jsonString);AddMessageResponse response = (AddMessageResponse) webServiceTemplate.marshalSendAndReceive(addMessage);// 调用结果return response.getOut();}}
5、这里只需要传一个String类型的json入参字符串,即可拿到调用结果。实际场景可以结合Controller实现。