本文已参与「新人创作礼」活动,一起开启掘金创作之路。
- 点击关注即可查看,对博主不感兴趣,看完之后即可取消关注。
之所以写博客。也是方便自己以后的查阅与帮助读者解决问题 废话不多说:
细节在于步骤:
在公共模块导入依赖:例如common
第一步依赖
<!-- swagger-->
<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>
第二步修改实体类
其他的实体按照这个格式配置,例如:orderEntity......
@ApiModel(value = "用户实体属性")
public class User implements Serializable {
/**
* 用户id
*/
@ApiModelProperty("用户id")
private Integer id;
/**
* 用户名称
*/
@ApiModelProperty("用户名称")
private String username;
/**
* 用户密码
*/
@ApiModelProperty("用户密码")
private String password;
/**
* 用户手机号
*/
@ApiModelProperty("用户手机号")
private String telephone;
}
注解意思我就不在细说:看图
第三步:来到用户微服务
添加配置文件类:
@Configuration
@EnableSwagger2
public class SwaggerConfig {
@Bean
public Docket docket(){
return new Docket(DocumentationType.SWAGGER_2)
.apiInfo(apiInfo()) //这个方法的作用(生成接口的时候页面显示的信息)
.select() //表示的是选择那些路径和API生成文档
.apis(RequestHandlerSelectors.basePackage("com.huatech.controller")) //告诉他要扫描的接口存在的这个包
.paths(PathSelectors.any()) //对所有的API进行监控
.build(); //构建
}
private ApiInfo apiInfo() {
return new ApiInfoBuilder()
.title("用户管理") //文档的标题
.description("用户微服务接口文档") //文档的描述
.version("1.0") //版本
.build();
}
}
具体配置文件啥意思看图、看注释:
第四步:用户控制层:
package com.huatech.controller;
/**
* @description: 控制层
* @author: SongXY
* @create: 2021-02-27 16:43
**/
@RestController
@RefreshScope//只需要在需要动态读取配置的类上添加此注解就可以
@RequestMapping("user")
@Api(tags = "用户模块的相关接口")
public class UserController {
@Resource
IUserService userService;
@Value("${config.name}")
private String name;
@Value("${config.env}")
private String env;
@RequestMapping(value = "finds", method = RequestMethod.GET)
@ApiOperation("查询用户集合")
public List<User> finds() {
return userService.finds();
}
@GetMapping("name")
@ApiOperation("获取配置文件的内容")
public String getName() {
return name;
}
@GetMapping("evn")
@ApiOperation("获取配置文件的内容")
public String getEvn() {
return env;
}
@GetMapping("selectUserIdAndUserName")
@ApiOperation("根据两个参数来确定一个用户")
@ApiImplicitParams({
@ApiImplicitParam(name = "id", value = "用户id", required = true, dataType = "int"),
@ApiImplicitParam(name = "name", value = "用户名", required = true, dataType = "String")
})
public User selectUserIdAndUserName(@RequestParam("id") int id, @RequestParam("name") String name) {
return userService.selectUserIdAndUserName(id, name);
}
@GetMapping("updateUser")
@ApiOperation("修改指定用户名")
@ApiImplicitParams({
@ApiImplicitParam(name = "name", value = "用户名", required = true, dataType = "String"),
@ApiImplicitParam(name = "id", value = "用户id", required = true, dataType = "int")
})
public String updateUser(@RequestParam("name") String name, @RequestParam("id") int id) {
int i = userService.updateUser(name, id);
if (i < 0) {
return "更新失败!";
}
return "更新成功";
}
@GetMapping("UserIds")
@ApiOperation("查询用户集合")
public List<User> UserIds() {
return userService.UserIds();
}
@PostMapping("insert")
@ApiOperation("插入用户")
public String insert(@RequestBody User user) {
return userService.insert(user);
}
@PostMapping("update")
@ApiOperation("更新用户")
public String update(@RequestBody User user) {
return userService.update(user);
}
@DeleteMapping("delete")
@ApiOperation("删除用户")
public String deleteIds(@RequestParam(value = "ids",required = false) List<Integer> ids) {
return userService.deleteIds(ids);
}
}
效果图: