54.3. Spring Boot Implementations

이 섹션에서는 Spring Boot에서 Swagger2Feature를 사용하는 방법에 대해 설명합니다.

OpenAPI 3 구현의 경우 OpenApiFeature(org.apache.cxf.jaxrs.openapi.OpenApiFeature)를 사용하십시오.

54.3.1. 빠른 시작 예

빠른 시작 예 (https://github.com/fabric8-quickstarts/spring-boot-cxf-jaxrs)는 Spring Boot와 함께 Apache CXF를 사용하는 방법을 보여줍니다. 빠른 시작에서는 Swagger가 활성화된 CXF JAX-RS 끝점을 포함하는 애플리케이션을 구성하는 Spring Boot를 사용합니다.

54.3.2. Swagger 활성화

Swagger를 활성화하는 데는 다음이 포함됩니다.

  • REST 애플리케이션에서 다음을 수행합니다.

    • Swagger2feature 가져오기:

      import org.apache.cxf.jaxrs.swagger.Swagger2Feature;
    • CXF 엔드포인트에 Swagger2Feature 추가:

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

      예를 들어 55.1 예 REST 애플리케이션 예 에서 참조하십시오.

  • Java 구현 파일에서 서비스에 필요한 각 주석에 대해 Swagger API 주석을 가져옵니다.

    import io.swagger.annotations.*

    여기서 * = Api Operation,ApiParam,ApiResponse,ApiResponses 등.

    자세한 내용은 https://github.com/swagger-api/swagger-core/wiki/Annotations 에서 참조하십시오.

    예를 들어 55.2 예 Java 구현 파일 에서 참조하십시오.

  • Java 파일에서 Swagger 주석을 JAX-RS 주석에 추가합니다(@PATH,@PUT,@ GET ,@GET,@ 509 ,@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);

}