原來(lái)看到別人用swagger葫笼,界面挺好看决记,也方便接口調(diào)試拔鹰,就自己搭建了一個(gè)
項(xiàng)目還是 http://www.reibang.com/p/6f9547e79b2b 這里面的項(xiàng)目寄疏,在上面加上了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("測(cè)試swagger2")
.description("還是個(gè)孩子呀伴榔!")
// .termsOfServiceUrl("http://localhost:8080/")
.contact("lijia") .version("1.0") .build();
}
}
然后再Controller上面添加描述,也可以不添加海铆,
@ApiOperation(value = "測(cè)試swagger2",notes = "測(cè)試swagger2")
@RequestMapping(value = "/swagger",method = RequestMethod.GET)
@ResponseBody
public String swagger(){
return "helloWorld";
}
啟動(dòng)Application迹恐,輸入
http://localhost:8080/swagger-ui.html
如果不指定method的方式,會(huì)全部出現(xiàn)卧斟,指定了就會(huì)出現(xiàn)一個(gè)殴边。
在下面填入條件,點(diǎn)擊try it out
主要看ResponseCode珍语,如果正常的話是200柄慰,自己封裝異常的話就看ResponseBody合蔽,比如上面的exception