原来看到别人用swagger,界面挺好看,也方便接口调试,就自己搭建了一个
项目还是 http://www.jianshu.com/p/6f9547e79b2b 这里面的项目,在上面加上了swagger
pom.xml
<!--swagger2-->
<dependency>
<groupId>io.springfox</groupId>
<artifactId>springfox-swagger2</artifactId>
<version>2.2.2</version>
</dependency>
<dependency>
<groupId>io.springfox</groupId>
<artifactId>springfox-swagger-ui</artifactId>
<version>2.2.2</version>
</dependency>
swagger配置文件:
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 {
@Bean
public Docket createRestApi() {
return new Docket(DocumentationType.SWAGGER_2)
.apiInfo(apiInfo()) .select()
.apis(RequestHandlerSelectors.basePackage("com.lijia.controller"))//访问controller目录
.paths(PathSelectors.any()) .build();
}
private ApiInfo apiInfo() {
return new ApiInfoBuilder()
.title("测试swagger2")
.description("还是个孩子呀!")
// .termsOfServiceUrl("http://localhost:8080/")
.contact("lijia") .version("1.0") .build();
}
}
然后再Controller上面添加描述,也可以不添加,
@ApiOperation(value = "测试swagger2",notes = "测试swagger2")
@RequestMapping(value = "/swagger",method = RequestMethod.GET)
@ResponseBody
public String swagger(){
return "helloWorld";
}
启动Application,输入
http://localhost:8080/swagger-ui.html
如果不指定method的方式,会全部出现,指定了就会出现一个。
在下面填入条件,点击try it out
主要看ResponseCode,如果正常的话是200,自己封装异常的话就看ResponseBody,比如上面的exception
网友评论