springboot学习笔记(一)

  1. Springboot官方文档地址:

https://docs.spring.io/spring-boot/docs/2.1.0.BUILD-SNAPSHOT/reference/htmlsingle/#common-application-properties

  1. Springboot对json格式的支持

常用框架 阿里fastjson,谷歌gson等

JavaBean序列化Json性能 Jackson > FastJson > Gson > Json-lib

Jackson注解

指定字段不返回 @JsonIgnore

指定日期格式

@JsonFormat(pattern="yyyy-MM-dd hh:mm:ss",locale="zh",timezone="GMT+8")

空字段不返回:@JsonInclude(Include.NON_NUll)

指定别名:@JsonProperty

  1. Springboot目录文件结构

src/main/java:存放代码

src/main/resources

static: 存放静态文件,比如 css、js、image, (访问方式 http://localhost:8080/js/main.js)

templates:存放静态页面jsp,html,tpl

config:存放配置文件,application.properties

resources:

引入依赖 Thymeleaf

org.springframework.boot

spring-boot-starter-thymeleaf

注意:如果不引人这个依赖包,html文件应该放在默认加载文件夹里面,

比如resources、static、public这个几个文件夹,才可以访问

Spring Boot 默认会挨个从

META/resources > resources > static > public  里面找是否存在相应的资源,如果有则直接返回。

  1. 配置注解

Controller上面配置

1、Controller上面配置

@PropertySource({"classpath:resource.properties"})

2、增加属性

@Value("${test.name}")

private String name;

实体类配置

1、添加 @Component 注解;

2、使用 @PropertySource 注解指定配置文件位置;

3、使用 @ConfigurationProperties 注解,设置相关属性;

注入bean的方式,属性名称和配置文件里面的key一一对应,就用加@Value 这个注解,如果不一样,就要加@value("${XXX}")

  1. 自定义异常

增加@ControllerAdvice注解,为全局异常类入口,如果想反悔json数据,则修改为@RestControllerAdvice就可以不加@ResponseBody

@ExceptionHandler(value = Exception.class)添加到方法注解上捕获全局异常, value表示捕获的异常类

自定义异常,需要引入thymeleaf依赖

org.springframework.boot

spring-boot-starter-thymeleaf

对应的教程地址:

https://docs.spring.io/spring-boot/docs/2.1.0.BUILD-SNAPSHOT/reference/htmlsingle/#boot-features-error-handling

  1. Springboot启动方式

Jar包启动,maven插件

 

org.springframework.boot

spring-boot-maven-plugin

目录结构讲解链接:

https://docs.spring.io/spring-boot/docs/2.1.0.BUILD-SNAPSHOT/reference/htmlsingle/#executable-jar-jar-file-structure

war包启动方式

在pom.xml中将打包形式 jar 修改为war  war

修改启动类

public class StartApplication extends SpringBootServletInitializer {

    @Override

    protected SpringApplicationBuilder configure(SpringApplicationBuilder application) {

        return application.sources(StartApplication.class);

    }

    public static void main(String[] args) throws Exception {

        SpringApplication.run(StartApplication.class, args);

    }

}

使用Jmter测试工具测试性能,QPS,TPS,RT

https://examples.javacodegeeks.com/enterprise-java/spring/tomcat-vs-jetty-vs-undertow-comparison-of-spring-boot-embedded-servlet-containers/

你可能感兴趣的:(自定义异常,配置注解,启动方式,springboot)