春ブーツSwagger2生成インターフェースのドキュメント

免責事項:この記事はブロガーオリジナル記事です、続くBY-SAのCC 4.0を著作権契約、複製、元のソースのリンクと、この文を添付してください。
このリンク: https://blog.csdn.net/weixin_44716935/article/details/102755988

闊歩はYAMLに基づいて、RESTfulなインターフェースで、JSON言語文書をオンラインに自動的に生成されたコードは、ツールによって自動的に生成されます。もちろん欠点があり、最も明白には比較的強いにコードです。

フロントとプロジェクトの後端より良いお尻のために、以降の転送を容易にするために、今、それがAPIインタフェースのドキュメントを書くように求めました。
1.pom.xml

    <dependency>
            <groupId>io.springfox</groupId>
            <artifactId>springfox-swagger2</artifactId>
            <version>2.9.2</version>
    </dependency>
	<!-- 网站依赖http://localhost:8080/swagger-ui.html-->
    <dependency> 
            <groupId>io.springfox</groupId>
            <artifactId>springfox-swagger-ui</artifactId>
            <version>2.9.2</version>
     </dependency>

2. [スタートクラス

@SpringBootApplication
@EnableSwagger2//表示开启Swagger
public class SwaggerApplication {
    public static void main(String[] args) {
        SpringApplication.run(SwaggerApplication.class, args);
    }
}

3.Swaggerの設定クラス

package org.javaboy.swagger.Config;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import springfox.documentation.builders.ApiInfoBuilder;
import springfox.documentation.builders.PathSelectors;
import springfox.documentation.builders.RequestHandlerSelectors;
import springfox.documentation.service.Contact;
import springfox.documentation.spi.DocumentationType;
import springfox.documentation.spring.web.plugins.Docket;
@Configuration //配置类
public class Swagger2Config {
    /**
     * org.javaboy.swagger.controller包的路径,不然生成的文档扫描不到接口
     * @return
     */
    @Bean
    Docket docket(){
        return new Docket(DocumentationType.SWAGGER_2)
                .select()
                .apis(RequestHandlerSelectors.basePackage("org.javaboy.swagger.controller"))
                .paths(PathSelectors.any())
                .build()
                .apiInfo(new ApiInfoBuilder()
                        .description("描述:人员信息用户模块...")
                        .title("标题:某公司_用户信息管理系统_接口文档")
                        .version("V1.0")
                        .contact(new Contact("大师兄","[email protected]","[email protected]"))
                        .build());
    }    
}

4.書き込みインタフェースのドキュメント

package org.javaboy.swagger.controller;

import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import org.javaboy.swagger.model.User;
import org.springframework.web.bind.annotation.*;

@RestController
@Api(tags="用户接口") //相当于类注释
public class UserController {

    @GetMapping("/hello")
    public String hello(){
        return "heeo swagger测试";
    }

    @GetMapping("/user")
    @ApiOperation("根据用户id查询") //相当于方法注释
    @ApiImplicitParam(name = "查询id",value = "用户id",defaultValue = "99") // 相当方法的参数注释
    public User getUserId(Integer id){
        User user = new User();
        user.setId(id);
        return user;
    }
    @PutMapping("/user")
    @ApiOperation("根据id更新用户名")
    @ApiImplicitParams({ //这注解能写多个参数注释
            @ApiImplicitParam(name = "id",value = "用户id",defaultValue = "99"),
            @ApiImplicitParam(name = "username",value="用户名",defaultValue = "默认值")
    })
    public User updateUsernameById(String username,Integer id){
        User user = new User();
        user.setId(id);
        user.setUsername(username);
        return user;
    }
    @DeleteMapping("/user{id}")
    @ApiOperation("根据id删除用户")
    @ApiImplicitParam(name = "id",value = "用户id",defaultValue = "99")
    public void deleteUserById(@PathVariable  Integer id){
        System.out.println(id);
    }
    @PostMapping("/user")
    @ApiOperation("根据对象添加用户")
    public User addUser(@RequestBody  User user){
        return user;
    }

}

Entityクラス

package org.javaboy.swagger.model;
import io.swagger.annotations.ApiModel;
import io.swagger.annotations.ApiModelProperty;
import java.util.Date;
@ApiModel("用户实体类") //字段比较多就注释
public class User {
    @ApiModelProperty("用户id") //注释
    private int id;
    @ApiModelProperty("用户名")
    private String username;
    @ApiModelProperty("年龄")
    private int age;
    @ApiModelProperty("时间")
    private Date ctm;

http:// localhost:8080 /闊歩 -ui.html
ここに画像を挿入説明
この良い再び変更され、中国の記事
参照記事
参照記事

おすすめ

転載: blog.csdn.net/weixin_44716935/article/details/102755988