Swagger2 online document generation official configuration

Swagger2 dependence

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

In use, relatively easy to find 2.7.2, 2.2.2 of possible error

 

Profiles

 

@Configuration 
@ EnableSwagger2 // open document 
public  class SwaggerConfig {
     // 1. declare property api documentation build 
    Private ApiInfo apiInfo () {
         return  new new ApiInfoBuilder (). Title ( "springboot use online documentation to build RestFul style Api" ) 
                . Description ( "bugs ape" ) 
                .termsOfServiceUrl ( "http://cnblogs.com/" ) 
                .contact ( "Java") Version ( "1.0." ) .build (); 
    } 
    // 2 core configuration disposed 
    public Docket No. createRestApi () {
         return  new new Docket(DocumentationType.SWAGGER_2)
                .apiInfo(apiInfo())
                .select().apis(RequestHandlerSelectors.basePackage("com.offcn.springbootdemo.controller"))
                .paths(PathSelectors.any()).build();
    }
}

 Modify Controller increased documentation comments

@PutMapping ( "/ {id}" ) 
        @ApiOperation (value = "update the specified user id information", notes = "user information is updated based on the id" ) 
        @ApiImplicitParams ({ 
                @ApiImplicitParam (name = "id", value = "User ID ", required = to true , dataType =" Long " ), 
                @ApiImplicitParam (name =" user ", value =" user Details entity user ", required = to true , dataType =" the user " ) 
        }) 
        public String the updateUser (@PathVariable ( "ID" ) Long ID, the User User) {
             for (the User user2: LISTUSER) {
                 IF (user2.getId()==id) {
                    user2.setName (user.getName ());
                    user2.setAge (user.getAge ()); 
                } 
            } 
            return "Success" ; 
        } 

// delete the specified user id 
        @DeleteMapping ( "/ {id}" ) 
        @ApiOperation (value = "delete the specified user id information", notes = "according to id delete" ) 
        @ApiImplicitParam (name = "id", value = "user id", required = to true , dataType = "Long" ) 

        public String deleteUser (@PathVariable ( "id" ) Long id) { 
            listUser.remove (the getUser (ID)); 
            return "Success" ; 

        } 
    }

Finally, access to the address: http: // localhost: 8080 / swagger-ui.html

 

 

 

 

 

Guess you like

Origin www.cnblogs.com/proyuan/p/11797719.html