说明:springboot版本 2.7.11
1.pom
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web-services</artifactId>
<version>2.7.11</version>
</dependency>
<dependency>
<groupId>org.apache.cxf</groupId>
<artifactId>cxf-rt-frontend-jaxws</artifactId>
<version>3.1.6</version>
</dependency>
<dependency>
<groupId>org.apache.cxf</groupId>
<artifactId>cxf-rt-transports-http</artifactId>
<version>3.1.6</version>
</dependency>
<dependency>
<groupId>org.apache.cxf</groupId>
<artifactId>cxf-spring-boot-starter-jaxws</artifactId>
<version>3.5.2</version>
</dependency>
2.config
package com.ruoyi.config;
import com.ruoyi.webservice.SmsService;
import org.apache.cxf.Bus;
import org.apache.cxf.bus.spring.SpringBus;
import org.apache.cxf.jaxws.EndpointImpl;
import org.apache.cxf.transport.servlet.CXFServlet;
import org.springframework.boot.web.servlet.ServletRegistrationBean;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import javax.annotation.Resource;
import javax.xml.ws.Endpoint;
@Configuration
public class WebServiceConfig {
@Resource
private SmsService smsService;
@Bean(name = Bus.DEFAULT_BUS_ID)
public SpringBus springBus() {
return new SpringBus();
}
@Bean
public ServletRegistrationBean getRegistrationBean() {
return new ServletRegistrationBean(new CXFServlet(), "/webServices/*");
}
@Bean
public Endpoint messageEndPoint() {
EndpointImpl endpoint = new EndpointImpl(springBus(), this.smsService);
endpoint.publish("/smsService");
return endpoint;
}
}
3.service
package com.ruoyi.webservice;
import com.ruoyi.dto.SmsRequest;
import com.ruoyi.dto.SmsResponse;
import javax.jws.WebMethod;
import javax.jws.WebParam;
import javax.jws.WebService;
@WebService(name = "smsService", targetNamespace = "http://tempuri.org")
public interface SmsService {
@WebMethod(action = "http://tempuri.org/sendSms" ,operationName = "sendSms")
SmsResponse sendSms(@WebParam(name = "sysname") String sysname,
@WebParam(name = "mobiles") String mobiles,
@WebParam(name = "content") String content);
}
4.serviceimpl
package com.ruoyi.webservice.impl;
import com.ruoyi.dto.SmsResponse;
import com.ruoyi.webservice.SmsService;
import org.springframework.http.HttpStatus;
import org.springframework.stereotype.Service;
import javax.jws.WebParam;
import javax.jws.WebService;
@Service
@WebService(name = "smsService", targetNamespace = "http://tempuri.org")
public class SmsServiceImpl implements SmsService {
@Override
public SmsResponse sendSms(@WebParam(name = "sysname") String sysname,
@WebParam(name = "mobiles") String mobiles,
@WebParam(name = "content") String content) {
SmsResponse response = new SmsResponse();
response.setCode(HttpStatus.OK.value());
response.setMessage("短信发送成功");
return response;
}
}
5.测试类
package com.ruoyi.test;
import cn.hutool.http.HttpRequest;
import cn.hutool.http.HttpResponse;
import cn.hutool.json.JSONUtil;
public class SmsServiceTest {
private static final String ADDRESS = "http://localhost:8080/webServices/smsService";
private static final String SOAP_ACTION = "sendSms";
private static final String NAMESPACE = "http://tempuri.org";
public static void main(String[] args) {
try {
HttpRequest request = HttpRequest.post(ADDRESS);
String soapBody = "<?xml version=\"1.0\" encoding=\"utf-8\"?>" +
"<soap:Envelope xmlns:soap=\"http://schemas.xmlsoap.org/soap/envelope/\" " +
"xmlns:sns=\"" + NAMESPACE + "\">" +
"<soap:Body>" +
"<sns:" + SOAP_ACTION + ">" +
"<sysname>您的系统名称</sysname>" +
"<mobiles>13800000000</mobiles>" +
"<content>这是一条测试短信</content>" +
"</sns:" + SOAP_ACTION + ">" +
"</soap:Body>" +
"</soap:Envelope>";
request.header("Content-Type", "text/xml; charset=utf-8")
.header("SOAPAction", NAMESPACE + "/" + SOAP_ACTION);
request.body(soapBody);
HttpResponse response = request.execute();
String responseContent = response.body();
System.out.println(responseContent);
String jsonResponse = JSONUtil.parse(responseContent).toString();
System.out.println(jsonResponse);
} catch (Exception e) {
e.printStackTrace();
}
}
}