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.*
여기서 * =
,ApiOperationApiParam,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);
}