springboot+cfx实现webservice功能

摘要:
1、 开发服务器1。创建新项目cfx web服务。最终完成的项目如下:pom.xml如下:˂?

一、开发服务端

1、新建工程 cfx-webservice ,最终的完整工程如下:

springboot+cfx实现webservice功能第1张

pom.xml如下:

<?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">
    <modelVersion>4.0.0</modelVersion>

    <groupId>com.cfx.simple.service</groupId>
    <artifactId>cfx-webservice</artifactId>
    <version>1.0-SNAPSHOT</version>
    <parent>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-parent</artifactId>
        <version>1.4.1.RELEASE</version>
    </parent>
    <dependencies>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-web</artifactId>
        </dependency>
        <dependency>
            <groupId>org.apache.cxf</groupId>
            <artifactId>cxf-spring-boot-starter-jaxws</artifactId>
            <version>3.1.12</version>
        </dependency>
    </dependencies>

</project>

红色部分的依赖是开发webservice的依赖包,其他的只是springboot需要的基本包

1、实体类(Student和User)

public class Student {
    private String stuName;
    private Integer stuAge;

    public Student(String stuName, Integer stuAge) {
        this.stuName = stuName;
        this.stuAge = stuAge;
    }

    public String getStuName() {
        return stuName;
    }

    public void setStuName(String stuName) {
        this.stuName = stuName;
    }

    public Integer getStuAge() {
        return stuAge;
    }

    public void setStuAge(Integer stuAge) {
        this.stuAge = stuAge;
    }
}
public class User {
    private String name;
    private String sex;

    public User(String name, String sex) {
        this.name = name;
        this.sex = sex;
    }

    public String getName() {
        return name;
    }

    public void setName(String name) {
        this.name = name;
    }

    public String getSex() {
        return sex;
    }

    public void setSex(String sex) {
        this.sex = sex;
    }

}

2、WebService的接口和实现类(一个是返回用户信息的WebService,一个是返回学生信息的WebService)

1)学生的

package com.cfx.simple.service;

import com.cfx.simple.model.Student;
import javax.jws.WebMethod;
import javax.jws.WebService;
import java.util.List;

/**
 * @author Administrator
 * @date 2019/01/30
 */
@WebService(targetNamespace = "http://service.simple.cfx.com")//  命名空间,写一个有意义的http地址就行,并不是网上所说的要写成包名倒序,只不过写成包名倒序易读而已
public interface StudentService {

    @WebMethod
    List<Student> getStudentList();
}
package com.cfx.simple.service;

import com.cfx.simple.model.Student;
import org.springframework.stereotype.Component;
import javax.jws.WebService;
import java.util.Arrays;
import java.util.List;

/**
 * @author Administrator
 * @date 2019/01/30
 */
@WebService(serviceName = "StudentService",
        targetNamespace = "http://service.simple.cfx.com",
        endpointInterface = "com.cfx.simple.service.StudentService")
@Component
public class StudentServiceImpl implements StudentService {
    @Override
    public List<Student> getStudentList() {
        Student stu1 = new Student("学生1",25);
        Student stu2 = new Student("学生2",30);
        return Arrays.asList(stu1,stu2);
    }
}

2)用户的

package com.cfx.simple.service;

import com.cfx.simple.model.User;
import javax.jws.WebMethod;
import javax.jws.WebParam;
import javax.jws.WebService;
import java.util.List;

/**
 * @author Administrator
 * @date 2019/01/30
 */
@WebService(targetNamespace = "http://service.simple.cfx.com")// 命名空间,写一个有意义的http地址就行,并不是网上所说的要写成包名倒序,只不过写成包名倒序易读而已
public interface UserService {

    @WebMethod
    List<User> getUserList(@WebParam(name = "userName") String userName);
}
package com.cfx.simple.service;

import com.cfx.simple.model.User;
import org.springframework.stereotype.Component;
import javax.jws.WebService;
import java.util.Arrays;
import java.util.List;

/**
 * @author Administrator
 * @date 2019/01/30
 */
@WebService(serviceName = "UserService",
        targetNamespace = "http://service.simple.cfx.com",
        endpointInterface = "com.cfx.simple.service.UserService")
