Spring Boot整合CXF项目

                                                                                                                     2018年12月29日,元旦放假,浙江绍兴

昨天做了一个Spring Boot整合CXF的简单小栗子。今天放假没事,跑妈妈这边来玩,晚上趁着这个雪天没事,记录一下项目创建过程和思路。

当前项目结构如下(后面会补充几种调用Web Service的方式的代码):

POM.XML文件也比较简单,就是在Spring Boot 的initalizer的时候把spring boot版本降到2.07,然后在web项里选择Apache CXF(JAX-RS)自动引入的依赖,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>
    <parent>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-parent</artifactId>
        <version>2.0.7.RELEASE</version>
        <relativePath/> <!-- lookup parent from repository -->
    </parent>
    <groupId>com.WebService</groupId>
    <artifactId>spring-cxf</artifactId>
    <version>0.0.1-SNAPSHOT</version>
    <name>spring-cxf</name>
    <packaging>war</packaging>
    <description>Demo project for Spring Boot</description>

    <properties>
        <java.version>1.8</java.version>
        <spring.boot.version>1.6.6</spring.boot.version>
    </properties>

    <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-jaxrs</artifactId>
            <version>3.2.5</version>
        </dependency>

        <dependency>
            <groupId>mysql</groupId>
            <artifactId>mysql-connector-java</artifactId>
        </dependency>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-test</artifactId>
            <scope>test</scope>
        </dependency>

        <!-- https://mvnrepository.com/artifact/org.apache.cxf/cxf-rt-frontend-jaxws -->
        <dependency>
            <groupId>org.apache.cxf</groupId>
            <artifactId>cxf-rt-frontend-jaxws</artifactId>
            <version>3.2.6</version>
        </dependency>




    </dependencies>

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

</project>

此处我们全部使用注解,尽量少的使用配置。

MyTestService的代码如下

package com.webservice.springcxf.service;

import javax.jws.WebService;

/**
 * @Author: gtd
 * @Description:
 * @Date: Create in 22:03 2018/12/29
 */
@WebService
public interface MyTestService {

    String print(String name);
}

实现类MyTestServiceImpl的代码如下:

package com.webservice.springcxf.service.impl;

import com.webservice.springcxf.service.MyTestService;
import org.springframework.stereotype.Component;

import javax.jws.WebMethod;
import javax.jws.WebParam;
import javax.jws.WebResult;
import javax.jws.WebService;
import javax.xml.ws.WebEndpoint;

/**
 * @Author: 高铁墩
 * @Description:
 * @Date: Create in 22:18 2018/12/29
 */
@WebService(serviceName = "myTestService", // 与接口中指定的name一致
        endpointInterface = "com.webservice.springcxf.service.MyTestService")// 接口地址
@Component
public class MyTestServiceImpl implements MyTestService {

    @WebMethod
    public String print(@WebParam (name = "username") String name) {
        return "hello," + name;
    }

}

我们的配置类WebServiceConfig代码如下(在加载配置类的时候,发布我们的接口)。

package com.webservice.springcxf.config;

import com.webservice.springcxf.service.MyTestService;
import org.apache.cxf.Bus;
import org.apache.cxf.jaxws.EndpointImpl;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;

import javax.xml.ws.Endpoint;

/**
 * @Author: gtd
 * @Description:
 * @Date: Create in 20:20 2018/12/30
 */
@Configuration
public class WebServiceConfig {
    @Autowired
    private Bus bus;

    @Autowired
    @Qualifier("myTestServiceImpl")
    private MyTestService myTestService;

    @Bean
    public Endpoint endpoint(){
        Endpoint endpoint = new EndpointImpl(bus,myTestService);    //创建Endpoint实例
        endpoint.publish("/myService");                     //设置发布地址并发布
        return endpoint;
    }

}

当然,我们也可以使用通过配置的方式发布,这里我把我第一次通过配置发布方式的配置文件代码也列出来吧,供大家参考:

applicationContext.xml 

扫描二维码关注公众号,回复: 4815001 查看本文章
<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
       xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
       xmlns:context="http://www.springframework.org/schema/context"
       xmlns:jaxws="http://cxf.apache.org/jaxws"
       xsi:schemaLocation="http://www.springframework.org/schema/beans
       http://www.springframework.org/schema/beans/spring-beans.xsd
       http://www.springframework.org/schema/context
       http://www.springframework.org/schema/context/spring-context.xsd
       http://cxf.apache.org/jaxws
       http://cxf.apache.org/schemas/jaxws.xsd
        ">

    <context:component-scan base-package="com.webservice.springcxf"/>

    <jaxws:endpoint id="myTestWebService"
        implementor="com.webservice.springcxf.service.impl.MyTestServiceImpl"
        address="/myService"
    />


</beans>

两种方式任选其一,执行启动类,在浏览器输入:http://127.0.0.1:8082/WebService/services/myService?wsdl

返回截图:

今天先到此位置,后面继续补充客户端代码和其他调用方式代码,以及对CXF的源码在其他文章里分析。晚安~~

源码地址:[email protected]:tiedungao/spring-cxf.git

猜你喜欢

转载自blog.csdn.net/gaotiedun1/article/details/85410354
今日推荐