SpringBoot 创建 WebService

本文介绍了在IDEA2022.1.4环境下,通过SpringBoot项目集成ApacheCXF创建WebService的步骤,包括添加依赖、定义接口、实现接口、配置类和测试。测试通过SoapUI进行,数据获取正常。
摘要由CSDN通过智能技术生成

开发环境: IDEA 2022.1.4

目录

1. 概述

2. 实现步骤

        2.1 POM现加依赖

        2.2  定义接口 IWebService

        2.3 创建类 IWebServiceImpl 并实现接口 IWebService

        2.4  配置类  CxfConfig

        2.5 启动服务

         2.6 测试


1. 概述

        虽然webservice这块使用很少,但在局域网作服务还是相当不错。今天突生想法,想做一个来试试,查阅百度资料,亲手实践下。方便操作,便在现有得SpringBoot项目上,来实践下,也顺便试试Spring web服务与webservice功能是否可以一起发布。

        我主要添加了三个java文件,一个接口IWebService,一个接口实现类IWebServiceImpl,一个配置类CxfConfig。

        

2. 实现步骤

        2.1 POM现加依赖

        <!-- WebSevice -->
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-web-services</artifactId>
        </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>

        2.2  定义接口 IWebService

@WebService(name = "WebService", targetNamespace = "service.xtjk.ceaning.com")
@BindingType(SOAPBinding.SOAP12HTTP_BINDING)
public interface IWebService {
    @WebMethod
    String BedInfo() throws JsonProcessingException;
    @WebMethod
    Result<?> DeptInfo();
    @WebMethod
    Result<?> UserInfo();
    @WebMethod
    Result<?> WardInfo();
    @WebMethod
    Result<?> OrderInfo(@WebParam(name = "param") String param);
    @WebMethod
    Result<?> InPatiengInfo(@WebParam(name = "param") String param);
    @WebMethod
    Result<?> SaveGlu(@WebParam(name = "param") String param);
}

        2.3 创建类 IWebServiceImpl 并实现接口 IWebService

@Slf4j
@Component
@WebService(name = "WebService",
        targetNamespace = "service.xtjk.ceaning.com",
        endpointInterface = "com.ceaning.xtjk.service.IWebService")
public class IWebServiceImpl implements IWebService {
    @Autowired
    private DictionaryMapper dictionaryMapper;
    @Autowired
    private OrderMapper orderMapper;
    @Autowired
    private InPatientMapper inPatientMapper;
    @Autowired
    private ResultMapper resultMapper;

    @Override
    public String BedInfo() throws JsonProcessingException {
        ObjectMapper obj= new ObjectMapper();
        List<Bed> list= null;
        try{
            list= dictionaryMapper.getBedInfo();
            return obj.writeValueAsString(Result.ok(list));
        } catch (Exception e){
            log.error(e.getMessage());
            return obj.writeValueAsString(Result.exception(e.getMessage()));
        }
    }

    @Override
    public String DeptInfo() throws JsonProcessingException {
        ObjectMapper obj= new ObjectMapper();
        List<Dept> list= null;
        try{
            list= dictionaryMapper.getDeptInfo();
            return obj.writeValueAsString(Result.ok(list));
        } catch (Exception e){
            log.error(e.getMessage());
            return obj.writeValueAsString(Result.exception(e.getMessage()));
        }
    }

    @Override
    public String UserInfo() throws JsonProcessingException {
        ObjectMapper obj= new ObjectMapper();
        List<User> list= null;
        try{
            list= dictionaryMapper.getUserInfo();
            return obj.writeValueAsString(Result.ok(list));
        } catch (Exception e){
            log.error(e.getMessage());
            return obj.writeValueAsString(Result.exception(e.getMessage()));
        }
    }

    @Override
    public String WardInfo() throws JsonProcessingException {
        ObjectMapper obj= new ObjectMapper();
        List<Ward> list= null;
        try{
            list= dictionaryMapper.getWardInfo();
            return obj.writeValueAsString(Result.ok(list));
        } catch (Exception e){
            log.error(e.getMessage());
            return obj.writeValueAsString(Result.exception(e.getMessage()));
        }
    }

    @Override
    public String OrderInfo(String param) throws JsonProcessingException {
        ObjectMapper mapper= new ObjectMapper();
        List<Order> list= null;
        try{
            OrderParam obj= mapper.readValue(param, OrderParam.class);
            list= orderMapper.getList(obj);
            return mapper.writeValueAsString(Result.ok(list));
        } catch (Exception e){
            log.error(e.getMessage());
            return mapper.writeValueAsString(Result.exception(e.getMessage()));
        }
    }

    @Override
    public String InPatiengInfo(String param) throws JsonProcessingException {
        ObjectMapper mapper= new ObjectMapper();
        List<InPatient> list= null;
        try{
            InPatientParam obj= mapper.readValue(param, InPatientParam.class);
            list= inPatientMapper.getList(obj);
            return mapper.writeValueAsString(Result.ok(list));
        } catch (Exception e){
            log.error(e.getMessage());
            return mapper.writeValueAsString(Result.exception(e.getMessage()));
        }
    }

    @Override
    public String SaveGlu(String param) throws JsonProcessingException {
        ObjectMapper mapper= new ObjectMapper();
        DbResult ret= null;
        try{
            ResultParam obj= mapper.readValue(param, ResultParam.class);
            ret= resultMapper.save(obj);
            if (ret.getCode().trim().equals("T")){
                return mapper.writeValueAsString(Result.ok(ret.getMessage()));
            }
            else{
                return mapper.writeValueAsString(Result.error(ret.getMessage()));
            }
        } catch (Exception e){
            log.error(e.getMessage());
            return mapper.writeValueAsString(Result.exception(e.getMessage()));
        }
    }
}

        2.4  配置类  CxfConfig

@Configuration
public class CxfConfig {
    @Autowired
    private IWebService service;

    @Bean
    public ServletRegistrationBean disServlet(){
        return new ServletRegistrationBean(new CXFServlet(), "/open/xtjk/*");
    }
    @Bean(name = Bus.DEFAULT_BUS_ID)
    public SpringBus springBus(){
        return new SpringBus();
    }
    @Bean
    public Endpoint endpoint(){
        EndpointImpl endpoint= new EndpointImpl(springBus(), service);
        endpoint.publish("/IWebService");
        return endpoint;
    }
}

        2.5 启动服务

        浏览器访问: http://127.0.0.1:8090/open/xtjk/IWebService?wsdl

        需要注意URL路径和CxfConfig得配置关联

         2.6 测试

        我使用另一个电脑安装得SoapUI进行测试,数据获取正常。就不截图了。

篇幅有限,就不罗列其他得了。Springboot相关知识网上很多。

评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值