@Component
public class UserServiceImpl implements UserService {
    @Override
    public List<User> getUserList(String userName) {
        System.out.println("输入参数:" + userName);
        User user1 = new User("张三", "男");
        User user2 = new User("李四", "男");
        return Arrays.asList(user1,user2);
    }
}

3、发布WebService的配置类

package com.cfx.simple.config;

import com.cfx.simple.service.StudentService;
import com.cfx.simple.service.UserService;
import org.apache.cxf.Bus;
import org.apache.cxf.jaxws.EndpointImpl;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;

import javax.xml.ws.Endpoint;

/**
 * @author Administrator
 * @date 2019/01/30
 */
@Configuration
public class WebServiceConfig {

    @Autowired
    private Bus bus;
    @Autowired
    private UserService userService;
    @Autowired
    private StudentService studentService;

    @Bean
    public Endpoint endpointUserService() {
        EndpointImpl endpoint = new EndpointImpl(bus,userService);
        endpoint.publish("/UserService");//接口发布在 /UserService 目录下
        return endpoint;
    }

    @Bean
    public Endpoint endpointStudentService() {
        EndpointImpl endpoint = new EndpointImpl(bus,studentService);
        endpoint.publish("/StudentService");//接口发布在 /StudentService 目录下
        return endpoint;
    }
}

4、springboot启动类

package com.cfx.simple;

import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;

/**
 * @author Administrator
 * @date 2019/01/30
 */
@SpringBootApplication
public class Application {
    public static void main(String[] args) throws Exception {
        SpringApplication.run(Application.class, args);
    }
}

5、application.yml

server:
  port: 8084
  context-path: /

启动程序,浏览器输入:http://localhost:8084/services 

springboot+cfx实现webservice功能第2张

点击WSDL

springboot+cfx实现webservice功能第3张

二、开发客户端进行测试

 1、新建cfx-webservice-client

 springboot+cfx实现webservice功能第4张

pom.xml如下:

<?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">
    <modelVersion>4.0.0</modelVersion>

    <groupId>cfx.webservice.client</groupId>
    <artifactId>cfx-webservice-client</artifactId>
    <version>1.0-SNAPSHOT</version>
    <dependencies>
        <!--调用webserivce的依赖-->
        <dependency>
            <groupId>org.apache.cxf</groupId>
            <artifactId>cxf-spring-boot-starter-jaxws</artifactId>
            <version>3.1.12</version>
        </dependency>
        <!--用于将对象转换成json-->
        <dependency>
            <groupId>net.sf.json-lib</groupId>
            <artifactId>json-lib</artifactId>
            <version>2.4</version>
            <classifier>jdk15</classifier>
        </dependency>
    </dependencies>
</project>

2、编写测试类

import net.sf.json.JSONObject;
import org.apache.cxf.endpoint.Client;
import org.apache.cxf.jaxws.endpoint.dynamic.JaxWsDynamicClientFactory;

import java.util.ArrayList;

/**
 * @author Administrator
 * @date 2019/01/30
 */
public class Test {
    public static void main(String[] args){
        //在一个方法中连续调用多次WebService接口,每次调用前需要重置上下文
        ClassLoader cl = Thread.currentThread().getContextClassLoader();

        JaxWsDynamicClientFactory dcf = JaxWsDynamicClientFactory.newInstance();
        System.out.println("学生的信息如下:================");
        printStudentList(dcf);
        //在调用第二个webservice前,需要重置上下文
        Thread.currentThread().setContextClassLoader(cl);

        System.out.println("用户的信息如下:================");
        printUserList(dcf);
    }

    private static void printUserList(JaxWsDynamicClientFactory dcf){
        Client client = dcf.createClient("http://localhost:8084/services/UserService?wsdl");
        // 需要密码的情况需要加上用户名和密码
        // client.getOutInterceptors().add(new ClientLoginInterceptor(USER_NAME, PASS_WORD));
        Object[] objects = new Object[0];
        try {
            // invoke("方法名",参数1,参数2,参数3....);
            objects = client.invoke("getUserList", "张三");
            if(objects.length>0){
                ArrayList<Object> objectList = (ArrayList)objects[0];
                for (Object o:objectList){
                    JSONObject jsonObject = JSONObject.fromObject(o);
                    System.out.println("userName:"+jsonObject.getString("name")+",sex:"+jsonObject.getString("sex"));
                }
            }
        } catch (java.lang.Exception e) {
            e.printStackTrace();
        }
    }

