springboot实现webservice的发布和调用

springboot使用cxf发布调用webservice
发布webservice
pom文件

<dependency>
            <groupId>org.apache.cxf</groupId>
            <artifactId>cxf-spring-boot-starter-jaxws</artifactId>
            <version>3.2.5</version>
        </dependency>

webservice接口

package com.example.webservicedemo.fabu;



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


@WebService
public interface TestService {
    @WebMethod
    String getUserName(@WebParam(name = "id") String id);


}

实现

//name暴露服务名称, targetNamespace:命名空间,设置为接口的包名倒写(默认是本类包名倒写). endpointInterface接口地址
@WebService(name = "test" ,targetNamespace ="http://fabu.webservicedemo.example.com/" ,endpointInterface = "com.example.webservicedemo.fabu.TestService")
@Component
public class TestServiceImpl implements TestService {
    @Override
    public String getUserName(String id)  {
        return "hello";
    }
 
}

配置类

package com.example.webservicedemo.fabu;

import org.apache.cxf.Bus;
import org.apache.cxf.jaxws.EndpointImpl;
import org.apache.cxf.transport.servlet.CXFServlet;
import org.springframework.beans.factory.annotation.Autowired;
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 WSConfig {
    @Autowired
    private Bus bus;
    @Autowired
    private TestService testService;
    //这里需要注意  由于springmvc 的核心类 为DispatcherServlet
    //此处若不重命名此bean的话 原本的mvc就被覆盖了。可查看配置类:DispatcherServletAutoConfiguration
    //一种方法是修改方法名称 或者指定bean名称
    //这里需要注意 若beanName命名不是 cxfServletRegistration 时,会创建两个CXFServlet的。
    //具体可查看下自动配置类:Declaration org.apache.cxf.spring.boot.autoconfigure.CxfAutoConfiguration
    //也可以不设置此bean 直接通过配置项 cxf.path 来修改访问路径的
    @Bean("cxfServletRegistration")
    public ServletRegistrationBean dispatcherServlet(){
        return new ServletRegistrationBean(new CXFServlet(),"/businessSystem/services/*");//发布服务名称
    }

    //终端路径
    @Bean
    public Endpoint endpoint() {
        EndpointImpl endpoint = new EndpointImpl(bus,testService);
        endpoint.publish("/testWS");
        return endpoint;
    }
}

尽量通过aplication.properties的cxf.path=/businessSystem/services/来配置,省略掉上面配置类的ServletRegistrationBean
aplication.properties

server.port=8082
cxf.path=/businessSystem/services/

最后访问地址
http://localhost:8082/businessSystem/services/testWS?wsdl

访问地址为:IP+配置的端口+cxf.path+ endpoint.publish的路径

webservice的调用
直接调用,省心
pom

 <dependency>
            <groupId>org.apache.cxf</groupId>
            <artifactId>cxf-spring-boot-starter-jaxws</artifactId>
            <version>3.2.5</version>
        </dependency>

wsimport -keep -p com.example.call http://localhost:8082/businessSystem/services/testWS?wsdl
-keep 生成java文件源码
-p 需要和放的地方的包路径一致,在哪个包下面放,包名就是那个包的路径
生成如下的类,将java文件放在-p配置的包下
在这里插入图片描述
调用代码

public static void main(String[] args) {
//TestServiceImplService testServiceImplService = new TestServiceImplService(new URL("http://localhost:8084/businessSystem/services/testWS?wsdl"));    可以传入URL,用于不同环境下的访问
        TestServiceImplService testServiceImplService = new TestServiceImplService();
        TestService testService = testServiceImplService.getTestPort();
        String aaa = testService.getUserName("aaa");
        System.out.println(aaa);
    }

其他调用发布webservice方式
axis
下载工具
https://archive.apache.org/dist/ws/axis/1_4/
此工具主要是生成客户端调用的java代码
解压后在同级目录创建xx.bat
在这里插入图片描述
内容如下:

//axis的lib的地址
set axis_lib=D:\webservice\axis-1_4\lib  

set java_cmd=java -Djava.ext.dirs=%axis_lib%
//wsdl地址
set axis_servlet=xxx:8080/ormrpc/services/EASLogin?wsdl
//包地址
set Package=com.example.jd
%java_cmd% org.apache.axis.wsdl.WSDL2Java -p%Package% -u %axis_servlet%

双击生成代码,将代码放入项目中上述命令-p指定的包下
调用前需要引入依赖

<!-- axis 1.4 jar start -->
        <dependency>
            <groupId>org.apache.axis</groupId>
            <artifactId>axis</artifactId>
            <version>1.4</version>
        </dependency>
        <!-- 日志引入  -->
        <dependency>
            <groupId>org.slf4j</groupId>
            <artifactId>slf4j-log4j12</artifactId>
            <version>1.7.12</version>
        </dependency>
        <dependency>
            <groupId>commons-discovery</groupId>
            <artifactId>commons-discovery</artifactId>
            <version>0.2</version>
            <exclusions>
                <exclusion>
                    <groupId>commons-logging</groupId>
                    <artifactId>commons-logging</artifactId>
                </exclusion>
            </exclusions>
        </dependency>
        <dependency>
            <groupId>org.apache.axis</groupId>
            <artifactId>axis-jaxrpc</artifactId>
            <version>1.4</version>
        </dependency>
        <dependency>
            <groupId>org.apache.axis</groupId>
            <artifactId>axis-saaj</artifactId>
            <version>1.4</version>
        </dependency>
        <dependency>
            <groupId>wsdl4j</groupId>
            <artifactId>wsdl4j</artifactId>
            <version>1.4</version>
        </dependency>
        <!-- axis 1.4 jar end -->

调用代码如下

public class JDTest {
//最好是这种调用方式,以接口的形式调用
public WSContext login(String userName, String password, String slnName, String dcName, String language, int dbType) throws Exception{
        EASLoginProxy loginProxy = new EASLoginProxyServiceLocator().getEASLogin(new URL(urlPre + "/ormrpc/services/EASLogin?wsdl"));
        return loginProxy.login(userName, password, slnName, dcName, language, dbType);
    }
    //不推荐
    public static void main(String[] args) throws MalformedURLException, RemoteException {
        URL url = new URL("xxx:8080/ormrpc/services/EASLogin");
        EASLoginSoapBindingStub stub = new EASLoginSoapBindingStub(url, new Service());
        WSContext wsContext = stub.login("te001", "123", "effs", "test1", "2", 2, "ss");
        System.out.println(wsContext.getSessionId());
    }
}
  • 0
    点赞
  • 4
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值