一把辛酸泪
自己刚开始使用postman测试接口,使用不方便算了,而且还不听话,还浪费了我大半天的时间,明明我的接口没毛病,它报400,我以为是自己接口写的有问题,类型不对呢。总之别用postman,感谢同事的帮助。
于是我我配置swagger,使用swagger测试。
步骤
1.首先在pom文件中引入
<!--引入swagger相关配置-->
<dependency>
<groupId>io.swagger</groupId>
<artifactId>swagger-annotations</artifactId>
<version>1.5.20</version>
</dependency>
<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>
2.添加相应swagger2类
package com.imooc.product.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.ApiInfo;
import springfox.documentation.spi.DocumentationType;
import springfox.documentation.spring.web.plugins.Docket;
import springfox.documentation.swagger2.annotations.EnableSwagger2;
@Configuration
@EnableSwagger2
public class Swagger2 {
@Bean
public Docket createRestApi() {
return new Docket(DocumentationType.SWAGGER_2)
.apiInfo(apiInfo())
.select()
.apis(RequestHandlerSelectors.basePackage("com.imooc.product.controller"))
.paths(PathSelectors.any()).build();
}
private ApiInfo apiInfo() {
return new ApiInfoBuilder().title("Springboot-api文档")
.description("").termsOfServiceUrl("")
.version("1.0").build();
}
}
// 注意:"com.imooc.product.controller" 指的是你的controller包路径
如图:
3.添加注解响应controller里面加注解
- @Api注解:添加到类上
- @ApiOperation:用在方法上
4.配置idea启动页面
5.一切正常,自己的接口也没有毛病