Springboot + cxf集成web service接口

编程教程 > Java > Spring (990) 2024-11-26 14:39:04
最近工作中需要用到webservice,而且结合spring boot进行开发,参照了一些网上的资料,配置过程中出现的了一些问题,于是写了这篇博客,记录一下我这次spring boot+cxf开发的webservice的配置过程,仅供参考。
一、项目是基于springboot搭建的webservice接口,首先是cxf接口,需要的jar包
<!-- cxf支持 -->
		<dependency>
			<groupId>org.apache.cxf</groupId>
			<artifactId>cxf-rt-frontend-jaxws</artifactId>
			<version>3.2.1</version>
		</dependency>

		<dependency>
			<groupId>org.apache.cxf</groupId>
			<artifactId>cxf-rt-transports-http</artifactId>
			<version>3.2.1</version>
		</dependency>
二、创建实体类,内容是用户信息查询和记录
package com.yeyue.cxfws.model;

import java.io.Serializable;

/**
 * @ClassName: User
 * @Description: user实体对象
 * @author: xiaoboLi
 * @date: 2017/11/20 17:20
 * @Copyright: 2017 All rights reserved.
 */
public class User implements Serializable {
    private Long id;
    private String username;
    private String password;

    public Long getId() {
        return id;
    }

    public void setId(Long id) {
        this.id = id;
    }

    public String getUsername() {
        return username;
    }

    public void setUsername(String username) {
        this.username = username;
    }

    public String getPassword() {
        return password;
    }

    public void setPassword(String password) {
        this.password = password;
    }
}

三、接下来创建接口类
package com.yeyue.cxfws.service;

import com.yeyue.cxfws.model.User;

import javax.jws.WebService;

/**
 * @ClassName: UserService
 * @Description: 接口
 * @author: xiaoboLi
 * @date: 2017/11/20 17:22
 * @Copyright: 2017 All rights reserved.
 */
@WebService
public interface UserService {
    User getUser(Long id);
}
四、创建接口实现类
package com.yeyue.cxfws.service.impl;

import com.yeyue.cxfws.model.User;
import com.yeyue.cxfws.service.UserService;

import javax.jws.WebService;
import java.util.HashMap;
import java.util.Map;

/**
 * @ClassName: UserServiceImpl
 * @Description: 描述
 * @author: xiaoboLi
 * @date: 2017/11/20 17:23
 * @Copyright: 2017 All rights reserved.
 */
@WebService(targetNamespace = "http://service.cxfws.yeyue.com/", endpointInterface = "com.yeyue.cxfws.service.UserService")
public class UserServiceImpl implements UserService {
    Map<Long, User> userMap = new HashMap<>();

    public UserServiceImpl() {
        System.out.println("向实体类插入数据");
        User user = new User();
        user.setId(1L);
        user.setUsername("test1");
        user.setPassword("aaaaaa");
        userMap.put(user.getId(), user);
        user.setId(2L);
        user.setUsername("test1");
        user.setPassword("aaaaaa");
        userMap.put(user.getId(), user);
        user.setId(3L);
        user.setUsername("test1");
        user.setPassword("aaaaaa");
        userMap.put(user.getId(), user);
    }

    @Override
    public User getUser(Long id) {
        return userMap.get(id);
    }
}
注释说明:在发布服务之前,需要在这里对service实现类的配置进行说明如图部分:

