Springboot整合Swagger2

1.新增jar包

   <!-- https://mvnrepository.com/artifact/io.springfox/springfox-swagger2 -->
        <dependency>
            <groupId>io.springfox</groupId>
            <artifactId>springfox-swagger2</artifactId>
            <version>2.8.0</version>
        </dependency>

        <!-- https://mvnrepository.com/artifact/io.springfox/springfox-swagger-ui -->
        <dependency>
            <groupId>io.springfox</groupId>
            <artifactId>springfox-swagger-ui</artifactId>
            <version>2.8.0</version>
        </dependency>

2.新增@EnableSwagger2

@SpringBootApplication(scanBasePackages = {"org.java.com.*"})
@EnableSwagger2
public class Application {

    /**
     * 1.java -jar
     * <p>
     * 2.mvn spring-boot:run
     *
     * @param args
     */
    public static void main(String[] args) {
        SpringApplication.run(Application.class, args);
    }

}
3.新增model类
@ApiModel("用户模型")
@AllArgsConstructor
@Data
public class User implements Serializable {
    private static final long serialVersionUID = -3378006049247087150L;

    @ApiModelProperty("用户编号")
    private Long id;
    @ApiModelProperty("用户姓名")
    private String name;
    @ApiModelProperty("登陆密码")
    private String password;

}

4.controller新增注解

@Api("user相关Api")
@RestController
@RequestMapping("/firstboot")
public class FirstBootController {

    @ApiOperation("根据id获取用户信息")
    @ApiImplicitParams({@ApiImplicitParam(paramType = "query", name = "id",
            dataType = "long", required = true, value = "用户编号id")})
    @ApiResponses({
            @ApiResponse(code = 0000, message = "调用成功", response = User.class)
    })
    @GetMapping(value = "/sayHello")
    public User getUserInfo(@RequestParam("id") Long id) {
        return new User(1L, "测试", "12334");
    }
}

5.浏览器输入

http://www.localhost.com:8080/swagger-ui.html

效果如下:

查看提供服务


查看模型


请求调用


阅读更多
想对作者说点什么?

博主推荐

换一批

没有更多推荐了,返回首页