SpringBoot整合WebService实例

webservice

发布webservice中所有非静态的方法都会被发布,final和静态方法不会对外发布。

自定义webservice服务端

1.首先引入maven依赖

<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.定义Cxf配置类

@Configuration
public class SapCxfConfig {

    //第二个参数url跟在服务器端口后面
    @Bean
    public ServletRegistrationBean dispatcherServlet() {
        return new ServletRegistrationBean(new CXFServlet(), "/webservce/*");
    }
    @Bean(name = Bus.DEFAULT_BUS_ID)
    public SpringBus springBus() {
        return new SpringBus();
    }

    @Bean
    public ISapPushExpenseWebservice sapPushExpenseWebservice() {
        return new SapPushExpenseWebservice();
    }
    
    //sapPushExpenseWebservice你要对外开放的对象
    @Bean
    public Endpoint endpoint() {
        EndpointImpl endpoint = new EndpointImpl(springBus(), sapPushExpenseWebservice());
        //publish()进行对外发布,里面的api跟在上面webservice参数的后面
        endpoint.publish("/api");
        return endpoint;
    }

}]

service类

@WebService(name = "SapPushExpenseWebservice")
public interface ISapPushExpenseWebservice {
    
    @WebMethod(operationName="sayHello") //相当于为这个方法制定别名,在wsdl节点上就会显示该方法名称为sayHello
    @WebMethod(exclude=true) //设置为true的情况下,该方法不会对外进行发布
    @WebResult(name="address") //表明方法的返回值名称,在发布wsdl中可以看到
    //@WebParam(name="address")该注解表示给这个参数定义名称,否则wsdl默认显示arg0
    String pushExpense(@WebParam(name="address")String address);

}

@WebService(name = "SapPushExpenseWebservice")
@Service
public class SapPushExpenseWebservice implements ISapPushExpenseWebservice {

    private static Logger logger = LoggerFactory.getLogger(SapPushExpenseWebservice.class);

    @Override
    public String pushExpense(String address) {
        logger.info(JSON.toJSONString(address));
        return address;
    }

}

上述注解详情参考这位博主的解说:
https://blog.csdn.net/cherry_11qianqian/article/details/82662315
https://blog.csdn.net/cherry_11qianqian/article/details/81536727

3.启动项目就可以看到去访问了,我是本地访问http://localhost:8010/webservice
address:显示我本地服务端的地址
wsdl:显示我这个服务端开放对象的文件夹,它这个是反过来的
namespace:显示的对象的文件夹
在这里插入图片描述

打开页面可以看到下面有wsdl的地址,点击跳转即可!!!

在这里插入图片描述

portType节点展示的是你对外开放的对象和方法名称

<?xml version='1.0' encoding='UTF-8'?>
<wsdl:definitions xmlns:xsd="http://www.w3.org/2001/XMLSchema" 
xmlns:wsdl="http://schemas.xmlsoap.org/wsdl/" 
xmlns:tns="http://impl.service.webservice_server.gongj.com/" 
xmlns:soap="http://schemas.xmlsoap.org/wsdl/soap/" 
xmlns:ns1="http://schemas.xmlsoap.org/soap/http" name="UserServerImplService" 
targetNamespace="http://impl.service.webservice_server.gongj.com/">


<!-- web service 使用的数据类型 -->
  <wsdl:types>
<xs:schema xmlns:xs="http://www.w3.org/2001/XMLSchema" xmlns:tns="http://impl.service.webservice_server.gongj.com/" elementFormDefault="unqualified" targetNamespace="http://impl.service.webservice_server.gongj.com/" version="1.0">

  <xs:element name="getUser" type="tns:getUser"/>

  <xs:element name="getUserResponse" type="tns:getUserResponse"/>

<!-- getUser 方法的请求参数类型 -->
  <xs:complexType name="getUser">
    <xs:sequence>
      <xs:element minOccurs="0" name="arg0" type="xs:long"/>
    </xs:sequence>
  </xs:complexType>

<!-- getUser 方法的响应参数类型 -->
  <xs:complexType name="getUserResponse">
    <xs:sequence>
      <xs:element minOccurs="0" name="return" type="tns:userDTO"/>
    </xs:sequence>
  </xs:complexType>

<!-- getUser 方法的响应参数的具体类型 -->
  <xs:complexType name="userDTO">
    <xs:sequence>
      <xs:element minOccurs="0" name="address" type="xs:string"/>
      <xs:element minOccurs="0" name="age" type="xs:int"/>
      <xs:element minOccurs="0" name="id" type="xs:long"/>
      <xs:element minOccurs="0" name="name" type="xs:string"/>
    </xs:sequence>
  </xs:complexType>
