1、增加maven依赖
<!--webservice接口-->
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web-services</artifactId>
</dependency>
<dependency>
<groupId>org.apache.cxf</groupId>
<artifactId>cxf-spring-boot-starter-jaxws</artifactId>
<version>3.2.7</version>
</dependency>
2、增加webservice配置类
import com.anxhit.mwm_manager.service.webservice.IRegisterApiService;
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.beans.factory.annotation.Autowired;
import org.springframework.boot.web.servlet.ServletRegistrationBean;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import javax.xml.ws.Endpoint;
@Configuration
public class WebServiceConfig {
@Autowired
private IRegisterApiService registerApiService;
/**
* 注入servlet bean name不能dispatcherServlet 否则会覆盖dispatcherServlet
*
* @return
*/
@Bean(name = "cxfServlet")
public ServletRegistrationBean cxfServlet() {
return new ServletRegistrationBean(new CXFServlet(), "/ws/*");
}
@Bean(name = Bus.DEFAULT_BUS_ID)
public SpringBus springBus() {
return new SpringBus();
}
/**
* 注册WebServiceDemoService接口到webservice服务
*
* @return
*/
@Bean(name = "RegisterApiServiceEndpoint")
public Endpoint sweptPayEndpoint() {
EndpointImpl endpoint = new EndpointImpl(springBus(), registerApiService);
endpoint.publish("/getName");
return endpoint;
}
}
3、增加提供服务的接口和接口实现类
这里接口的命名空间targetNamespace
必须要有,没有的话报错
import com.anxhit.mwm_manager.service.bo.RegisterBO;
import javax.jws.WebMethod;
import javax.jws.WebParam;
import javax.jws.WebService;
@WebService
public interface IRegisterApiService {
@WebMethod
public RegisterBO getConfig(@WebParam(name = "verificationCode", targetNamespace = "http://webservice.service.mwm_manager.anxhit.com/") String verificationCode);
}
接口实现类中各种参数定义好
import com.anxhit.mwm_manager.persistence.entity.AuthAdmin;
import com.anxhit.mwm_manager.persistence.entity.OrgHospital;
import com.anxhit.mwm_manager.persistence.entity.Organization;
import com.anxhit.mwm_manager.service.IAccountService;
import com.anxhit.mwm_manager.service.IOrgHospitalService;
import com.anxhit.mwm_manager.service.IOrganizationService;
import com.anxhit.mwm_manager.service.bo.RegisterBO;
import com.anxhit.mwm_manager.service.webservice.IRegisterApiService;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import javax.jws.WebService;
@Service
@WebService(serviceName = "IRegisterApiService", targetNamespace = "http://webservice.service.mwm_manager.anxhit.com/",
endpointInterface = "com.anxhit.mwm_manager.service.webservice.IRegisterApiService"
)
public class RegisterApiServiceImpl implements IRegisterApiService {
@Autowired private IOrgHospitalService hospitalService;
@Autowired private IOrganizationService organizationService;
@Autowired private IAccountService accountService;
@Override
public RegisterBO getConfig(String verificationCode) {
RegisterBO registerBO = new RegisterBO();
QueryWrapper<OrgHospital> hospitalParams = new QueryWrapper<>();
hospitalParams.lambda().eq(OrgHospital::getVerificationCode,verificationCode);
OrgHospital hospital = this.hospitalService.getOne(hospitalParams);
registerBO.setHospital(hospital);
Organization organization = this.organizationService.getById(hospital.getOrgId());
registerBO.setOrganization(organization);
QueryWrapper<AuthAdmin> authAdminParams = new QueryWrapper<>();
authAdminParams.lambda().eq(AuthAdmin::getHospitalId, hospital.getId());
AuthAdmin authAdmin = this.accountService.getOne(authAdminParams);
registerBO.setAuthAdmin(authAdmin);
return registerBO;
}
}
4、启动并通过postman调试
启动后本例通地址http://localhost:20002/ws](http://localhost:20002/ws
访问,此处的ws
为配置文件中
点击链接出现服务信息
在postman中输入以下配置
POST连接,url为
http://localhost:20002/ws/getName
,可通过配置信息与服务信息查看Headers中Content-Type选择text/xml
body选择raw
<?xml version="1.0" encoding="utf-8"?>
<soap:Envelope xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:xsd="http://www.w3.org/2001/XMLSchema" xmlns:soap="http://schemas.xmlsoap.org/soap/envelope/">
<soap:Body>
<getConfig xmlns="http://webservice.service.mwm_manager.anxhit.com/">
<verificationCode>apz88z</verificationCode>
</getConfig>
</soap:Body>
</soap:Envelope>
- getConfig为方法名
- xmlns中填写targetNamespace
- verificationCode为参数名
配置完成发送请求获取报文
使用不难,难在原理呀兄弟