2016-03-13 53 views
3

我正在使用springboot +球衣來實現網頁寧靜。現在我將把大舉集成到我們的應用程序中。我確實如下。如何整合Swagger與運動衫+彈簧靴

我加了下面就的build.gradle依賴性:

compile('io.springfox:springfox-swagger2:'+springfoxSwaggerVersion) 
compile('io.springfox:springfox-petstore:'+springfoxSwaggerVersion) 
compile('io.springfox:springfox-swagger-ui:'+springfoxSwaggerVersion) 
compile('io.swagger:swagger-jersey2-jaxrs:1.5.8') 

我能夠啓動Web應用程序,但我徘徊其中URL是招搖?我試着用http://localhost:8080,http://localhost:8080/swaggerhttp://localhost:8080/swagger-ui.html。但是他們都不能訪問。

+0

貴應用程序有一個上下文根?它應該在相對於上下文根的swagger-ui.html中可用。 – nerdherd

+0

上下文根只是「/」。 –

回答

0

也許你使用,而舊版本springfox的,但現在(對於版本2.4.0),它必須配置從您的代碼非常不同,看到http://springfox.github.io/springfox/docs/current/

例如,我有以下springfox配置:

@Configuration 
@EnableSwagger2 
class SwaggerConfig { 
    @Bean 
    Docket rsApi() { 
     new Docket(DocumentationType.SWAGGER_12) 
      .apiInfo(apiInfo()) 
      .select() 
        .apis(RequestHandlerSelectors.basePackage('com.test.service')) 
      .paths(PathSelectors.any()) 
      .build() 
      .pathMapping('/') 
      .useDefaultResponseMessages(false) 
    } 

    private ApiInfo apiInfo() { 
     new ApiInfoBuilder() 
      .title('Test API') 
      .description('Test API') 
      .version('0.0.10.SNAPSHOT') 
      .termsOfServiceUrl('') 
      .contact('Test company') 
      .license('Public') 
      .licenseUrl('http://example.com/') 
      .build() 
    } 
} 
6

我認爲@EnableSwagger2註釋和springfox如果端點是使用Spring MVC而不是JAX-RS實現來實現的,則依賴關係將起作用。

我幾個月前在博客這個,Microservices using Spring Boot, Jersey Swagger and Docker

基本上,如果你需要記錄您的澤西實現端點,你將需要:

1) 請確信你的春天啓動的應用程序掃描

@SpringBootApplication(
    scanBasePackages = { 
     "com.asimio.jerseyexample.config", "com.asimio.jerseyexample.rest" 
    } 
) 

2)澤西配置類實現:位於特定的包(即com.asimio.jerseyexample.config)經由部件

package com.asimio.jerseyexample.config; 
... 
@Component 
public class JerseyConfig extends ResourceConfig { 

    @Value("${spring.jersey.application-path:/}") 
    private String apiPath; 

    public JerseyConfig() { 
     // Register endpoints, providers, ... 
     this.registerEndpoints(); 
    } 

    @PostConstruct 
    public void init() { 
     // Register components where DI is needed 
     this.configureSwagger(); 
    } 

    private void registerEndpoints() { 
     this.register(HelloResource.class); 
     // Access through /<Jersey's servlet path>/application.wadl 
     this.register(WadlResource.class); 
    } 

    private void configureSwagger() { 
     // Available at localhost:port/swagger.json 
     this.register(ApiListingResource.class); 
     this.register(SwaggerSerializers.class); 

     BeanConfig config = new BeanConfig(); 
     config.setConfigId("springboot-jersey-swagger-docker-example"); 
     config.setTitle("Spring Boot + Jersey + Swagger + Docker Example"); 
     config.setVersion("v1"); 
     config.setContact("Orlando L Otero"); 
     config.setSchemes(new String[] { "http", "https" }); 
     config.setBasePath(this.apiPath); 
     config.setResourcePackage("com.asimio.jerseyexample.rest.v1"); 
     config.setPrettyPrint(true); 
     config.setScan(true); 
    } 
} 

3)使用JAX-RS(新澤西州)和揚鞭註釋資源實現:

package com.asimio.jerseyexample.rest.v1; 
... 
@Component 
@Path("/") 
@Consumes(MediaType.APPLICATION_JSON) 
@Produces(MediaType.APPLICATION_JSON) 
@Api(value = "Hello resource", produces = "application/json") 
public class HelloResource { 

    private static final Logger LOGGER = LoggerFactory.getLogger(HelloResource.class); 

    @GET 
    @Path("v1/hello/{name}") 
    @ApiOperation(value = "Gets a hello resource. Version 1 - (version in URL)", response = Hello.class) 
    @ApiResponses(value = { 
     @ApiResponse(code = 200, message = "Hello resource found"), 
     @ApiResponse(code = 404, message = "Hello resource not found") 
    }) 
    public Response getHelloVersionInUrl(@ApiParam @PathParam("name") String name) { 
     LOGGER.info("getHelloVersionInUrl() v1"); 
     return this.getHello(name, "Version 1 - passed in URL"); 
    } 
... 
} 

4)確保您的應用的春天啓動配置文件,使執行器終端的Spring MVC(區別)和澤西島(資源)的端點:

application.yml

... 
# Spring MVC dispatcher servlet path. Needs to be different than Jersey's to enable/disable Actuator endpoints access (/info, /health, ...) 
server.servlet-path:/
# Jersey dispatcher servlet 
spring.jersey.application-path: /api 
...