一个简单的webservice的发布和调用

导入pom坐标

<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0"
         xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
         xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
    <parent>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-parent</artifactId>
        <version>2.0.3.RELEASE</version>
        <relativePath/>
    </parent>
    <modelVersion>4.0.0</modelVersion>


    <groupId>com.lkw</groupId>
    <artifactId>testWebService</artifactId>
    <version>1.0-SNAPSHOT</version>

    <properties>
        <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
        <project.reporting.outputEncoding>UTF-8</project.reporting.outputEncoding>
        <java.version>1.8</java.version>
    </properties>

    <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>
        </dependency>
        <dependency>
            <groupId>org.projectlombok</groupId>
            <artifactId>lombok</artifactId>
        </dependency>
        <dependency>
            <groupId>com.google.code.gson</groupId>
            <artifactId>gson</artifactId>
        </dependency>
        <dependency>
            <groupId>org.apache.cxf</groupId>
            <artifactId>cxf-spring-boot-starter-jaxws</artifactId>
            <version>3.2.5</version>
        </dependency>
    </dependencies>

    <build>
        <plugins>
            <plugin>
                <groupId>org.springframework.boot</groupId>
                <artifactId>spring-boot-maven-plugin</artifactId>
            </plugin>
        </plugins>
    </build>


</project>

注意:这里webservice的版本和springboot要注意,不然是会报错的 

编写webservice的配置类

package com.lkw.config;

import com.lkw.service.PersonService;
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.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 WebServiceConfig{

    @Autowired
    private PersonService personService;

    /**
     * 注入servlet  bean name不能dispatcherServlet 否则会覆盖dispatcherServlet
     *
     * @return
     */
    @Bean
    public ServletRegistrationBean cxfServlet() {
        return new ServletRegistrationBean(new CXFServlet(), "/webservice/*");
    }


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

    /**
     * 注册WebServiceDemoService接口到webservice服务
     *
     * @return
     */
    @Bean
    public Endpoint endpoint() {
        EndpointImpl endpoint = new EndpointImpl(springBus(), personService);
        endpoint.publish("/webservice");
        return endpoint;
    }

}

编写接口

package com.lkw.service;


import javax.jws.WebService;

@WebService
public interface PersonService {

    String sayHellow();
}

编写实现类

这里我们使用gson的json工具类来帮我们返回一个json字符串

package com.lkw.service.impl;

import com.google.gson.Gson;
import com.lkw.entity.Person;
import com.lkw.service.PersonService;
import org.springframework.stereotype.Component;

import javax.jws.WebService;
import java.util.Arrays;
import java.util.List;

/**
 * @Description:
 * serviceName:  //发布的服务名称
 * targetNamespace:  //接口包名的倒写
 * endpointInterface:   //接口的包全名
 */
@Component
@WebService(serviceName = "testWebService",  
        targetNamespace = "http://service.lkw.com/",    
        endpointInterface = "com.lkw.service.PersonService")    
public class PersonServiceImpl implements PersonService {


    private List<Person> personList = Arrays.asList(
            new Person("1", "张三", "24"),
            new Person("2", "李四", "24"),
            new Person("3", "王五", "24"),
            new Person("4", "赵六", "24"),
            new Person("5", "田七", "24")

    );


    @Override
    public String sayHellow() {
        Gson gson = new Gson();
        String s = gson.toJson(personList);
        return s;
    }
}

启动springboot工程

 然后访问 http://localhost:8080/webservice/webservice?wsdl

接下来发布成功,我们调用发布的webservice

编写测试类调用接口,这里我们使用反射的来调用

    @Test
    public void test1() {

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

        // 需要密码的情况需要加上用户名和密码
        // client.getOutInterceptors().add(new ClientLoginInterceptor(USER_NAME,PASS_WORD));

        try {
            Object[] sayHellows = client.invoke("sayHellow");
            System.out.println(sayHellows[0]);
            String sayHellow = (String) sayHellows[0];
            Gson gson = new Gson();
            List<Person> personList = gson.fromJson(sayHellow, new TypeToken<List<Person>>() {
            }.getType());

            System.out.println("返回数据:" + personList);
        } catch (java.lang.Exception e) {
            e.printStackTrace();
        }
    }

启动测试类调用,可以看到调用成功,并借助gson的工具类帮我们转成我们所需要的实体

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值