springboot webservice 服务端 实例

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软件进行测试

 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值