CXF和spring boot整合发布webservice服务

一、首先创建一个springboot项目,记得直选springweb那一项
导依赖

<dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-web</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.1.12</version>
        </dependency>
        <dependency>
            <groupId>org.apache.cxf</groupId>
            <artifactId>cxf-rt-transports-http</artifactId>
            <version>3.1.12</version>
        </dependency>
        <dependency>
            <groupId>org.apache.cxf</groupId>
            <artifactId>cxf-rt-transports-http-jetty</artifactId>
            <version>3.1.12</version>
        </dependency>
        <!-- https://mvnrepository.com/artifact/junit/junit -->
        <dependency>
            <groupId>junit</groupId>
            <artifactId>junit</artifactId>
            <version>4.12</version>
            <scope>test</scope>
        </dependency>

二、接口和实现类和原先的一样

package com.xr.service;

import com.xr.entity.Users;

import javax.jws.WebParam;
import javax.jws.WebResult;
import javax.jws.WebService;
import java.util.List;

@WebService
public interface HelloWorld {

    public String sayHello(@WebParam(name = "name") String name);

    public void add(@WebParam(name = "users") Users users);
    public @WebResult(name = "javaList")
    List<Users> usersList();

}

package com.xr.service.impl;

import com.xr.entity.Users;
import com.xr.service.HelloWorld;

import java.util.ArrayList;
import java.util.List;

public class HelloWorldImpl implements HelloWorld {
    private List<Users> list=new ArrayList<>();
    @Override
    public String sayHello(String name) {
        return name+"hello";
    }

    @Override
    public void add(Users users) {
         list.add(users);
    }

    @Override
    public List<Users> usersList() {
        return list;
    }
}

实体类

package com.xr.entity;

public class Users {
    private Integer id;
    private String name;

    public Users() {
    }

    public Integer getId() {
        return id;
    }

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

    public String getName() {
        return name;
    }

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

三、编写WebServiceConfig.java放在config包下

package com.xr.config;

import com.xr.service.HelloWorld;
import com.xr.service.impl.HelloWorldImpl;
import org.apache.cxf.Bus;
import org.apache.cxf.bus.spring.SpringBus;
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;

import javax.xml.ws.Endpoint;

@Configuration
public class WebServiceConfig {

    @Bean("cxfRegistration")
    public ServletRegistrationBean dispatcherServlet() {
        //CXFServlet 是有的 捕捉/service/*
        /*等会wsdl查看是直接到这个目录下去查看*/
        return new ServletRegistrationBean(new CXFServlet(), "/service/*");//发布服务名称
    }

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

    @Bean
    /*
    * 要发布的对象,放到spring容器中*/
    public HelloWorld helloWorldService() {
        return new HelloWorldImpl();
    }

    @Bean
    public Endpoint endpoint() {
        EndpointImpl endpoint = new EndpointImpl(springBus(), helloWorldService());//绑定要发布的服务
        endpoint.publish("/hello"); //显示要发布的名称
        return endpoint;
    }
}

然后去运行,没报错即可
效果如下
CXF和spring boot整合发布webservice服务

上一篇:java-Apache CXF FaultListener未在cxf总线中注册


下一篇:java – 尝试调用CXF / SOAP Web服务时的LinkageError