• springboot


    回顾spring

    • 轻量级的:指的是spring核心功能的jar包不大。
    • 非侵入式的:业务代码不需要继承或实现spring中任何的类或接口
    • IOC:控制反转(Inverse of Control),以前项目都是在哪儿用到对象,在哪儿new,把生成对象的权利反转给spring框架,可以对对象进行功能的增强(让spring把对象管理起来,在哪用在哪注入)
    • AOP:面向切面编程。AOP 是一种编程思想,是面向对象编程(OOP)的一种补充。面向对象编程将程序抽象成各个层次的对象,而面向切面编程是将程序抽象成各个切面。

    缺点:

    • 配置非常麻烦,且配置模板化(都一样的)
    • 需要开发人员添加许多的依赖,且依赖之间的版本不好控制

    SpringBoot对以上spring的两个缺点进行补充,主要是对spring框架的搭建进行封装简化。不是说就不需要配置了,只是换了一种方式进行配置,并把许多模板化配置直接整合了。

    springBoot是基于spring框架,对spring框架的搭建进行封装,不是代替spring,spring的核心功能不变的。可以快速搭建,并集成其他的框架和组件。

    约定(大家公认的一种做法)大于配置的核心思想

    SpringBoot特点

    1. 创建独立的spring应用程序。
    2. 直接内嵌 tomcat(一个项目就是一个服务,就是微服务的思想)、jetty 和 undertow
    3. 提供了固定化的"starter"配置,以简化构建配置,尽可能的自动配置spring和第三方库。
    4. 提供产品级的功能,如:安全指标、运行状况监测和外部化配置等。
    5. 绝对不会生成代码,并且不需要 XML 配置,提供了另外一种方式进行配置。
    搭建

    一个基于springboot的spring应用程序

    1.去官方选择配置 https://start.spring.io/

    2.选择配选项,在线生成并下载。解压后修改 pom.xml 版本为 2.6.6,删除mvn 文件夹、mvnw.cmd 文件下载,解压 修改pom.xml,删除不需要的文件。

    3.导入到idea。选择Open or Import

    4.开发一个controller(spring、springweb直接集成搭建好)

    1. package com.ffyc.news.web;
    2. @RestController
    3. @RequestMapping(path = "loginCtl")
    4. public class LoginController {
    5. @RequestMapping(path = "/test")
    6. public String test(){
    7. System.out.println("hello");
    8. return "hello";
    9. }
    10. }

    5.ip:端口/控制地址/方法地址。

    127.0.0.1:8080/loginCtl/test,访问8080就是访问这个项目

    1. @SpringBootApplication
    2. public class NewsApplication {
    3. /*spring应用程序的启动类, 必须放置在其他类的上一级包中
    4. @ComponentScan() 扫描本包中所有的类*/
    5. public static void main(String[] args) {
    6. SpringApplication.run(NewsApplication.class, args);
    7. }
    8. }

    springBoot 的核心功能

    1. 起步依赖:会把项目中使用到的相关的组件自动依赖进来(json,日志...)
    2. 自动配置springboot会根据项目中依赖的相关组件,在启动时,自动创建并初始化。配置web,启动时就会自动的去加载web相关的组件,例如DispatcherServlet(约定大于配置)。

    spring Boot 配置文件

    Spring Boot 使用一个全局的配置文件(配置文件名是固定的

    1.application.properties 属性文件格式,键值对存储数据,把配置文件中的值都存放在此文件中

    server.port=8080 

    2.application.yml ,yml 是 yaml(YAML Ain’t Markup Language)语言的文件,以数据为中心。

    yml 配置示例:

    1. #配置内置服务器的端口号
    2. server:
    3. port: 9999

    key:空格value 以此来表示一对键值对(空格不能省略);以空格的缩进来表示层级关系,只要是左边对齐的一列数据都是同一个层级的。

    值的写法:

    • 字面量:普通的值[数字,布尔值,字符串]
    • 字面量直接写在后面就可以,字符串默认不用加上双引号或者单引号;

    案例:

    1. user:
    2. name: zhangsan
    3. age: 20

    使用@Value 注解标签将配置文件中的内容映射到类中的属性。@Value("${user.name}")

    springboot添加spring中的jdbc模块

    功能:1.提供了JdbcTemplate 2.事务管理(重点)

    1.为了连接数据库需要引入 jdbc 支持,在 pom.xml 中引入如下配置:

    1. <dependency>
    2. <groupId>org.springframework.bootgroupId>
    3. <artifactId>spring-boot-starter-jdbcartifactId>
    4. dependency>
    5. <dependency>
    6. <groupId>mysqlgroupId>
    7. <artifactId>mysql-connector-javaartifactId>
    8. <version>8.0.16version>
    9. dependency>

    2.在 application.yml 中配置数据源信息

    1. spring:
    2. #配置数据库的链接库信息,生成默认的数据源对象,并生成jdbcTemplate,事务管理功能都会进行初始化
    3. datasource:
    4. url: jdbc:mysql://127.0.0.1:3306/ssmdb?serverTimezone=Asia/Shanghai
    5. username: root
    6. password: 123456
    7. driver-class-name: com.mysql.cj.jdbc.Driver

    3.测试

    1. @RestController
    2. @RequestMapping(path = "/loginCtl")
    3. public class LoginController {
    4. @Autowired
    5. JdbcTemplate jdbcTemplate;
    6. @Transactional
    7. @RequestMapping(path = "/test")
    8. public String test(){
    9. jdbcTemplate.execute("insert into admin(account)values ('aaaaaaaaaa')");
    10. System.out.println(10/0);
    11. jdbcTemplate.execute("insert into admin(account)values ('bbbbbbbbbb')");
    12. System.out.println("hello");
    13. return "hello";
    14. }
    15. }
    springBoot 整合阿里数据源

    1.导入阿里数据源 jar

    1. <dependency>
    2. <groupId>com.alibabagroupId>
    3. <artifactId>druidartifactId>
    4. <version>1.1.10version>
    5. dependency>

    2.在 yml 文件中注册阿里数据库连接池

    • Size: 5 初始化时建立物理连接的个数
    • minIdle: 1 最小连接池数量
    • maxActive: 20 最大连接池数量
    1. spring:
    2. #配置数据库的链接库信息,生成默认的数据源对象,并生成jdbcTemplate,事务管理功能都会进行初始化
    3. datasource:
    4. url: jdbc:mysql://127.0.0.1:3306/ssmdb?serverTimezone=Asia/Shanghai
    5. username: root
    6. password: 123456
    7. driver-class-name: com.mysql.cj.jdbc.Driver
    8. type: com.alibaba.druid.pool.DruidDataSource #指定数据源类型,还需要创建DruidDataSource对象
    9. initialSize: 5 #数据库连接池相关的配置
    10. maxActive: 20
    11. minIdle: 1

    3.添加配置类,生成DruidDataSource对象类

    1. //配置注解,表示此类是springBoot项目中一个配置类,sprngboot启动时会扫描
    2. @Configuration
    3. public class DruidDataSourceConfig {
    4. //@Bean == 作用在方法上,方法中会产生一个对象,最终把此对象交给spring容器
    5. @Bean
    6. @ConfigurationProperties(prefix = "spring.datasource")
    7. public DataSource druid() {
    8. //创建DruidDataSource对象,并从yml配置文件读取配置值,赋值
    9. DruidDataSource dataSource = new DruidDataSource();
    10. //dataSource.setInitialSize();
    11. return dataSource;
    12. }
    13. }
    springBoot 集成 mybatis

    1.导入jar

    1. <dependency>
    2. <groupId>org.mybatis.spring.bootgroupId>
    3. <artifactId>mybatis-spring-boot-starterartifactId>
    4. <version>2.1.4version>
    5. dependency>

    2.在application.yml文件中配置mybatis

    1. #mybatis配置,创建sqlsessionFactory
    2. mybatis:
    3. type-aliases-package: com.ffyc.news.model
    4. mapper-locations: classpath:mappers/*Mapper.xml
    5. configuration: #mybatis中settings配置
    6. map-underscore-to-camel-case: true #mysql和java的驼峰对应
    7. cache-enabled: true #缓存

    3.启动类上添加@MapperScan("接口所在的包地址"),或者在接口上添加@Mapper

    1. @SpringBootApplication
    2. @MapperScan("com.ffyc.news.dao") //扫描接口所在的包,生成接口的代理对象
    3. public class NewsApplication {
    4. /*spring应用程序的启动类, 必须放置在其他类的上一级包中
    5. @ComponentScan() 扫描本包中所有的类*/
    6. public static void main(String[] args) {
    7. SpringApplication.run(NewsApplication.class, args);
    8. }
    9. }
    1. @RestController
    2. @RequestMapping(path = "/loginCtl")
    3. public class LoginController {
    4. @Autowired
    5. LoginService loginService;
    6. @Transactional
    7. @RequestMapping(path = "/login")
    8. public String login(){
    9. loginService.login(new Admin());
    10. return "hello";
    11. }
    12. }
    1. @Service
    2. @Transactional
    3. public class LoginService {
    4. @Autowired
    5. LoginDao loginDao;
    6. public Admin login(Admin admin){
    7. Admin admin1=loginDao.login(admin);
    8. return admin1;
    9. }
    10. }
    1. @Repository
    2. public interface LoginDao {
    3. Admin login(Admin admin);
    4. }
    1. "com.ffyc.news.dao.LoginDao">

    springboot中的注解标签

    1.@SpringBootApplication:springboot中最核心注解标签,是一个组合注解标签,里面包含3个:@SpringBootConfiguration、@EnableAutoConfiguration、@ComponentScan

    2.@Configuration:作用在类上面,表明此类是一个配置类,启动时就会自动的去加载

    @Bean:作用在方法上,此方法会返回一个对象,交给spring框架管理

    1. @Configuration//配置注解,表示此类是springBoot项目中一个配置类,sprngboot启动时会扫描
    2. public class DruidDataSourceConfig {
    3. //@Bean == 作用在方法上,此方法产生的对象,最终返回并交给spring容器管理
    4. @Bean
    5. @ConfigurationProperties(prefix = "spring.datasource")
    6. //创建DruidDataSource对象,并从yml配置文件读取配置值,赋值
    7. public DataSource druid() {
    8. DruidDataSource dataSource = new DruidDataSource();
    9. //dataSource.setInitialSize();
    10. return dataSource;
    11. }
    12. }

    @Bean等同于这个:

    1. <bean id="dataSource" class="com.alibaba.druid.pool.DruidDataSource">
    2. <property name="driverClassName" value="${classDriverName}">property>
    3. <property name="url" value="${url}">property>
    4. <property name="username" value="${uname}">property>
    5. <property name="password" value="${pwd}">property>
    6. <property name="initialSize" value="5">property>
    7. <property name="maxActive" value="10">property>
    8. bean>

    3.@ConfigurationProperties(prefix = "spring.datasource") 可以将application.yml文件中的内容读取到,并赋给对应的属性

    @Value("${user.name}") 可以将application.yml文件中的内容读取到,并赋给对应的属性

    1. filePath:
    2. path1: D:/
    3. path2: E:/
    1. @SpringBootTest
    2. class NewsApplicationTests {
    3. @Value("${filePath.path1}")
    4. String path1;
    5. @Value("${filePath.path2}")
    6. String path2;
    7. @Test
    8. void contextLoads() {
    9. System.out.println(path1+"::"+path2);
    10. }
    11. }

    验证token的拦截器

    1. package com.ffyc.news.common;
    2. import com.fasterxml.jackson.databind.json.JsonMapper;
    3. import org.springframework.web.servlet.HandlerInterceptor;
    4. import javax.servlet.http.HttpServletRequest;
    5. import javax.servlet.http.HttpServletResponse;
    6. //token验证的拦截器
    7. public class Token implements HandlerInterceptor {
    8. @Override
    9. public boolean preHandle(HttpServletRequest request, HttpServletResponse response, Object handler) throws Exception {
    10. String adminToken=request.getHeader("adminToken");
    11. boolean res=JWTUtil.verify(adminToken);
    12. if(!res){
    13. CommonResult commonResult=new CommonResult(401,"token失效");
    14. JsonMapper jsonMapper=new JsonMapper();
    15. String json=jsonMapper.writeValueAsString(commonResult);
    16. response.getWriter().print(json);
    17. }
    18. return res;
    19. }
    20. }
    1. package com.ffyc.news.common;
    2. import org.springframework.context.annotation.Configuration;
    3. import org.springframework.web.servlet.config.annotation.*;
    4. @Configuration
    5. public class WebConfig implements WebMvcConfigurer{
    6. public void addInterceptors(InterceptorRegistry registry) {
    7. InterceptorRegistration inter = registry.addInterceptor(new Token());
    8. inter.addPathPatterns("/admin/**"); //管理员需要拦截过滤地址
    9. inter.excludePathPatterns("/admin/loginCtl/login");//放行地址
    10. }
    11. }

    springboot解决跨域

    1. package com.ffyc.news.config;
    2. import org.springframework.context.annotation.Bean;
    3. import org.springframework.context.annotation.Configuration;
    4. import org.springframework.web.cors.CorsConfiguration;
    5. import org.springframework.web.cors.UrlBasedCorsConfigurationSource;
    6. import org.springframework.web.filter.CorsFilter;
    7. import java.util.Collections;
    8. @Configuration
    9. public class CorsConfig {
    10. //跨域配置
    11. @Bean
    12. public CorsFilter corsFilter() {
    13. CorsConfiguration corsConfiguration = new CorsConfiguration();
    14. //1,允许任何来源
    15. corsConfiguration.setAllowedOriginPatterns(Collections.singletonList("*"));
    16. //2,允许任何请求头
    17. corsConfiguration.addAllowedHeader(CorsConfiguration.ALL);
    18. //3,允许任何方法
    19. corsConfiguration.addAllowedMethod(CorsConfiguration.ALL);
    20. //4,允许凭证
    21. corsConfiguration.setAllowCredentials(true);
    22. UrlBasedCorsConfigurationSource source = new UrlBasedCorsConfigurationSource();
    23. source.registerCorsConfiguration("/**", corsConfiguration);
    24. return new CorsFilter(source);
    25. }
    26. }

    统一异常处理

    一旦web层出现异常,就会被globalException拦截到

    1. package com.ffyc.news.common;
    2. //使用面向切面的思想,将Controller层中所出现的异常进行统一的捕获
    3. @RestControllerAdvice
    4. public class GlobalExceptionHandler {
    5. //统一异常处理
    6. @ExceptionHandler(Exception.class)
    7. public CommonResult globalException(Exception e) {
    8. System.out.println("aaaaa"+e.getMessage());
    9. CommonResult commonResult = new CommonResult(500,e.getMessage());
    10. return commonResult;
    11. }
    12. }

    项目中集成日志功能

    最早调式程序:使用System.out.println(res); 直观的看到结果,跟踪程序运行过程。

    1.为什么要在程序中添加日志功能?

    日志记录:记录程序执行过程的痕迹。不能是使用System.out.println(res);输出,平常练习可以,正式环境没有控制台。最好是将程序运行中的必要的数据,输出到一个文件中,做到长久保存日志文件。

    主要是为了方便我们监测生产环境的变量值变化以及代码运行轨迹等,记录生产环境中的值(入参,返回值),运行到了哪个方法(位置),将信息输出到文件中,对于分析锁定问题有很好的帮助。

    2.如何快速的实现日志功能

    将日志信息输出到文件中,在市面上有很多的日志组件,可以帮助我们快速的实现日志功能。

    常见的日志组件:

    • slf4j
    • commons-logging
    • Log4J
    • Logback

    前两个是日志上层功能定义的接口,后两个主要是底层的实现。一般首选强烈推荐使用 slf4j + logback,在spring依赖中已经集成了。

    提供的日志组件功能就更加的全面,例如: 将日志分为不同的等级,可以在不同的情况下,输出不同等级的日志信息

    从低到高:debug(开发,练习)

    • debug 包含debug,以及以上的日志都可以输出
    • info 包含info,以及以上的日志都可以输出
    • warn 包含warn,error
    • error 只输出error

    1.在类中使用

    1. #日志配置
    2. logging:
    3. level:
    4. com.ffyc.news: debug #日志级别
    5. file:
    6. name: E:/log/log.log #日志位置
    1. @RestController
    2. @RequestMapping(path = "/admin/loginCtl")
    3. public class LoginController {
    4. //创建logger对象(slf4j中的)
    5. static Logger logger= LoggerFactory.getLogger(LoginController.class);
    6. @Autowired
    7. LoginService loginService;
    8. //后端可以接收前端提交的json数据,但是必须是对象接收,必须在参数的前面添加@RequestBody
    9. @RequestMapping(path = "/login")
    10. public CommonResult login(@RequestBody Admin admin){
    11. //在需要的位置调用具体的日志级别方法输出
    12. logger.debug("进入到后端登录功能:account={},password={}",admin.getAccount(),admin.getPassword());
    13. logger.info("进入到后端登录功能:account={},password={}",admin.getAccount(),admin.getPassword());
    14. logger.warn("进入到后端登录功能:account={},password={}",admin.getAccount(),admin.getPassword());
    15. logger.error("进入到后端登录功能:account={},password={}",admin.getAccount(),admin.getPassword());
    16. Admin a=loginService.login(admin);
    17. logger.debug("登录完成:account={},password={}",a.getAccount(),a.getPassword());
    18. if(a!=null){
    19. return new CommonResult(200,a,"登录成功");
    20. }
    21. return new CommonResult(201,"账号或密码错误");
    22. }
    23. }

    2.SpringBoot使用AOP统一打印日志

    导入依赖 jar

    1. <dependency>
    2. <groupId>org.springframework.bootgroupId>
    3. <artifactId>spring-boot-starter-aopartifactId>
    4. dependency>

    定义切面通知类

    1. package com.ffyc.news.common;
    2. import org.aspectj.lang.JoinPoint;
    3. import org.aspectj.lang.annotation.AfterReturning;
    4. import org.aspectj.lang.annotation.Aspect;
    5. import org.aspectj.lang.annotation.Before;
    6. import org.aspectj.lang.annotation.Pointcut;
    7. import org.slf4j.Logger;
    8. import org.slf4j.LoggerFactory;
    9. import org.springframework.stereotype.Component;
    10. import org.springframework.web.context.request.RequestContextHolder;
    11. import org.springframework.web.context.request.ServletRequestAttributes;
    12. import javax.servlet.http.HttpServletRequest;
    13. import java.util.Enumeration;
    14. @Component
    15. @Aspect
    16. public class LogAspect {
    17. private Logger logger = LoggerFactory.getLogger(getClass());
    18. //通配符,定义规则,哪些方法可以被我的切面切到,这里的意思是不限返回值和方法和参数
    19. @Pointcut("execution(public * com.ffyc.news.web..*.*(..))")
    20. public void webLog() {
    21. }
    22. @Before("webLog()")
    23. public void doBefore(JoinPoint joinPoint) {
    24. ServletRequestAttributes attributes = (ServletRequestAttributes)
    25. RequestContextHolder.getRequestAttributes();
    26. HttpServletRequest request = attributes.getRequest();
    27. // 记录下请求内容
    28. logger.info("HTTP_METHOD :{} ", request.getMethod());
    29. logger.info("IP : {}", request.getRemoteAddr());
    30. //获取所有请求参数
    31. Enumeration enu = request.getParameterNames();
    32. while (enu.hasMoreElements()) {
    33. String name = (String) enu.nextElement();
    34. logger.info("name:{},value:{}", name, request.getParameter(name));
    35. }
    36. }
    37. /*@AfterReturning(returning = "ret", pointcut = "webLog()")
    38. public void doAfter(Object ret) throws Throwable {
    39. // 处理完请求,返回内容
    40. logger.info("RESPONSE : " + ret);
    41. }*/
    42. }

    Swagger组件

    现在前后端都是自己在进行开发,前端向后端提交什么数据,后端向前端返回什么数据都是知道的。但是前后端实际开发中,是由不同的人员进行开发的,需要后端开发人员向前端开发人员提供一个说明文档,后期有可能要修改文档,重新发送给前端开发人员。 现在有很多的一些组件,可以帮助我们来在线生成API文档,在类或方法上添加一些注解标签,就可以生成文档,修改后会自动的更新。

    但是前后端实际开发中,是由不同的人员进行开发的。

    需要后端开发人员向前端开发人员提供一个说明文档

    登录

    接口功能:登录

    接口地址:/admin/loginCtl/login

    提交数据:请求头: token 请求体: account: password:

    返回状态码:200成功、500报错 201 202

    Swagger功能:

    • 支持API自动生成同步的在线文档:使用 Swagger 后可以直接通过代码生成文档,不再需要自己手动编写接口文档了,对程序员来说非常方便,可以节约写文档的时间去学习新技术。
    • 提供Web页面在线测试API:光有文档还不够,Swagger 生成的文档还支持在线测试。参数和格式都定好了,直接在界面上输入参数对应的值即可在线测试接口。
    搭建

    1.导入 jar

    1. <dependency>
    2. <groupId>io.springfoxgroupId>
    3. <artifactId>springfox-swagger2artifactId>
    4. <version>2.9.2version>
    5. dependency>
    6. <dependency>
    7. <groupId>io.springfoxgroupId>
    8. <artifactId>springfox-swagger-uiartifactId>
    9. <version>2.9.2version>
    10. dependency>

    2.在 application.yml 中配置

    1. spring:
    2. mvc:
    3. pathmatch:
    4. matching-strategy: ant_path_matcher

    3.添加 swagger 配置类

    • 配置 swagger 扫描方式
    1. package com.ffyc.news.config;
    2. import io.swagger.annotations.Api;
    3. import org.springframework.context.annotation.Bean;
    4. import org.springframework.context.annotation.Configuration;
    5. import springfox.documentation.builders.ApiInfoBuilder;
    6. import springfox.documentation.builders.PathSelectors;
    7. import springfox.documentation.builders.RequestHandlerSelectors;
    8. import springfox.documentation.service.ApiInfo;
    9. import springfox.documentation.service.Contact;
    10. import springfox.documentation.spi.DocumentationType;
    11. import springfox.documentation.spring.web.plugins.Docket;
    12. import springfox.documentation.swagger2.annotations.EnableSwagger2;
    13. @Configuration
    14. @EnableSwagger2
    15. public class SwaggerConfig {
    16. /**
    17. * 创建API应用
    18. * apiInfo() 增加API相关信息
    19. * 通过select()函数返回一个ApiSelectorBuilder实例,用来控制哪些接口暴露给Swagger来展现,
    20. * 本例采用指定扫描的包路径来定义指定要建立API的目录。
    21. * @return
    22. */
    23. @Bean
    24. public Docket createRestApi() {
    25. return new Docket(DocumentationType.SWAGGER_2)
    26. .apiInfo(apiInfo())
    27. .enable(true)
    28. .select()
    29. .apis(RequestHandlerSelectors.withClassAnnotation(Api.class))
    30. .paths(PathSelectors.any())
    31. .build();
    32. }
    33. /**
    34. * 创建该API的基本信息(这些基本信息会展现在文档页面中)
    35. * 访问地址:http://项目实际地址/swagger-ui.html
    36. * @return
    37. */
    38. private ApiInfo apiInfo() {
    39. return new ApiInfoBuilder()
    40. .title("Spring Boot中使用Swagger2构建RESTful APIs")
    41. .description("更多请关注http://www.baidu.com")
    42. .termsOfServiceUrl("http://www.baidu.com")
    43. .contact(new Contact("a", "b", "c"))
    44. .version("1.0")
    45. .build();
    46. }
    47. }
    • 及放行静态资源请求不被 spring 拦截

    web层配置 /,所有访问后端的请求,都会进入到DispatcherServLet,swagger组件生成的api是网页格式的,是在后端生成的,势必就会访问后端swagger的html页面。访问swagger-ui.html时,就会被DispatcherServlet拦截,所以需要配置访问swagger相关页面时,不让其进入到DispatcherServLet

    1. package com.ffyc.news.common;
    2. import org.springframework.context.annotation.Configuration;
    3. import org.springframework.web.servlet.config.annotation.*;
    4. @Configuration
    5. public class WebConfig implements WebMvcConfigurer{
    6. public void addInterceptors(InterceptorRegistry registry) {
    7. InterceptorRegistration inter = registry.addInterceptor(new Token());
    8. inter.addPathPatterns("/admin/**"); //管理员需要拦截过滤地址
    9. inter.excludePathPatterns("/admin/loginCtl/login");//放行地址
    10. //放行行前台首页,文章详细信息等地址
    11. inter.excludePathPatterns("/swagger*/**"); //放行swagger
    12. inter.excludePathPatterns("/v2/**");//放行swagger
    13. inter.excludePathPatterns("/webjars/**");//放行swagger
    14. }
    15. @Override
    16. public void addResourceHandlers(ResourceHandlerRegistry registry) {
    17. registry.addResourceHandler("swagger-ui.html").addResourceLocations(
    18. "classpath:/META-INF/resources/");
    19. registry.addResourceHandler("/webjars/**").addResourceLocations(
    20. "classpath:/META-INF/resources/webjars/");
    21. }
    22. }
    Swagger 使用的注解及其说明

    主要用在控制器类和model类上面

    1、@Api(tags="类的功能描述"):用在类上,说明该类的作用

    2、@ApiOperation(value="方法概述", notes="功能详细描述"):用在方法上,用于对方法功能说明

    3、@ApiImplicitParam:用来注解来给方法入参增加说明。

    paramType 属性表示参数放在哪里,主要有以下几个属性:

    • header-->放在请求头。请求参数的获取:@RequestHeader(代码中接收注解)
    • query-->用于 get 请求的参数拼接。请求参数的获取
    • path(用于 restful 接口)-->请求参数的获取:@PathVariable(代码中接收注解)
    • body-->放在请求体。请求参数的获取:@RequestBody(代码中接收注解)

    4、参数为实体类时,在实体类上添加注解

    • @ApiModel:描述一个Model类的信息(参数为实体类时使用)
    • @ApiModelProperty:描述一个 model 的属性

  • 相关阅读:
    频频出现的DevOps到底是什么呢?浅浅了解下什么是DevOps吧
    从0开始学python(八)
    [NOIP2003 普及组] 乒乓球
    【电脑插入U盘或者内存卡显示无法格式化FAT32如何解决】
    css 动画基础知识和案例
    Cesium数据,后端权限验证(实现类似cesium ion的token权限验证)
    信息安全基础笔记
    JS奇淫技巧:一行赋值语句,能玩出多少花样?
    flask-cache使用报错Python3 ModuleNotFoundError: No module named ‘werkzeug.contrib‘
    Leecode第13题题解
  • 原文地址:https://blog.csdn.net/m0_73503454/article/details/132956689