1:pom引入
<groupId>io.springfox</groupId>
<artifactId>springfox-swagger2</artifactId>
<version>2.4.0</version>
</dependency>
<dependency>
<groupId>io.springfox</groupId>
<artifactId>springfox-swagger-ui</artifactId>
<version>2.4.0</version>
</dependency>
2:访问地址
http://localhost:8080/swagger-ui.html
3:常用注解
@ApiOperation:用在方法上,说明方法的作用
@ApiImplicitParams:用在方法上包含一组参数说明 【注意】后面有s
- @ApiImplicitParam:用在@ApiImplicitParams注解中,指定一个请求参数的各个方面
- paramType:参数放在哪个地方
- header-->请求参数的获取:@RequestHeader
- query-->请求参数的获取:@RequestParam
- path(用于restful接口)-->请求参数的获取:@PathVariable
【重点】这里就是可以传入多个参数
- name:参数名
- dataType:参数类型
- required:参数是否必须传
- value:参数的意思
- defaultValue:参数的默认值
@ApiResponses:用于表示一组响应
@ApiResponse:用在@ApiResponses中,一般用于表达一个错误的响应信息- code:数字,例如400
- message:信息,例如"请求参数没填好"
- response:抛出异常的类
@ApiModel:描述一个Model的信息(这种一般用在post创建的时候,使用@RequestBody这样的场景,请求参数无法使用@ApiImplicitParam注解进行描述的时候)
4:上代码
1:配置文件
@Configuration
@EnableSwagger2
public class SwaggerConfig {
@Bean
public Docket createRestApi() {
return new Docket(DocumentationType.SWAGGER_2)
.apiInfo(apiInfo())
.select()
.apis(RequestHandlerSelectors.withMethodAnnotation(ApiOperation.class)) //这里采用包含注解的方式来确定要显示的接口
//.apis(RequestHandlerSelectors.basePackage("com.rongzi.demo.modules.system.controller")) //这里采用包扫描的方式来确定要显示的接口
.paths(PathSelectors.any())
.build();
}
private ApiInfo apiInfo() {
return new ApiInfoBuilder()
.title("客户模块")
.description("多数据源客户文档")
.termsOfServiceUrl("http://www.baidu.com")
.contact("www.baidu.com")
.version("2.0")
.build();
}
}
2:controller
@RequestMapping("/clt_base")
@RestController
public class CltBaseController {
@Autowired
Clt_BaseService clt_baseService;
@ApiOperation(value = "获取列表", notes = "根据城市标签来获取对应列表")
@ApiImplicitParam(name = "cityFlag", value = "城市标志", required = true, paramType = "query")
@GetMapping(value = "")
public List<Clt_base> findCltBaseAllByCity(@RequestParam("cityFlag") String cityFlag) {
List<Clt_base> clt_baseByCityTagList = clt_baseService.findClt_baseByCityTag(cityFlag);
return clt_baseByCityTagList;
}
@ApiOperation(value = "删除客户", notes = "根据id和城市标志来删除指定客户")
@ApiImplicitParams({
@ApiImplicitParam(name = "id", value = "客户ID", required = true, paramType = "path")
, @ApiImplicitParam(name = "cityFlag", value = "城市标志", required = true, paramType = "query")
})
@RequestMapping(value = "/delete/{id}", method = RequestMethod.DELETE)
public Tip deleteCltBaseByCity(@PathVariable("id") int id,
@RequestParam(value = "cityFlag", required = true) String cityFlag) {
Boolean flag = clt_baseService.deleteCltBaseByIdAndCityFlag(id, cityFlag);
if (flag) {
return new SuccessTip();
} else {
return new ErrorTip(500, "操作失败");
}
}
@ApiOperation(value = "获取客户", notes = "根据id和城市标志来获取指定客户")
@ApiImplicitParams({
@ApiImplicitParam(name = "id", value = "客户ID", required = true, paramType = "path")
, @ApiImplicitParam(name = "cityFlag", value = "城市标志", required = true, paramType = "query")
})
@PostMapping(value = "/{id}")
public Clt_base getClt_BaseById(@PathVariable("id") int id, @RequestParam("cityFlag") String cityFlag) {
return clt_baseService.getCltBaseById(id, cityFlag);
}
}
5:效果图
没有啦。不知道怎么生成网站