1.创建一个service,类似于开放一个接口的路径
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.beans.factory.annotation.Qualifier;
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 YCWebService {
@Bean
public ServletRegistrationBean wsServlet(){
return new ServletRegistrationBean(new CXFServlet(), "/ws/*");
}
@Autowired
private YCAPIWebService YCAPIWebService;
@Autowired
@Qualifier(Bus.DEFAULT_BUS_ID)
private SpringBus bus;
@Bean
public Endpoint endpoint(){
//开放服务路径 ,ws/YCAPIWebService
EndpointImpl endpoint = new EndpointImpl(bus, YCAPIWebService);
endpoint.publish("/YCAPIWebService");
return endpoint;
}
}
2.创建一个接口
import javax.jws.WebMethod;
import javax.jws.WebParam;
import javax.jws.WebResult;
import javax.jws.WebService;
//一般为包路径倒序
@WebService(targetNamespace = "http://yongchuanfuyou.hospital.epms.ambition.com")
public interface YCAPIWebService {
//提供患者服务
@WebMethod
//返回的对象
@WebResult(name = "YCResult", targetNamespace = "http://yongchuanfuyou.hospital.epms.ambition.com", partName = "ResultInfo")
YCResult patientRequest(@WebParam(name = "patient", targetNamespace = "http://yongchuanfuyou.hospital.epms.ambition.com") String patient);
}
3.接口实现
import cn.hutool.core.util.XmlUtil; import cn.hutool.json.JSONConfig; import cn.hutool.json.JSONObject; import cn.hutool.json.JSONUtil; import com.ambition.epms.model.yongChuanFuYouModel.*; import lombok.extern.slf4j.Slf4j; import org.springframework.stereotype.Component; import javax.jws.WebService; @Component @WebService(name = "YCAPIWebService", targetNamespace = "http:yongchuanfuyou.hospital.epms.ambition.com", endpointInterface = "com.ambition.epms.hospital.yongChuanfuyou.ycwebservice.YCAPIWebService", portName = "10000") @Slf4j public class YCAPIWebServiceImp implements YCAPIWebService { @Override public YCResult patientRequest(String patient) { JSONObject json = JSONUtil.parseFromXml(s1); return YCResult.successResult(1,"成功",null); } }
① 如果以对象接受参数
需要在类上添加
@XmlRootElement(name = "Request") @XmlAccessorType(XmlAccessType.FIELD)字段上加:
@XmlElement(name = "ymgl")
如果不能确定bean是否能够正常接受到xml数据,通过soapui软件进行查看此接口的xml格式
② 也可以String接受参数,但是需要在传输时添加转义符号
<![CDATA[......]]
4.通过SoapUI软件进行测试