Swagger

通过docket进行设置配置swagger,里面要的东西都可自己new一个类似的对象然后放进去
例如这个apiInfo对象

package com.***.config;

import org.springframework.beans.factory.annotation.Value;
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.ApiInfo;
import springfox.documentation.spi.DocumentationType;
import springfox.documentation.spring.web.plugins.Docket;
import springfox.documentation.swagger2.annotations.EnableSwagger2;

@Configuration
@EnableSwagger2
public class SwaggerConfig {
    @Value("${swagger2.enable}")
    private Boolean swagger2Enable;
    @Bean
    public Docket createRestApi() {
        return new Docket(DocumentationType.SWAGGER_2)
                .enable(swagger2Enable)
                .pathMapping("/")
                .apiInfo(apiInfo())
                .select()
                .apis(RequestHandlerSelectors.basePackage("com.***"))
                .paths(PathSelectors.any())
                .build();
    }

    private ApiInfo apiInfo() {
        return new ApiInfoBuilder()
                .title("*** Api Document")
                .version("1.0.0")
                .build();
    }
}

方法参数案例:


@ApiOperation(value = "接口总体描述", notes = "<span style='color:red;'>详细描述:</span>&nbsp;方法详细描述信息")
@ApiImplicitParams({
        @ApiImplicitParam(name = "username", value = "用户名", dataType = "String", defaultValue = "qlh"),
        @ApiImplicitParam(name = "password", value = "密码", dataType = "String", defaultValue = "123")
})
@PostMapping("/")
public String login(String username, String password) {
    return "Hello login ~";
}


 @ApiImplicitParam(
            name = "username", value = "用户的用户名-String", required = true, paramType="body"
    )

@RequestBody

@ApiOperation("更新个人信息")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "wechatId", value = "微信ID", dataTypeClass = MemberInfo.MemberParams.class),
            @ApiImplicitParam(name = "nickName", value = "昵称", dataTypeClass = MemberInfo.MemberParams.class),
            @ApiImplicitParam(name = "headerImage", value = "微信头像连接", dataTypeClass = MemberInfo.MemberParams.class),
    })
    @PostMapping("/pub/member/updateMy")
    public ResponseObject updateMemberInfo(@ApiIgnore @RequestBody MemberInfo.MemberParams params) {

还可以直接在实体类通过

@ApiModel
@ApiModelProperty

@Data
    @EqualsAndHashCode(callSuper = false)
    @ApiModel(value="MemberParams",description = "MemberParams")
    public static class MemberParams{
        @ApiModelProperty(value = "微信wechatId",required = true, example = "wechatId")
        private String wechatId;
        @ApiModelProperty(value = "昵称", example = "花花")
        private String nickName;
    }

posted @ 2022-06-21 16:15  Arborblog  阅读(33)  评论(0编辑  收藏  举报