54.3. Spring 引导实现

这部分论述了如何在 Spring Boot 中使用 Swagger2Feature。

请注意,对于 OpenAPI 3 的实现,请使用 OpenApiFeature(org.apache.cxf.jaxrs.openapi.OpenApiFeature)。

54.3.1. Quickstart 示例

Quickstart 示例(https://github.com/fabric8-quickstarts/spring-boot-cxf-jaxrs)演示了如何将 Apache CXF 与 Spring Boot 搭配使用。Quickstart 使用 Spring Boot 配置一个启用了 Swagger 的 CXF JAX-RS 端点的应用程序。

54.3.2. 启用 Swagger

启用 Swagger 涉及:

  • 在 REST 应用程序中:

    • 导入 Swagger2Feature:

      import org.apache.cxf.jaxrs.swagger.Swagger2Feature;
    • 将 Swagger2Feature 添加到 CXF 端点:

      endpoint.setFeatures(Arrays.asList(new Swagger2Feature()));

      例如,请参阅 55.1 示例 REST 应用程序

  • 在 Java 实现文件中,为服务所需的每个注解导入 Swagger API 注解:

    import io.swagger.annotations.*

    其中 * = Api,ApiOperation,ApiResponse, ApiResponse ,ApiResponses 等等。

    详情请查看 https://github.com/swagger-api/swagger-core/wiki/Annotations

    例如,请参阅 55.2 示例 Java 实施文件

  • 在 Java 文件中,将 Swagger 注释添加到 JAX-RS 注释的端点(@PATH,@PUT,@POST,@GET,@Produces,@Consumes,@DELETE,@PathParam 等等)。

    例如,请参阅 55.3 示例 Java 文件

55.1 示例 REST 应用程序

package io.fabric8.quickstarts.cxf.jaxrs;

import java.util.Arrays;

import org.apache.cxf.Bus;
import org.apache.cxf.endpoint.Server;
import org.apache.cxf.jaxrs.JAXRSServerFactoryBean;
import org.apache.cxf.jaxrs.swagger.Swagger2Feature;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.context.annotation.Bean;

@SpringBootApplication
public class SampleRestApplication {

    @Autowired
    private Bus bus;

    public static void main(String[] args) {
        SpringApplication.run(SampleRestApplication.class, args);
    }

    @Bean
    public Server rsServer() {
        // setup CXF-RS
        JAXRSServerFactoryBean endpoint = new JAXRSServerFactoryBean();
        endpoint.setBus(bus);
        endpoint.setServiceBeans(Arrays.<Object>asList(new HelloServiceImpl()));
        endpoint.setAddress("/");
        endpoint.setFeatures(Arrays.asList(new Swagger2Feature()));
        return endpoint.create();
    }
}

55.2 示例 Java 实施文件

import io.swagger.annotations.Api;

@Api("/sayHello")
public class HelloServiceImpl implements HelloService {

    public String welcome() {
        return "Welcome to the CXF RS Spring Boot application, append /{name} to call the hello service";
    }

    public String sayHello(String a) {
        return "Hello " + a + ", Welcome to CXF RS Spring Boot World!!!";
    }

}

55.3 示例 Java 文件

package io.fabric8.quickstarts.cxf.jaxrs;

import javax.ws.rs.GET;
import javax.ws.rs.Path;
import javax.ws.rs.PathParam;
import javax.ws.rs.Produces;
import javax.ws.rs.core.MediaType;

import org.springframework.stereotype.Service;

@Path("/sayHello")
@Service
public interface HelloService {

    @GET
    @Path("")
    @Produces(MediaType.TEXT_PLAIN)
    String welcome();

    @GET
    @Path("/{a}")
    @Produces(MediaType.TEXT_PLAIN)
    String sayHello(@PathParam("a") String a);

}