</xs:schema>
  </wsdl:types>
  
  <!-- 
        message:用来定义soap消息结构
        part:引用上面 types 中的约束格式
 -->
  <wsdl:message name="getUser">
    <wsdl:part element="tns:getUser" name="parameters">
    </wsdl:part>
  </wsdl:message>
  <wsdl:message name="getUserResponse">
    <wsdl:part element="tns:getUserResponse" name="parameters">
    </wsdl:part>
  </wsdl:message>
  
  <!-- 
    portType:用来指定服务器端的接口
    operation:接口中定义的方法
    input:方法getUser的输入
    output:方法getUser的输出
    输入输出引用的是上面message的定义
     -->
  <wsdl:portType name="UserServerImpl">
    <wsdl:operation name="getUser">
      <wsdl:input message="tns:getUser" name="getUser">
    </wsdl:input>
      <wsdl:output message="tns:getUserResponse" name="getUserResponse">
    </wsdl:output>
    </wsdl:operation>
  </wsdl:portType>
  
  <!-- 
    type属性:引用<portType>的定义
    <soap:binding style="document">:表示传输的一个document (xml)
    <input><output>方法的输入、输出
    <soap:body use="literal" />:表示body传输采用文本即xml格式的文本
-->
  <wsdl:binding name="UserServerImplServiceSoapBinding" type="tns:UserServerImpl">
    <soap:binding style="document" transport="http://schemas.xmlsoap.org/soap/http"/>
    <wsdl:operation name="getUser">
      <soap:operation soapAction="" style="document"/>
      <wsdl:input name="getUser">
        <soap:body use="literal"/>
      </wsdl:input>
      <wsdl:output name="getUserResponse">
        <soap:body use="literal"/>
      </wsdl:output>
    </wsdl:operation>
  </wsdl:binding>
  
  <!-- 
    name:用于指定客户端的容器类/工厂类
     binding:引用上面的 binding 标签
     port name:容器通过这个方法获得实现类,自动生成会出现这个类,可以不用管
     address:客户端真正用于请求的地址
     -->
  <wsdl:service name="UserServerImplService">
    <wsdl:port binding="tns:UserServerImplServiceSoapBinding" name="UserServerImplPort">
      <soap:address location="http://localhost:8080/webservice/api"/>
    </wsdl:port>
  </wsdl:service>
  
</wsdl:definitions>

webservice客户端

首先引入依赖:这个依赖跟上面服务端依赖一致哦!!!

 <!-- https://mvnrepository.com/artifact/org.apache.cxf/cxf-rt-frontend-jaxws -->
        <dependency>
            <groupId>org.apache.cxf</groupId>
            <artifactId>cxf-rt-frontend-jaxws</artifactId>
            <version>3.1.9</version>
        </dependency>
        <dependency>
            <groupId>org.apache.cxf</groupId>
            <artifactId>cxf-rt-transports-http</artifactId>
            <version>3.1.9</version>
        </dependency>

1.简单的写法

private static Logger logger= LoggerFactory.getLogger(DemoApplication.class);

public static void main(String[] args){
    JaxWsDynamicClientFactory dcf = JaxWsDynamicClientFactory.newInstance();
	Client client = dcf.createClient("http://localhost:8010/webservice/api?wsdl");

        try {
            // invoke("调用服务端某个方法的方法名",传递的参数1,参数2,参数3....);
            Object[] objects = client.invoke("pushExpense", "上海市");
           logger.info("输出内容:"+objects);
        } catch (java.lang.Exception e) {
            e.printStackTrace();
        }
}

//输出:输出内容:上海市

2.可以把对象进行简单封装

①属性类

@Data
public class WebClientConfig {
    //路径url
    private String url;
    //对应要调用的方法名
    private String method;
    //设置是否开启这个对象的调用
    private boolean enabled;
}

②客户端调用的公共配置类

@ConfigurationProperties(prefix = "test-web-service")
@Component
public class CustomWebServiceClient {
    private Logger logger= LoggerFactory.getLogger(CustomWebServiceClient.class);

    public void setConfigs(Map<String, WebClientConfig> configs) {
        this.configs = configs;
    }
	//存储读取配置文件的数据对象
    private Map<String,WebClientConfig> configs;

    JaxWsDynamicClientFactory dcf = JaxWsDynamicClientFactory.newInstance();

    public Object[] sendAdnReceiveInfo(String configId,Object... data){
        //根据每个不同的调用对象传递调用参数,获取url等参数
        WebClientConfig clientConfig=configs.get(configId);
        if(Objects.isNull(clientConfig)||!clientConfig.isEnabled()){
            logger.info("webservice连接对象为null,或者连接配置未开启!");
            return null;
        }
        Object[] obj;
        String wsdUrl=clientConfig.getUrl();
        try {
            //获取cxf客户端
            Client client=dcf.createClient(wsdUrl);
            //第一个参数是被调用方的方法名,后面参数是传递过去的值
            obj=client.invoke(clientConfig.getMethod(),data);
            System.out.println("输出内容:"+obj);

        }catch (Exception e){
            throw new RuntimeException(e);
        }finally {
            logger.info("关闭了!!!");
        }
        return obj;
    }
}

③xml配置

test-web-service:
  configs:
    TEST_WEB:
      enabled: true
      url: "http://localhost:8010/webservice/api?wsdl"
      method: "pushExpense"

③调用方法

@Autowirde
private CustomWebServiceClient customWebServiceClient;

public String send() {
    //第一个参数为调用配置文件的那个对象key,第二个参数就是传递的值啦,也可以传多个值,就完成啦!!!
      String address= customWebServiceClient.sendAdnReceiveInfo("TEST_WEB","上海市");
       sout(address);
}


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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值