首先要在启动类上添加注解@EnableSwagger2Doc
在pom.xml文件中添加坐标:
<dependency>
<groupId>com.spring4all</groupId>
<artifactId>swagger-spring-boot-starter</artifactId>
<version>1.9.0.RELEASE</version>
<!--排除自带 UI 依赖-->
<exclusions>
<exclusion>
<artifactId>springfox-swagger-ui</artifactId>
<groupId>io.springfox</groupId>
</exclusion>
</exclusions>
</dependency>
<dependency>
<groupId>com.github.xiaoymin</groupId>
<artifactId>swagger-bootstrap-ui</artifactId>
<version>1.9.2</version>
</dependency>
控制层代码
@RestController
@Api(tags = "用户的控制层")
public class UserController {
@Autowired
private UserService userService;
/**
* 查询
* @param id
* @return
*/
@GetMapping("user/{id}")
@ApiOperation(value = "根据id进行查询",notes = "备注")
@ApiImplicitParam(value = "主键",type = "path",name = "id",required = true,dataType = "int")
public User findById(@PathVariable("id") Integer id){
return userService.findById(id);
}
@PostMapping("addUser")
public String addUser(User user){
userService.addUser(user);
return "新增成功";
}
}
访问页面展示效果: