1. pom.xml导入Swagger依赖
<!--swagger3-->
<dependency><groupId>org.springdoc</groupId><artifactId>springdoc-openapi-starter-webmvc-ui</artifactId><version>2.0.2</version>
</dependency>
2.创建SwaggerConfig配置类,添加@Configuration注解
import io.swagger.v3.oas.models.OpenAPI;
import io.swagger.v3.oas.models.info.Info;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
@Configuration
public class SwaggerConfig {@Beanpublic OpenAPI springShopOpenAPI() {return new OpenAPI().info(new Info().title("接口文档").description("学生成绩管理系统API文档").version("v1"));}}
3.在controller类中(可不添加以下注解)添加@Tag、@Operation注解,注: 当你只写了一个接口时,如果只是在类上@RestController上设置接口访问路径,会出现No operations defined in spec!,这是需要在方法上再加个GetMapping/PostMapping/PutMapping/DeleteMapping就可以解决了
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.tags.Tag;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;@Tag(name = "登录管理")
@RestController
@RequestMapping("/login")
public class LoginController {@AutowiredUserService userService;/*** 登录* @param user* @return*/@Operation(summary = "查询")@GetMapping("/ling")public AjaxResult login(@RequestBody User user){User login = userService.login(user);return AjaxResult.success(login);}
}
4.访问:http://localhost:端口号/swagger-ui/index.htm