springboot整合webservice服务发布与访问

转自:https://blog.csdn.net/qq_41027942/article/details/83149959

转自:https://blog.csdn.net/wangyuanjun008/article/details/79121687

服务发布:

pom:

<dependencies>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-web</artifactId>
        </dependency>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-web-services</artifactId>
        </dependency>

        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-test</artifactId>
            <scope>test</scope>
        </dependency>

    </dependencies>

代码:Application 类

package com.zjxt.webservicedemo;

import com.zjxt.webservicedemo.service.impl.WebServiceImpl;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;

import javax.xml.ws.Endpoint;

@SpringBootApplication
public class WebservicedemoApplication {

    public static void main(String[] args) {
        SpringApplication.run(WebservicedemoApplication.class, args);

        String url = "http://localhost:8081/wsServeice";
        Endpoint.publish(url,new WebServiceImpl());

        System.out.println("发布webService成功!");

    }
    
}

service类:

package com.zjxt.webservicedemo.service;

import javax.jws.WebMethod;

@javax.jws.WebService
public interface WebService {

    @WebMethod
    String sayHello(String user);

}
package com.zjxt.webservicedemo.service.impl;

import com.zjxt.webservicedemo.service.WebService;

import java.util.Date;

@javax.jws.WebService
public class WebServiceImpl implements WebService {
    @Override
    public String sayHello(String user) {
        return user+" sayHello at "+new Date();
    }
}

然后运行主类,启动程序,控制台打印出发布webservice成功!访问 http://localhost:8081/wsServeice?wsdl(注意一定要加wsdl),浏览器显示一个xml文档,表示webservice服务发布成功了。

服务访问:

pom:

<dependencies>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-web</artifactId>
        </dependency>

        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-test</artifactId>
            <scope>test</scope>
        </dependency>


        <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>

    </dependencies>

 

测试类:

package com.zjxt.gpsdataservice.test;

import org.apache.cxf.endpoint.Client;
import org.apache.cxf.jaxws.endpoint.dynamic.JaxWsDynamicClientFactory;

public class Test1 {
    public static void main(String[] args) {

        // 创建动态客户端
        JaxWsDynamicClientFactory dcf = JaxWsDynamicClientFactory.newInstance();
//        Client client = dcf.createClient("http://localhost:8081/wsServeice?wsdl");
        Client client = dcf.createClient("http://localhost:8081/wsServeice?wsdl");

        // 需要密码的情况需要加上用户名和密码
        // client.getOutInterceptors().add(new ClientLoginInterceptor(USER_NAME,PASS_WORD));
        Object[] objects = new Object[0];
        try {

            // invoke("方法名",参数1,参数2,参数3....);
            objects = client.invoke("sayHello", "老王");
            System.out.println("返回数据:" + objects[0]);
        } catch (java.lang.Exception e) {
            e.printStackTrace();
        }

    }
}

测试类需要与服务发布不在同一个项目中,否则会报错,目前我还没解决。

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值