Spring Boot几个重要的注解

SpringBoot注解(annotations)
1.@SpringBootApplication = @SpringBootConfiguration + @EnableAutoConfiguration + @ComponentScan,其中@SpringBootConfiguration等同于Spring中的XML文件,使用java代码可以检查类型的安全性;@EnableAutoConfiguration 开启自动配置;@ComponentScan 可以让Spring Boot扫描到Configuration类,可以自动装配一些Bean,@Component可配合CommandLineRunner使用,在程序启动后执行一些基础任务。
2.@RestController、@RequestMapping是springMVC的注解,不是springboot特有的。
@RestController = @Controller + @ResponseBody,返回json等数据格式,不能返回jsp、html页面,视图解释器无法解析jsp、html页面。若想使试图解释器可以解析return的jsp、html页面,则应该使用@Controller注解。
3.@ResponseBody:表示该方法的返回结果直接写入HTTP response body中,一般在异步获取数据时使用。
4.@RequestMapping提供路由信息,返回值通常解析为跳转路径,负责URL到Controller中的具体函数的映射,任何“/”路径的http请求都被映射到home方法。但是加上@responsebody后返回结果不会被解析为跳转路径,而是直接写入HTTP response body中。
5.@Autowired自动导入依赖的bean。
6.@PathVariable获取参数。
7.@JsonBackReference解决嵌套外链问题。
8.@RepositoryRestResourcepublic配合spring-boot-starter-data-rest使用。
9.@Bean:用@Bean标注方法等价于XML中配置的bean。
10.@Import:用来导入其他配置类。
11.@ImportResource:用来加载xml配置文件。
12.@JsonIgnore,指定字段不返回,将ResponseBody中的Javabean返回前端过程中,忽略这个属性,对序列化也有影响,在一些业务场景中,如为了数据安全密码属性是不能显示出来的。对pwd属性加上注解@JsonIgnore,如下所示,则在前端界面中是不会显示出pwd的值。
public class User {

	private int age;
	
	@JsonIgnore
	private String pwd;

	@JsonProperty("account")
	@JsonInclude(Include.NON_NULL)
        private String phone;
	
	@JsonFormat(pattern="yyyy-MM-dd hh:mm:ss",locale="zh",timezone="GMT+8")
        private Date createTime;
	
	public Date getCreateTime() {
		return createTime;
	}

	public void setCreateTime(Date createTime) {
		this.createTime = createTime;
	}

	public int getAge() {
		return age;
	}

	public void setAge(int age) {
		this.age = age;
	}

	public String getPwd() {
		return pwd;
	}

	public void setPwd(String pwd) {
		this.pwd = pwd;
	}

	public String getPhone() {
		return phone;
	}

	public void setPhone(String phone) {
		this.phone = phone;
	}

	public User() {
		super();
	}

	public User(int age, String pwd, String phone, Date createTime) {
		super();
		this.age = age;
		this.pwd = pwd;		
		this.createTime = createTime;
	}	
	
}
13.@JsonProperty("account"),指定别名,将属性的名称序列化为另外一个名称,如将phone的属性序列化为account,则将注解@JsonProperty("account")添加到该属性的前面,这个注解的主要目的是为了防止别人根据暴露出的key值推断出数据库表结构,暴露出的信息越少越好。
14.@JsonInclude(Include.NON_NULL),空字段不放回,即实体类与json转换时属性值为null的不参与序列化。主要是防止前端显示出null值,将@JsonInclude(Include.NON_NULL) 注解添加到该属性前。
15.@JsonFormat(pattern="yyyy-MM-dd hh:mm:ss",locale="zh",timezone="GMT+8"),时间注解,将Jackson 时间格式化。

猜你喜欢

转载自blog.csdn.net/qq_38332574/article/details/84233280