栏目分类:
子分类:
返回
名师互学网用户登录
快速导航关闭
当前搜索
当前分类
子分类
实用工具
热门搜索
名师互学网 > IT > 软件开发 > 后端开发 > Java

WebService接口发布和调用

Java 更新时间: 发布时间: IT归档 最新发布 模块sitemap 名妆网 法律咨询 聚返吧 英语巴士网 伯小乐 网商动力

WebService接口发布和调用

首先引入apache.cxf包maven坐标

        
            org.apache.cxf
            cxf-spring-boot-starter-jaxws
            3.3.4
        
发布WebService接口

接口定义

import javax.jws.WebMethod;
import javax.jws.WebParam;
import javax.jws.WebService;


@WebService(name = "xzhWebService", targetNamespace = "http://webservice.xzh.com")
public interface XzhWebService {

    @WebMethod
    String xzhMethod(@WebParam(name = "xmlContent", targetNamespace = "http://webservice.xzh.com") String xmlContent);
}

实现类

public class XzhWebServiceImpl implements XzhWebService {

    @Override
    public String xzhMethod(String xmlContent) {
        return "success → " + xmlContent;
    }
}

配置

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.boot.web.servlet.ServletRegistrationBean;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;

import javax.xml.ws.Endpoint;


@Configuration
public class WebServiceConfig {

    @Bean
    public ServletRegistrationBean registration() {
        // 注册servlet bean组件,并在路径上添加/ws(不是必须)
        return new ServletRegistrationBean(new CXFServlet(), "/ws/*");
    }

    @Bean(name = Bus.DEFAULT_BUS_ID)
    public SpringBus springBus() {
        return new SpringBus();
    }

    @Bean
    public XzhWebService webService() {
        return new XzhWebServiceImpl();
    }

    @Bean
    public Endpoint endpoint() {
        EndpointImpl endpoint = new EndpointImpl(springBus(), webService());
        // 发布的服务地址
        endpoint.publish("/xzh");
        return endpoint;
    }
}
调用WebService接口

调用实际是个http请求,但是参数外层需要包装一层xml,我这里直接采用hutool工具包直接调用:

Hutool参考文档

    public static void main(String[] args) {
        // 发送请求
        SoapClient client = SoapClient.create("http://localhost:8596/bop-oms/ws/xzh")
                .setMethod("xzhMethod", "http://webservice.xzh.com")
                .setParam("xmlContent", "hello world!");
        String res = client.send(true);
        System.out.println(res);
    }

可以看到打印的res接口为:



  
    
      success → hello world!
    
  

采用Postman调用验证也能得到相同结果:

转载请注明:文章转载自 www.mshxw.com
本文地址:https://www.mshxw.com/it/985313.html
我们一直用心在做
关于我们 文章归档 网站地图 联系我们

版权所有 (c)2021-2022 MSHXW.COM

ICP备案号:晋ICP备2021003244-6号