2016-10-13 16 views
0

swagger2を使用して、SpringブートアプリケーションのREST APIドキュメントを生成しようとしています。 Application.JavaSwagger2を使用してSpringブートREST APIドキュメントを生成する方法は?

ここ
import org.springframework.context.annotation.Bean; 
import org.springframework.context.annotation.Configuration; 
import org.springframework.http.ResponseEntity; 
import springfox.documentation.builders.ApiInfoBuilder; 
import springfox.documentation.builders.RequestHandlerSelectors; 
import springfox.documentation.service.ApiInfo; 
import springfox.documentation.spi.DocumentationType; 
import springfox.documentation.spring.web.plugins.ApiSelectorBuilder; 
import springfox.documentation.spring.web.plugins.Docket; 
import springfox.documentation.swagger.web.UiConfiguration; 
import static springfox.documentation.builders.PathSelectors.*; 
@Configuration 
public class ApiDocumentationConfiguration { 
    @Bean 
    public Docket documentation() { 
    return new Docket(DocumentationType.SWAGGER_2) 
     .select() 
     .apis(RequestHandlerSelectors.any()) 
     //.paths(regex("/.*")) 
     .build() 
     .pathMapping("/") 
     .apiInfo(metadata()); 
    } 
    @Bean 
    public UiConfiguration uiConfig() { 
     return UiConfiguration.DEFAULT; 
    } 
    private ApiInfo metadata() { 
     return new ApiInfoBuilder() 
    .title("My awesome API") 
    .description("Some description") 
    .version("1.0") 
    .contact("[email protected]") 
    .build(); 
    } 
} 

は次のとおりです:ここで

server.port = ${port:8082} 
server.contextPath=/myServicePath 
spring.h2.console.enabled=true 
logging.level.org.hibernate.SQL=debug 
spring.datasource.url=jdbc:mysql://${mysql-host:localhost}:${mysql-port:3306}/${mysql-dbname:mydb} 
spring.datasource.username=${mysql-user:root} 
spring.datasource.password=${mysql-password:password} 
spring.datasource.driver-class-name=com.mysql.jdbc.Driver 

spring.jpa.hibernate.ddl-auto=update 
spring.jpa.show-sql=true 

は私swaggerConfig.Javaである:ここでは

は私のapplication.propertiesファイルの内容です

ここ
import org.springframework.boot.SpringApplication; 
import org.springframework.boot.autoconfigure.SpringBootApplication; 
import org.springframework.context.annotation.Bean; 
import org.springframework.context.annotation.ComponentScan; 
import org.springframework.context.annotation.Import; 
import springfox.documentation.swagger2.annotations.EnableSwagger2; 
import org.springframework.web.bind.annotation.RequestMapping; 
import org.springframework.web.bind.annotation.RequestMethod; 
import org.springframework.web.bind.annotation.RestController; 

@SpringBootApplication 
@ComponentScan 
@EnableSwagger2 
public class Application { 
    public static void main(String[] args) 
    { 
     SpringApplication.run(Application.class, args); 
    } 
} 

である私コントローラ:

@Path("/") 
@Consumes(MediaType.APPLICATION_JSON) 
@Produces(MediaType.APPLICATION_JSON) 
@RestController 

public class MyController { 

    @Autowired 
    private HttpServletRequest request; 

    @ApiOperation(value = "doStuff", nickname = "doStuff", response = Response.class) 
    @RequestMapping(method = RequestMethod.GET, produces = "application/json") 
    public String doStuff(@RequestBody String command) { 
     return "TestString"; 
    }  
} 

私はSpringブート1.4.0とswagger2を使用しています。私は自分のpom.xmlに依存関係の下に追加しました:

<dependency> 
    <groupId>io.springfox</groupId> 
    <artifactId>springfox-swagger2</artifactId> 
    <version>2.1.1</version>    
</dependency> 
<dependency> 
    <groupId>io.springfox</groupId> 
    <artifactId>springfox-swagger-ui</artifactId> 
    <version>2.1.1</version> 
</dependency> 

私はURL http://localhost:8082/swagger-ui.htmlを使用する場合は、 誰もがこれを理解するために私を助けることができる私は、エラー404

を得ましたか。

ありがとうございます。

+0

は、なぜあなたはPATH' @ '使用していますか? –

+0

@CássioMazzochiMolin私はapplication.propertiesにcontextPathを定義しました。 – Vishwas

+0

'@Path'はJAX-RSからのものです。あなたがその注釈で何を達成しようとしているのか分かりません。ちなみに、JAX-RSからは '@ Produces'と' @ Consumes'もあります。 –

答えて

0

リソースハンドラが必要な場合があります。私はあなたが注釈付き@SpingBootApplicationする前に、あなたの@Configuration注釈付きクラスにEnableSwagger2 @使用する必要が考える

@Override 
public void addResourceHandlers(ResourceHandlerRegistry registry) { 

    registry.addResourceHandler("swagger-ui.html") 
      .addResourceLocations("classpath:/META-INF/resources/"); 

    registry.addResourceHandler("/webjars/**") 
      .addResourceLocations("classpath:/META-INF/resources/webjars/"); 
} 
+0

このスニペットはどこに追加すればよいですか? – Vishwas

+0

@Vishwas 'WebMvcConfigurerAdapter'を拡張し、' @ EnableWebMvc'で注釈を付けるクラスを作成します。 –

+0

お返事ありがとうございます。私はこれを試してみましょう。 – Vishwas

0

:ようなものである可能性があります。このarticle近い

に見てみましょう.paths(PathSelectors.anyを())を追加してみてください。

@Bean 
    public Docket api() { 
     return new Docket(DocumentationType.SWAGGER_2) 
      .select()         
      .apis(RequestHandlerSelectors.any())    
      .paths(PathSelectors.any())       
      .build();           
    } 
関連する問題