在Spring Boot项目中配置sawgger

认识Swagger

Swagger 是一个规范和完整的框架,用于生成、描述、调用和可视化 RESTful 风格的 Web 服务。总体目标是使客户端和文件系统作为服务器以同样的速度来更新。

作用:

    1. 接口的文档在线自动生成。

    2. 功能测试。

1.在pom文件中添加一下依赖

	<!--swagger依赖-->
		<dependency>
			<groupId>io.springfox</groupId>
			<artifactId>springfox-swagger2</artifactId>
			<version>2.9.2</version>
		</dependency>
		<dependency>
			<groupId>io.springfox</groupId>
			<artifactId>springfox-swagger-ui</artifactId>
			<version>2.9.2</version>
		</dependency>
		<dependency>
			<groupId>io.swagger</groupId>
			<artifactId>swagger-annotations</artifactId>
			<version>1.5.21</version>
		</dependency>
		<dependency>
			<groupId>io.swagger</groupId>
			<artifactId>swagger-models</artifactId>
			<version>1.5.21</version>
		</dependency>

第二步,配置swagger的配置类


@Configuration
public class Swagger2Configuration {

    @Bean
    public Docket createRestApi() {
        return new Docket(DocumentationType.SWAGGER_2)
                .apiInfo(apiInfo())
                .select()
                .apis(RequestHandlerSelectors.withClassAnnotation(Api.class))//这是注意的代码
                .paths(PathSelectors.any())
                .build();
    }

    private ApiInfo apiInfo() {
        return new ApiInfoBuilder()
                .title("小米商城接口文档")
                .description("小米商城相关接口的文档")
                .termsOfServiceUrl("http://localhost:2333/login")
                .version("1.0")
                .build();
    }

}

第三步,在启动类上加上  @EnableSwagger2  注解


@EnableSwagger2
@SpringBootApplication
public class KsApplication {

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

}

第四步,接收对象传参的例子  

在pojo上


@Data
@ApiModel("购物车")
public class Cart {
    @ApiModelProperty( "购物车ID")
    private int ID;

    @ApiModelProperty( "用户ID")
    private int userId;

    @ApiModelProperty( "商品ID")
    private int shopId;

    @ApiModelProperty( "数量")
    private int count;
    
    @ApiModelProperty( "合计")
    private int collectType;

第五步:添加文档内容,

Swagger使用的注解及其说明:

@Api:用在类上,说明该类的作用。

@ApiOperation:注解来给API增加方法说明。

@ApiImplicitParams : 用在方法上包含一组参数说明。

@ApiImplicitParam:用来注解来给方法入参增加说明。

@ApiResponses:用于表示一组响应

@ApiResponse:用在@ApiResponses中,一般用于表达一个错误的响应信息

    l   code:数字,例如400

    l   message:信息,例如"请求参数没填好"

    l   response:抛出异常的类   

@ApiModel:描述一个Model的信息(一般用在请求参数无法使用@ApiImplicitParam注解进行描述的时候)

    l   @ApiModelProperty:描述一个model的属性
 

在controller上


@Api(value = "管理员模块")
@Controller
@Slf4j
@RequestMapping("api/v1/admin")
public class AdminController {
    @Autowired
    private AdminService adminService;
    @Autowired
    private UserService userService;

    //管理员登录
    @PostMapping("/login")
    @ResponseBody
    @ApiOperation(
            value = "管理员登录",
            notes = "管理员登录",
            response = Results.class
    )
    public Results AdminLogin(String username,String password){
        return adminService.adminLogin(username,password);
    }

完成上述代码添加上,启动Spring Boot程序,访问:http://localhost:8080/swagger-ui.html

注:根据自己项目的端口号来访问

  • 1
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值