操作示例图-3f2ce249d2bb4154b387ef311660ec07.png
http://service.cxfws.yeyue.com/这是我的业务类所在路径
com.yeyue.cxfws.service.UserService这是我的接口所在路径
如果不加上上述内容,发布的接口路径wsdl中的targetNamespace会是类默认的路径,并且客户端在进行调用的时候会报错:
No operation was found with the name {http://impl.service.demo.paybay.cn/}getUser.
那么原因就是:在CXF发布服务的时候,发布的是业务类(UserServiceImpl.java),那么默认的命名空间就会是业务类所在包(路径),而对外界暴露的则是接口类(UserService.java),那么对于客户端调用的时侯,需要按照接口类所在路径进行命名空间的定义。
所以在发布之前我们要在业务类(UserServiceImpl.java)上增加注解,指定命名空间,然后再进行发布,
操作示例图-12806853926e4224aeaae2dd9bfc093e.png
五、接下来我就要对webservice服务进行发布:
package com.yeyue.cxfws.config;

import com.yeyue.cxfws.service.UserService;
import com.yeyue.cxfws.service.impl.UserServiceImpl;
import org.apache.cxf.Bus;
import org.apache.cxf.bus.spring.SpringBus;
import javax.xml.ws.Endpoint;
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;

/**
 * @ClassName: WebServiceConfig
 * @Description: 发布webservice接口
 * 访问地址:http://localhost:8080/test/user?wsdl
 * @author: xiaoboLi
 * @date: 2017/11/20 17:35
 * @Copyright: 2017 All rights reserved.
 */
@Configuration
public class CxfWebServiceConfig {
    @Bean
    public ServletRegistrationBean dispatcherServlet() {
        //接口部署的前部分地址
        return new ServletRegistrationBean(new CXFServlet(), "/test/*");
    }
    @Bean(name = Bus.DEFAULT_BUS_ID)
    public SpringBus springBus() {
        return new SpringBus();
    }
    @Bean
    public UserService userService() {
        return new UserServiceImpl();
    }
    @Bean
    public Endpoint endpoint() {
        EndpointImpl endpoint = new EndpointImpl(springBus(), userService());
        //访问的wsdl路径
        endpoint.publish("/user");
        return endpoint;
    }
}
 
那么到这里呢,我们的所有的步骤基本完成了,启动spring boot 然后再浏览器中输入url:http://localhost:8080/webservice/test/user?wsdl
可以看到有相关的wsdl描述信息输出了,说明服务已经发布了。
六、调用服务
package com.yeyue.cxfws.test;

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

/**
 * @ClassName: Client
 * @Description: 描述
 * @author: xiaoboLi
 * @date: 2017/11/20 18:18
 * @Copyright: 2017 All rights reserved.
 */
public class Client {
    public static void main(String[] args) throws Exception {
        JaxWsDynamicClientFactory dcf =JaxWsDynamicClientFactory.newInstance();
        org.apache.cxf.endpoint.Client client =dcf.createClient("http://localhost:8080/webservice/test/user?wsdl");
        Object[] objects=client.invoke("getUser",1L);
        System.out.println("*****"+objects[0].toString());
    }
}
最后附上项目层级结构
操作示例图-dc504cf0484c4f30bf180e66d28d7f1f.png




评论
User Image
提示:请评论与当前内容相关的回复,广告、推广或无关内容将被删除。

相关文章
cxf、Springboot、webservice 源文:https://www.cnblogs.com/fuxin41/p/6289162.html
spring boot整合cxf发布webservice服务和cxf客户端调用,说起web service最近几年restful大行其道,大有取代传统soap web service的趋势,但是...
WebService 常见面试问题解答,在这篇文章中,我们几乎涵盖了您需要熟悉的Webservice概念的每个知识点部分,以回答任何层级问题。您还可以在分类中找到最佳的WebService服务面...
Java 10上的Apache CXF
Java编程中纯jdk java方式编写webservice服务(server)和客服端(client)
spring boot整合Jersey2.x实现JAX-RS webservice
本篇博客讲的spring boot如何集成 spring web service,如果您想用Apache CXF集成,那么可能不适合您。为什么使用spring web servce 项目地址 呢...
spring boot RPC 框架 Hessian,本文主要讲解spring boot整合hessian实现Spring boot RPC调用和Spring boot rpc框架hessian...
Spring Boot 2.0 绑定properties属性资源文件 Spring Boot 2.0 读取properties配置文件值 Spring Boot 2.0获取properties配...
引言Spring Boot 2.0最近去了GA,所以我决定写我关于Spring的第一篇文章很长一段时间
Spring WebFlux 项目实战 在Spring WebFlux中创建多个RouterFunctions,在这篇文章中,我们将着眼于在Spring WebFlux中将多个路由器功能定义到不...
引言在这篇文章中,我们将讨论如何使用Spring Boot Security OAuth2保护REST API