• springBoot如何编写一个webService服务呢?


    转自:

    springBoot如何编写一个webService服务呢?

    下文笔者讲述使用springBoot编写一个webService的方法分享,如下所示:

    pom中加入webservice依赖-开启指定的starter器

    
            org.apache.cxf
            cxf-spring-boot-starter-jaxws
            3.2.5
        
     

    java后台对外暴露webService

    接口定义
    import com.comjava265.model.AchieveExpress;
    import javax.jws.WebMethod;
    import javax.jws.WebParam;
    import javax.jws.WebResult;
    import javax.jws.WebService;
    import java.util.List;
    
    @WebService(name = "MaoMaoService", // 对外的服务名称
            targetNamespace = "http://service.web.java265.com") //命名空间,一般是接口的包名倒序
    public interface MaoMaoService {
    
        @WebMethod
        @WebResult(name = "String",targetNamespace = "")
        List findByName(@WebParam(name = "name") String name);
    
    } 
    
    
    --具体的实现类 
    import org.springframework.beans.factory.annotation.Autowired;
    import org.springframework.data.mongodb.core.MongoTemplate;
    import org.springframework.data.mongodb.core.query.Criteria;
    import org.springframework.data.mongodb.core.query.Query;
    import org.springframework.stereotype.Component;
    
    import javax.jws.WebService;
    import java.util.List;
    
    @WebService(serviceName = "MaoMaoService",//与前面接口一致
            targetNamespace = "http://service.web.java265.com",  //与前面接口一致
            endpointInterface = "com.java265.web.service.MaoMaoService")  //接口地址
    @Component
    public class MaoMaoServiceImpl implements MaoMaoService {
    
        @Autowired
        UserService userService;
        
        @Override
        public List findByName(String name) {
            List res= userService.findByName(name);
            return res;
        }
    
    } 
    
    ------服务端发布相关的接口(可发布多个)
    import org.apache.cxf.Bus;
    import org.apache.cxf.jaxws.EndpointImpl;
    import org.springframework.beans.factory.annotation.Autowired;
    import org.springframework.context.annotation.Bean;
    import org.springframework.context.annotation.Configuration;
    
    import javax.xml.ws.Endpoint;
    
    /**
     * webservice
     */
    @Configuration
    public class WebConfig {
    
        @Autowired
        private Bus bus;
    
        @Autowired
        MaoMaoService maoMaoService;
    
        @Autowired
        AchieveExpressService achieveExpressService;
    
        @Bean
        public Endpoint endpoint() {
            EndpointImpl endpoint = new EndpointImpl(bus, maoMaoService);
            endpoint.publish("/MaoMaoService");
            return endpoint;
        }
    
        @Bean
        public Endpoint endpoint2() {
            EndpointImpl endpoint = new EndpointImpl(bus, maoMaoService);
            endpoint.publish("/MaoMaoService2");
            return endpoint;
        }
    }
    

    采用以上方式,即可完成webService的发布

  • 相关阅读:
    JVM(三) 垃圾回收
    如何创建一个SpringBoot项目
    Mysql 数据恢复逻辑 基于binlog redolog undolog
    rsync远程同步
    回收站里面删除的照片如何恢复?
    防火墙部署模式 -- 镜像流量(旁路模式)
    MySQL索引优化实战指南(InsCode AI 创作助手)
    系列七、线程8锁
    【校招VIP】前端算法考察之字符串
    深入理解 Jetpack Compose 内核:SlotTable 系统
  • 原文地址:https://blog.csdn.net/qq_25073223/article/details/127917122