常用注解
常用注解
1、@RestController和@RequestMapping是Spring MVC的注解,不是Spring Boot特有的。
2、@RestController = @Controller + @ResponseBody
3、@SpringBootApplication = @SpringBootConfiguration + @EnableAutoConfiguration + @ComponentScan
@RequestMapping
@GetMapping = @RequestMapping(method = RequestMethod.GET)
@PostMapping = @RequestMapping(method = RequestMethod.POST)
@PutMapping = @RequestMapping(method = RequestMethod.PUT)
@DeleteMapping = @RequestMapping(method = RequestMethod.DELETE)
@RequestParam(value = "name", required = true)可以设置默认值,比如分页
@RequestBody 请求体映射实体类需要指定http头为 content-type为application/json charset=utf-8
@RequestHeader 请求头,比如鉴权
@RequestHeader("access_token") String accessToken
@JsonXXX注解
指定字段不返回:@JsonIgnore
指定日期格式:@JsonFormat(pattern=“yyyy-MM-dd hh:mm:ss”,locale=“zh”,timezone=“GMT+8”)
空字段不返回:@JsonInclude(Include.NON_NUll)
指定别名:@JsonProperty
public class ColleagueDTO extends BaseDTO{
private String username;
@JsonIgnore
private String password;
private String nickName;
private Integer sex;
@JsonFormat(pattern="yyyy-MM-dd HH:mm:ss",locale = "zh",timezone="GMT+8")
private Date registerDate;
@JsonProperty("保密字段")
private BigDecimal salary;
@JsonFormat(pattern="yyyy-MM-dd HH:mm:ss",locale = "zh",timezone="GMT+8")
private Date birthDate;
private Integer age;
}
目录结构
src/main/java:存放代码
src/main/resources
static: 存放静态文件,比如 css、js、image, (访问方式 http://localhost:8080/js/main.js)
templates:存放静态页面jsp,html,tpl
config:存放配置文件,application.properties
同个文件的加载顺序,静态资源文件Spring Boot 默认会挨个从META/resources > resources > static > public 里面找是否存在相应的资源,如果有则直接返回。
spring.resources.static-locations =
classpath:/META-INF/resources/,
classpath:/resources/,
classpath:/static/,
classpath:/public/
如果要访问静态html网页,需要添加依赖:
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-thymeleaf</artifactId>
</dependency>
如果要更改静态资源文件的路径,可以在application.properties配置。
spring.resources.static-locations=classpath:/META-INF/resources/,classpath:/resources/,classpath:/static/,classpath:/public/,classpath:/自定义文件夹/```