1.导入jar包:
<!--swagger2-->
<dependency>
<groupId>io.springfox</groupId>
<artifactId>springfox-swagger2</artifactId>
<version>2.6.1</version>
</dependency>
<dependency>
<groupId>io.springfox</groupId>
<artifactId>springfox-swagger-ui</artifactId>
<version>2.6.1</version>
</dependency>
2.编写配置:
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 Swagger2 {
@Bean
public Docket createRestApi() {
return new Docket(DocumentationType.SWAGGER_2)
.apiInfo(apiInfo())
.select()
.apis(RequestHandlerSelectors.basePackage(""))
.paths(PathSelectors.any())
.build();
}
private ApiInfo apiInfo() {
return new ApiInfoBuilder()
.title("结合swagger使用,实现restful api文档")
.description("")
.termsOfServiceUrl("https://blog.csdn.net/xxx")
.version("1.0")
.build();
}
}
3.在controller层写注解:
@ApiOperation("创建用户")
@ApiImplicitParam(name="user", value="用户实体", required=true, dataType="User")
@PostMapping("/add")
@ResponseBody
public ServerResponse<String> add(@ModelAttribute User user) {
ServerResponseDTO<String> response = userService.saveRegister(user);
if(response.isSuccess()) {
return ServerResponse.createBySuccess();
}
return ServerResponse.createByErrorMessage(response.getMsg());
}
@ApiOperation("根据id获取用户信息")
@ApiImplicitParam(name="id", value="用户Id", required=true, dataType="Integer", paramType="path")
@GetMapping("/{id}")
@ResponseBody
public ServerResponse<User> getUserById(@PathVariable Integer id) {
User user = userService.getUserById(id);
if(null != user) {
return ServerResponse.createBySuccess(user);
}
return ServerResponse.createByErrorMessage("用户不存在");
}
@ApiOperation("根据id删除用户信息")
@ApiImplicitParam(name="id", value="用户Id", required=true, dataType="Integer", paramType="path")
@DeleteMapping("/{id}")
@ResponseBody
public ServerResponse<User> deleteUserById(@PathVariable Integer id) {
ServerResponseDTO<String> deleteResponse = userService.deleteUserById(id);
if(null != deleteResponse) {
return ServerResponse.createBySuccess();
}
return ServerResponse.createByErrorMessage(deleteResponse.getMsg());
}
@ApiOperation(value="更新用户详细信息", notes="根据url的id指定更新用户,并根据传过来的user值来更新用户信息")
@ApiImplicitParams({
@ApiImplicitParam(name="id", value="用户Id", required=true, dataType="Integer", paramType="path"),
@ApiImplicitParam(name="user", value="用户详细信息实体user", required=true, dataType="User")
})
@PutMapping("/{id}")
@ResponseBody
public ServerResponse<User> updateUserById(@PathVariable Integer id, @ModelAttribute User user) {
user.setId(id);
ServerResponseDTO<User> updateResponse = userService.updateUser(user);
if(updateResponse.isSuccess()) {
return ServerResponse.createBySuccess();
}
return ServerResponse.createByErrorMessage(updateResponse.getMsg());
}
4.访问地址:
http://localhost:8080/shop/swagger-ui.html