springBoot集成SwaggerUI

很简单:

1.引入依赖:

     
            
                io.springfox
                springfox-swagger2
                2.5.0
            
            
            
                io.springfox
                springfox-swagger-ui
                2.5.0
            

2,新建一个配置类,启动swagger

@Configuration
@EnableSwagger2
public class SwaggerConfig {
    @Bean
    public Docket api() {
        return new Docket(DocumentationType.SWAGGER_2)
                .select()
                .apis(RequestHandlerSelectors.any())
                .paths(PathSelectors.any())
                .build();
    }
}

3.在接口中使用注解声明描述:

@Api(tags = "Customer API" ,description = "customer相关Rest API")
@RestController
public class CustomerController {
    Logger logger= LoggerFactory.getLogger(CustomerController.class);


    @Autowired
    private RestTemplate restTemplate;

    @ApiOperation("获取用户姓名")
    @GetMapping("/user/{userName}")
    private String getUser(@PathVariable("userName") String userName){
        logger.info("-------CustomerController request USER-SERVICE for getUser-----");
        return restTemplate.getForObject("http://USER-SERVICE/user/"+userName,String.class);
    }
}

如上,

@Api(tags = "Customer API" ,description = "customer相关Rest API"): 相当于描述一组接口的名称 (放在Controller上) 
@ApiOperation("获取用户姓名"):声明每个接口名称(一个URL)

参数注解:如果是一个实体对象,那就在实体对象里面的属性上使用@ApiModelProperty注解

public class OmsOrderQueryParam {
    @ApiModelProperty(value = "订单编号")
    private String orderSn;
    @ApiModelProperty(value = "收货人姓名/号码")
    private String receiverKeyword;
    @ApiModelProperty(value = "订单状态:0->待付款;1->待发货;2->已发货;3->已完成;4->已关闭;5->无效订单")
    private Integer status;
    @ApiModelProperty(value = "订单类型:0->正常订单;1->秒杀订单")
    private Integer orderType;
    @ApiModelProperty(value = "订单来源:0->PC订单;1->app订单")
    private Integer sourceType;
    @ApiModelProperty(value = "订单提交时间")
    private String createTime;
}

 

启动项目,SwaggerUI提供访问的页面默认为 /swagger-ui.html,项目启动端口为8060

那访问地址为:http://localhost:8060/swagger-ui.html

springBoot集成SwaggerUI_第1张图片

你可能感兴趣的:(SpringBoot)