• spring-boot整合Cxf的webservice案例


    1.运行环境

    开发工具:intellij idea

    JDK版本:1.8

    项目管理工具:Maven 4.0.0

    2.Maven Plugin管理

     1 <?xml version="1.0" encoding="UTF-8"?>
     2 <project xmlns="http://maven.apache.org/POM/4.0.0"
     3          xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
     4          xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
     5     <modelVersion>4.0.0</modelVersion>
     6 
     7     <groupId>spring-boot-ws</groupId>
     8     <artifactId>spring-boot-ws</artifactId>
     9     <version>1.0-SNAPSHOT</version>
    10 
    11     <parent>
    12         <groupId>org.springframework.boot</groupId>
    13         <artifactId>spring-boot-starter-parent</artifactId>
    14         <version>1.5.6.RELEASE</version>
    15     </parent>
    16 
    17     <dependencies>
    18         <dependency>
    19             <groupId>org.springframework.boot</groupId>
    20             <artifactId>spring-boot-starter-web</artifactId>
    21         </dependency>
    22         <!-- CXF webservice -->
    23         <dependency>
    24             <groupId>org.apache.cxf</groupId>
    25             <artifactId>cxf-spring-boot-starter-jaxws</artifactId>
    26             <version>3.1.11</version>
    27         </dependency>
    28     </dependencies>
    29 
    30 
    31 </project>
    View Code

    3.WebService方法创建

     1 package com.goku.demo.controller;
     2 
     3 import org.springframework.stereotype.Component;
     4 
     5 import javax.jws.WebMethod;
     6 import javax.jws.WebParam;
     7 import javax.jws.WebService;
     8 
     9 /**
    10  * Created by nbfujx on 2017/11/27.
    11  */
    12 @Component
    13 @WebService(serviceName = "ExampleService"
    14         ,targetNamespace="http://controller.demo.goku.com/")
    15 public class ExampleController {
    16 
    17     @WebMethod
    18     public String echo(@WebParam(name = "para") String para) {
    19     return "hello"+para;
    20     }
    21 }
    View Code

    4.CxfConfig配置编写

     1 package com.goku.demo.config;
     2 
     3 import com.goku.demo.controller.ExampleController;
     4 import org.apache.cxf.Bus;
     5 import org.apache.cxf.jaxws.EndpointImpl;
     6 import org.springframework.beans.factory.annotation.Autowired;
     7 import org.springframework.context.annotation.Bean;
     8 import org.springframework.context.annotation.Configuration;
     9 
    10 import javax.xml.ws.Endpoint;
    11 
    12 /**
    13  * Created by nbfujx on 2017/11/27.
    14  */
    15 @Configuration
    16 public class CxfConfig {
    17 
    18     @Autowired
    19     private Bus bus;
    20 
    21     @Autowired
    22     private ExampleController examplecontroller;
    23 
    24     @Bean
    25     public Endpoint endpoint() {
    26         EndpointImpl endpoint = new EndpointImpl(bus,examplecontroller);
    27         endpoint.publish("/ExampleService");//接口发布在 /NetbarServices 目录下
    28         return endpoint;
    29     }
    30 
    31 }
    View Code

    5.WsApplication启动类编写

     1 package com.goku.demo;
     2 
     3 import org.springframework.boot.SpringApplication;
     4 import org.springframework.boot.autoconfigure.SpringBootApplication;
     5 import org.springframework.boot.web.servlet.ServletComponentScan;
     6 
     7 /**
     8  * Created by nbfujx on 2017/10/19.
     9  */
    10 // Spring Boot 应用的标识
    11 @SpringBootApplication
    12 @ServletComponentScan
    13 public class WsApplication {
    14 
    15     public static void main(String[] args) {
    16         // 程序启动入口
    17         // 启动嵌入式的 Tomcat 并初始化 Spring 环境及其各 Spring 组件
    18         SpringApplication.run(WsApplication.class,args);
    19     }
    20 }
    View Code

    6.Cxf客户端编写

     1 package test.com.goku.demo.client;
     2 
     3 
     4 import org.apache.cxf.endpoint.Client;
     5 import org.apache.cxf.jaxws.JaxWsProxyFactoryBean;
     6 import org.apache.cxf.jaxws.endpoint.dynamic.JaxWsDynamicClientFactory;
     7 
     8 import com.goku.demo.controller.ExampleController;
     9 
    10 /**
    11  * Created by nbfujx on 2017/11/27.
    12  */
    13 public class CxfClient {
    14 
    15     public static void main(String[] args) {
    16         echo();
    17     }
    18 
    19     public static void echo() {
    20         // 创建动态客户端
    21         JaxWsDynamicClientFactory dcf = JaxWsDynamicClientFactory.newInstance();
    22         Client client = dcf.createClient("http://localhost:8080/services/ExampleService?wsdl");
    23         Object[] objects = new Object[0];
    24         try {
    25             objects = client.invoke("echo", "str");
    26             System.out.println("echo:" + objects[0]);
    27         } catch (java.lang.Exception e) {
    28             e.printStackTrace();
    29         }
    30     }
    31 }
    View Code

    7.查看测试结果

    先启动cxf服务端,再进行客户端调阅

    8.GITHUB地址

    https://github.com/nbfujx/springBoot-learn-demo/tree/master/spring-boot-ws

    待续

  • 相关阅读:
    终端程序开机后自动运行
    SQLite 3 使用前要知道的
    JS 框架 :后台系统完整的解决方案
    JS 继承:extend、mixin 和 plugin(三)
    JS 控件事件
    JS 控件 jQuery扩展函数
    Js 控件编写 继承:extend、mixin和plugin(一)
    关于js new Date() 出现NaN 的分析
    js Dialog 实践
    JS控件 生命周期
  • 原文地址:https://www.cnblogs.com/nbfujx/p/7905889.html
Copyright © 2020-2023  润新知