springboot + webservice

什么是 webservice

WebService是一种跨编程语言、跨操作系统平台的远程调用技术。

server 端:提供服务
client 端:调用服务

示例
Server 端

因为是基于 Springboot 创建的,所以需要 springboot 的依赖,这里省略了,只写相关主要代码以及依赖

  1. pom.xml
<!-- CXF webservice -->
<dependency>
		<groupId>org.apache.cxf</groupId>
		<artifactId>cxf-spring-boot-starter-jaxws</artifactId>
		<version>3.1.11</version>
</dependency>
  1. 配置类

配置注册相应的 bean,并且发布出去

@Configuration
public class CxfConfig {

    // 默认servlet路径 /*,如果覆写则按照自己定义的来,注册一个 CXFServlet
    @Bean
    public ServletRegistrationBean dispatcherServlet() {
        return new ServletRegistrationBean(new CXFServlet(), "/services/*");
    }

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

    // 把实现类交给spring管理
    @Bean
    public IService appService() {
        return new UserServiceImpl();
    }

    // 终端路径
    @Bean
    public Endpoint endpoint() {
        EndpointImpl endpoint = new EndpointImpl(springBus(), appService());
        endpoint.getInInterceptors().add(new AuthInterceptor());// 添加校验拦截器
        // 发布
        endpoint.publish("/user");
        return endpoint;
    }
}
  1. 定义接口类(也就是提供出去的服务)
@WebService(targetNamespace = "http://service.beng.com/")
public interface IService {

    @WebMethod
    String getUserName(@WebParam(name = "id") String id) throws UnsupportedEncodingException;

    public User getUserNameById(String id) throws UnsupportedEncodingException;

    @WebMethod
    public User getUser(String id) throws UnsupportedEncodingException;
}
  1. 接口实现类
    定义接口服务的具体实现逻辑
@WebService(name = "UserService", targetNamespace = "http://service.beng.com/", endpointInterface = "com.beng.service.IService")
public class UserServiceImpl implements IService {

    @Override
    public String getUserName(String id) throws UnsupportedEncodingException {
        System.out.print("=========>>>>>>>>>" + id);
        // TODO 调用数据库(查询数据库业务逻辑)

        User user = User.builder().id(1).username("小机灵鬼").age(18).build();
        return JSONObject.toJSONString(user);
    }

    @Override
    public User getUserNameById(String id) throws UnsupportedEncodingException {
        System.out.print("=========>>>>>>>>>" + id);
        // TODO 调用数据库(查询数据库业务逻辑)

        return User.builder().id(1).username("小机灵鬼").age(18).build();
    }

    @Override
    public User getUser(String id) throws UnsupportedEncodingException {
        System.out.print("=========>>>>>>>>>" + id);
        // TODO 调用数据库(查询数据库业务逻辑)

        return User.builder().id(1).username("小机灵鬼").age(18).build();
    }

}
  1. 启动后访问 http://localhost:8888/services/user?wsdl

在这里插入图片描述
到此 server 端就启动完成了。

Client 端

客户端有两种调用当时:

  1. 不需要将服务端的代码copy过来
public class CxfDynamicClient {

    private static String address = "http://localhost:8888/services/user?wsdl";

    public static void invokeRemoteService() {
        // 创建动态客户端
        JaxWsDynamicClientFactory dcf = JaxWsDynamicClientFactory.newInstance();
        Client client = dcf.createClient(address);
        // 需要密码的情况需要加上用户名和密码
        client.getOutInterceptors().add(new LoginInterceptor("root", "admin"));
        Object[] objects = new Object[0];
        try {
            System.out.println("======client" + client);
            objects = client.invoke("getUserNameById", "1");
            User user = new User();
            BeanUtils.copyProperties((objects[0]), user);
            System.out.println("返回数据:" + user.toString());
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

    public static void main(String[] args) {
        invokeRemoteService();
    }
  1. 需要将服务员的接口copy过来
public class CxfDefaultClient {
    private static String address = "http://localhost:8888/services/user?wsdl";

    public static void invokeRemoteService() {
        try {
            // 代理工厂
            JaxWsProxyFactoryBean jaxWsProxyFactoryBean = new JaxWsProxyFactoryBean();
            // 设置代理地址
            jaxWsProxyFactoryBean.setAddress(address);
            // 添加用户名密码拦截器
            jaxWsProxyFactoryBean.getOutInterceptors().add(new LoginInterceptor("root", "admin"));
            // 设置接口类型
            jaxWsProxyFactoryBean.setServiceClass(IService.class);
            // 创建一个代理接口实现
            IService cs = (IService) jaxWsProxyFactoryBean.create();
            // 数据准备
            String LineId = "1";
            // 调用代理接口的方法调用并返回结果
            User result = (User) cs.getUser(LineId);
            System.out.println("==============返回结果:" + result);
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

    public static void main(String[] args) {
        invokeRemoteService();
    }
}

github 地址:https://github.com/abinbao/spring-bucket/tree/master/springboot-webservice

  • 0
    点赞
  • 4
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值