如何在springboot引入CXF webservice

通过上面两篇文章的讲解,我们已经知道了如何生成本地的SSL证书,已经如何zaispringboot中引入HTTPS。那么下面这篇文章开始顺水推舟讲解如何在springboot中开发CXF的webservice。 CXF集成springboot官方文档。里面讲解的非常清晰了。这里就不在赘述了。下面我直接根据自己的项目讲解。代码在spring-soap-https-ssl-server

pom文件

在已经存在

       <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-web</artifactId>
        </dependency>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter</artifactId>
        </dependency>

        <!-- SOAP dependence CXF核心的starter包 -->
        <dependency>
            <groupId>org.apache.cxf</groupId>
            <artifactId>cxf-spring-boot-starter-jaxws</artifactId>
            <version>3.1.12</version>
        </dependency>

写cxf webservice的 Endpoint

Endpoint Interface定义

    // 命名空间,一般是接口的包名倒序
    @WebService(targetNamespace = "http://endpoint.cxfEndpoint.moheqionglin.com")
    public interface CountryEndpoint {
        @WebMethod
        public CountryResponse getCountryByName(CountryByNameRequest req);
    }

Endpoint 实现

@WebService(serviceName = "CountryEndpointService", //服务名
        targetNamespace = "http://endpoint.cxfEndpoint.moheqionglin.com",//报名倒叙,并且和接口定义保持一致
        endpointInterface = "com.moheqionglin.cxfEndpoint.endpoint.CountryEndpoint",
        wsdlLocation = "classpath:wsdls/CountryEndpoint.wsdl")
@Component("countryEndpoint")
public class CountryEndpointImpl implements CountryEndpoint{

    Logger log = LoggerFactory.getLogger(this.getClass());
    @Autowired
    protected CountryRepository countryRepository;
    public CountryEndpointImpl(){}

    public CountryResponse getCountryByName(CountryByNameRequest req) {
        log.info("Get Country By Name Request: {}", req.getName());
        Country c = countryRepository.findCountry(req.getName());
        if(c == null){
            throw new RuntimeException("Not Found by Name : " + req.getName());
        }
        return new CountryResponse(c);
    }
}

接口交互的message定义

@XmlAccessorType(XmlAccessType.FIELD)
@XmlType(namespace = "message.cxfEndpoint.moheqionglin.com")
public class CountryByNameRequest {
    private String name;

    public String getName() {
        return name;
    }

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

@XmlAccessorType(XmlAccessType.FIELD)
@XmlType(namespace = "message.cxfEndpoint.moheqionglin.com")
public class CountryResponse {

    private String name ;
    private String capital;
    private String currency;
    private int population;

    public CountryResponse (){}
    public CountryResponse(Country c){
        this.name = c.getName();
        this.capital = c.getCapital();
        this.currency = c.getCurrency();
        this.population = c.getPopulation();
    }

    public String getName() {
        return name;
    }

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

    public String getCapital() {
        return capital;
    }

    public void setCapital(String capital) {
        this.capital = capital;
    }

    public String getCurrency() {
        return currency;
    }

    public void setCurrency(String currency) {
        this.currency = currency;
    }

    public int getPopulation() {
        return population;
    }

    public void setPopulation(int population) {
        this.population = population;
    }
}

注入到


    @Bean
    public Endpoint endpoint() {
        EndpointImpl endpoint = new EndpointImpl(bus, countryEndpoint);
        endpoint.publish("/CountryEndpoint");//接口发布在 /NetbarServices 目录下
        return endpoint;
    }

获取WSDL

https://localhost:9443/services/CountryEndpoint?wsdl
如果觉得我的文章对您有用,请随意打赏。您的支持将鼓励我继续创作!
慷慨打赏