spring boot2 integrated api documentation tool swagger UI

Explain

Step 1: create a project

Step 2: import development tools

  • Open the download directory and extract the project file

  • Launch idea and import project file

Step 3: introduce swagger UI package

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

Step 4: create a swagger configuration

@EnableSwagger2
@Configuration
public class SwaggerConfig {


    @Bean
    public Docket api(){

        return new Docket(DocumentationType.SWAGGER_2)
                .apiInfo(apiInfo())
                .select()
                .apis(RequestHandlerSelectors.basePackage("com.shelton.swaggerui"))
                .build();
    }


    public ApiInfo apiInfo(){
        return new ApiInfoBuilder()
                .title("api Interface specification")
                .version("1.0.0")
                .build();
    }

}

Step 5: create a POST api interface


@RestController
@Api(value = "User interface", tags={"User operation interface"})
public class IndexController {

    @ApiOperation(value = "User login",notes = "Sign in",httpMethod = "POST")
    @PostMapping("/login")
    public Object get(@RequestParam @ApiParam(name="username",value="Login account",required=true) String username, @RequestParam @ApiParam(name="password",value="Password",required=true) String password){

        Map map = new HashMap();
        map.put("msg","success");
        map.put("code","1");
        return map;
    }
}
  • Page rendering

  • Test api interface request

  • The result returned here is the same as we expected.

summary

Tags: Programming Spring

Posted on Sun, 12 Jan 2020 07:11:43 -0800 by codersrini