SpringBoot整合Swagger

首先引入依赖信息,

<!-- https://mvnrepository.com/artifact/com.spring4all/swagger-spring-boot-starter -->
        <dependency>
            <groupId>com.spring4all</groupId>
            <artifactId>swagger-spring-boot-starter</artifactId>
            <version>1.8.0.RELEASE</version>
        </dependency>

这里我查了maven依赖库,Swagger2-spring-boot-starter的依赖信息包似乎挂到了GitHub上,而且使用率比较低,所以此处选用com.spring4all的类库,这里面整合的也是Swagger2.

编写application.yml文件:

server:
  port: 8080
swagger:
  enabled: true
  print-init: true
  docket:
    base-package: com.meiszl.Swagger2SpringBoot
    api-info:
      contact:
        name: meiszl
        email: tiaode@qq.com
        url: https://blog.csdn.net/qq_31404603

编写对应的启动类:

package com.meiszl.Swagger2SpringBoot;

import com.meiszl.Swagger2SpringBoot.Pojo.User;
import io.swagger.annotations.*;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import springfox.documentation.swagger2.annotations.EnableSwagger2;

@SpringBootApplication
@EnableSwagger2
@RestController
@Api
@ApiResponses({@ApiResponse(code = 200, message = "success", response = ResponseEntity.class)})
public class SwaggerMain {

    public static void main(String[] args){
        SpringApplication.run(SwaggerMain.class,args);
    }
    @GetMapping("/index")
    @ApiOperation(value="创建用户", notes="根据User对象创建用户")
    @ApiImplicitParam(name = "user", value = "用户详细实体user", required = true, dataType = "User")
    public String index(User User) {
        return "index";
    }

    @GetMapping("/home")
    public String home() {
        return "home";
    }

    @GetMapping("/home/test")
    public String homeTest() {
        return "test";
    }

    @GetMapping("/test")
    public String test() {
        return "test";
    }

    @GetMapping("/index/test")
    public String indexTest() {
        return "test";
    }

    @GetMapping("/index/test/a")
    public String indexTestA() {
        return "test";
    }

}

启动访问http://localhost:8080/swagger-ui.html

访问成功

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值