Springboot整合Springmvc、Swagger2生成 漂亮的 UI(swagger-ui-layer和swagger2markup)

一、首先实现Springboot整合Swagger2生成

我自己使用的是 springboot 2.1.0,默认8080端口

 

springboot依赖 :

<parent>
   <groupId>org.springframework.boot</groupId>
   <artifactId>spring-boot-starter-parent</artifactId>
   <version>2.1.0.RELEASE</version>
   <relativePath/> <!-- lookup parent from repository -->
</parent>

 

swagger依赖:

<dependency>
    <groupId>io.springfox</groupId>
    <artifactId>springfox-swagger2</artifactId>
    <version>2.6.1</version>
</dependency>

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

Springboot加上swagger注解:

@SpringBootApplication
@EnableSwagger2
public class SpringbootSwagger2Application {

   public static void main(String[] args) {
      SpringApplication.run(SpringbootSwagger2Application.class, args);
   }

}

配置swagger:

@Configuration
public class SwaggerConfig {
    @Bean
    public Docket createRestApi() {
        return new Docket(DocumentationType.SWAGGER_2)
                        .apiInfo(apiInfo())
                        .select()
                        .apis(RequestHandlerSelectors.basePackage("com.example.demo.controller"))
                        .paths(PathSelectors.any())
                        .build();
    }

    private ApiInfo apiInfo() {
        return new ApiInfoBuilder()
                        .title("springboot利用swagger构建api文档")
                        .description("简单优雅的restfun风格")
                        .termsOfServiceUrl("http://blog.youkuaiyun.com/saytime")
                        .version("1.0")
                        .build();
    }

}

 

测试的 controller(只加了swagger简单注释):

@RestController
@RequestMapping("/")
public class UserController extends BaseController{

    @Autowired
    private UserManager userManager;

    @ApiOperation(value="获取用户详细信息", notes="根据id来获取用户详细信息")
    @ApiImplicitParam(name = "id", value = "用户ID", required = true, dataType = "Integer", paramType = "path")
    @RequestMapping(value = "user/query-by-id", method = RequestMethod.GET)
    public Result<UserVo> queryUserById(@RequestParam Long id) {
        return success(userManager.queryUserById(id));
    }
}

 

显示的页面 http://localhost:8080/swagger-ui.html

二、接入swagger-

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值