    private static void printStudentList(JaxWsDynamicClientFactory dcf){

        Client client = dcf.createClient("http://localhost:8084/services/StudentService?wsdl");
        // 需要密码的情况需要加上用户名和密码
        // client.getOutInterceptors().add(new ClientLoginInterceptor(USER_NAME, PASS_WORD));
        Object[] objects = new Object[0];
        try {
            // invoke("方法名",参数1,参数2,参数3....);
            objects = client.invoke("getStudentList","");
            if(objects.length>0){
                ArrayList<Object> objectList = (ArrayList)objects[0];
                for (Object o:objectList){
                    JSONObject jsonObject = JSONObject.fromObject(o);
                    System.out.println("stuName:"+jsonObject.getString("stuName")+",stuAge:"+jsonObject.getString("stuAge"));
                }
            }
        } catch (java.lang.Exception e) {
            e.printStackTrace();
        }
    }
}
学生的信息如下:================
stuName:学生1,stuAge:25
stuName:学生2,stuAge:30
用户的信息如下:================
userName:张三,sex:男
userName:李四,sex:男

上面的红色代码不能去掉,去掉后,第一个webservice调用正常,第二个会报以下错误:

springboot+cfx实现webservice功能第5张

免责声明:文章转载自《springboot+cfx实现webservice功能》仅用于学习参考。如对内容有疑问,请及时联系本站处理。

上篇MySQL下载安装、基本配置、问题处理生成函数下篇

宿迁高防,2C2G15M,22元/月;香港BGP,2C5G5M,25元/月 雨云优惠码:MjYwNzM=

相关文章

JSSDK实现微信自定义分享---java 后端获取签名信息

一、首先说下关于微信Access_token的问题,微信Access_token分为2中: 1.授权token获取方式: 这种token需要code值(如何获取code值查看官方文档) 1 "https://api.weixin.qq.com/sns/oauth2/access_token?appid=" + appId + "&secret="...

如何解析json字符串及返回json数据到前端

前言:最近需要实现的任务是:写若干个接口,并且接口中的请求数据是json格式,然后按照请求参数读取前端提前整理好的json数据,并且将json数据返回到服务器端。 主要的工具:Gson 2.8.2 项目支撑:springboot maven 0、前导——了解一下基本的json语法 JSON是一种类似 XML的语言,是用了存储和交换文本信息的语法。它...

.Net编程之Web Service 和WCF的历史和特性

Web Service 的工作原理       Web Service也叫XML Web Service WebService是一种可以接收从Internet或者Intranet上的其它系统中传递过来的请求,轻量级的独立的通讯技术。是:通过SOAP在Web上提供的软件服务,使用WSDL文件进行说明,并通过UDDI进行注册。WebService可用基...

企业微信开发之授权登录

以前写过一篇公众号的授权登录https://blog.csdn.net/dsn727455218/article/details/65630151,今天给大家分享一下企业微信的授权登录。 大致都差不多流程 注意事项: 1.网页授权及JS-SDK需要在企业微信上配置可信域名 2.企业微信授权登录里面填写你的可信域名 调用流程为:A) 用户访问第三方服务,第三...

webService 服务端搭建

  首先,下载CXF,官网(http://cxf.apache.org/),具体位置如下图:      解压后,得到以下目录   下面,我们开始建立工程,在新建的工程lib目录下复制上述lib中的所有文件,特别是endorsed文件夹也要原样复制。 1        WebService的服务器端 1)      创建工程   在eclipse/myEcl...

Json与List的相互转换

问题由来: 最近由于做一个项目,项目的一个功能就是根据Listview的内容生成一个二维码,然后扫描二维码获取list,再重新显示listview。 核心就是: list—->生成二维码——>获取二维码—–>获取list 生成二维码的方法: http://blog.csdn.net/demonliuhui/article/details/...