1、导入启动器和相关依赖
<dependency>
<groupId>io.springfox</groupId>
<artifactId>springfox-boot-starter</artifactId>
<version>3.0.0</version>
</dependency>
<dependency>
<groupId>com.github.xiaoymin</groupId>
<artifactId>swagger-bootstrap-ui</artifactId>
<version>1.9.6</version>
</dependency>
2.配置整合
(1)必做
在启动类上加:@EnableOpenApi
也可以加在swagger配置类上。
@SpringBootApplication
@EnableOpenApi
public class Demo01Application {
public static void main(String[] args) {
SpringApplication.run(Demo01Application.class, args);
}
}
(2)选做
在根目录下创建config文件,写swagger的配置文件
@Configuration
public class Swagger3Config {
@Bean
public Docket getDocket() {
return new Docket(DocumentationType.OAS_30).apiInfo(ApiInfo());
}
@Bean
public ApiInfo ApiInfo(){
return new ApiInfoBuilder()
.title("客户关系管理系统-接口文档")
.description("这是**的crm!!!!")
.contact(new Contact("username","url","email"))
.version("v1.0")
.build();
}
}
3.swagger注解
(1)controller请求接口
@Api
加在controller类前。描述类的作用。
@ApiOperation
加在controller中的方法前。描述方法的作用。
@ApiImplicitParams
加在controller中的方法前。描述方法的参数(请求参数)。
@ApiResponses
加在controller中的方法前。描述方法的返回值(响应json数据)。
@Controller
@Api(tags = "客户管理接口",value = "客户CRUD操作")
public class CustomerController {
@Autowired
private CustomerService customerService;
@GetMapping("/customer/findAllCustomer")
public ModelAndView findAllCustomer(int pageNum,int pageSize){
ModelAndView mv=new ModelAndView();
PageInfo<Customer> pageInfo= customerService.findAllCustomer(pageNum,pageSize);
mv.addObject("pageInfo",pageInfo);
mv.setViewName("/customer/customers");
return mv;
}
@PostMapping("/customer/addCustomer")
@ResponseBody
@ApiOperation(value = "添加客户",notes = "this is add ...")
@ApiImplicitParams({
@ApiImplicitParam(name = "CId",value = "客户编号",paramType = "header"),
@ApiImplicitParam(name = "CName",value = "客户姓名",required = true),
@ApiImplicitParam(name = "cSex",value = "客户性别",paramType = "query",defaultValue = "男"),
@ApiImplicitParam(name = "CTel",value = "客户电话"),
@ApiImplicitParam(name = "CJob",value = "客户职位",paramType = "path"),
@ApiImplicitParam(name = "CCompany",value = "客户所属公司")
})
@ApiResponses({
@ApiResponse(code = 1001,message = "操作成功")
})
public String addCustomer(Customer customer){
return "add ok";
}
@GetMapping("/customer/findCustomerByCid")
@ResponseBody
@ApiOperation(value = "根据cid查找客户")
@ApiImplicitParams({
@ApiImplicitParam(name = "cid" ,value = "客户编号")
})
public Customer findCustomerByCid(int cid){
return new Customer();
}
@GetMapping("/customer/findCustomerByCid2")
@ResponseBody
@ApiOperation(value = "根据cid查找客户222")
@ApiImplicitParams({
@ApiImplicitParam(name = "cid" ,value = "客户编号")
})
public Customer2 findCustomerByCid2(int cid){
return new Customer2();
}
}
(2)domain实体类
@ApiModel
加在实体类前,用于描述实体类的作用。
@ApiModelProperty
加在实体类的属性前,用于描述实体类的属性。
@Data
@ApiModel
public class Customer2 {
@ApiModelProperty(name = "ccId", value = "客户编号")
private int ccId;
@ApiModelProperty(name = "ccName", value = "客户姓名")
private String ccName;
@ApiModelProperty(name = "ccSex", value = "客户性别")
private String ccSex;
@ApiModelProperty(name = "ccTel", value = "客户电话")
private String ccTel;
@ApiModelProperty(name = "ccJob", value = "客户职位")
private String ccJob;
@ApiModelProperty(name = "ccCompany", value = "客户所属公司")
private String ccCompany;
}
(3)注意事项(踩坑教训)
命名规范:
java属性名必须以两个及以上的小写字母开头!!!!
这意味着,数据库列名如果有多个单词构成,第一个单词必须有两个及以上的字母!!!!!