dropwizard常用属性注解
dropwizard框架默认使用的fastxml的jackson注解
一、两种情况下都有效的注解
1. @JsonIgnore
@JsonIgnore 用来告诉 Jackson 在处理时忽略该注解标注的 java pojo 属性,不管是将 java 对象转换成 json 字符串,还是将 json 字符串转换成 java 对象。下面是一个具体的例子
首先定义一个java pojo类
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
|
package org.lifw.jackosn.annotation;
import com.fasterxml.jackson.annotation.JsonIgnore;
public class SomeEntity {
private String name;
@JsonIgnore
private String desc;
public String getName() {
return name;
}
public void setName(String name) {
this .name = name;
}
public String getDesc() {
return desc;
}
public void setDesc(String desc) {
this .desc = desc;
}
@Override
public String toString() {
return "SomeEntity [name=" + name + ", desc=" + desc + "]" ;
}
}
|
测试代码
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
|
package org.lifw.jackosn.annotation;
import java.io.IOException;
import org.junit.Test;
import com.fasterxml.jackson.databind.ObjectMapper;
public class AnnotationTest {
private String jsonString =
"{\"name\":\"aaa\",\"desc\":\"aaa is a good person\"}" ;
@Test
public void testJonsIgnore() throws IOException {
//java对象 -> json字符串
SomeEntity entity = new SomeEntity();
entity.setName( "aaa" );
entity.setDesc( "aaa is a good person" );
System.out.println( new ObjectMapper().writeValueAsString(entity));
//json字符串 -> java对象
SomeEntity entity2 = new ObjectMapper().readValue(jsonString, SomeEntity. class );
System.out.println(entity2);
}
}
|
输出结果
1
2
|
{ "name" : "aaa" }
SomeEntity [name=aaa, desc= null ]
|
从上面的例子可以看出序列化和反序列化的过程中 desc 属性都被忽略了,这个注解非常常用。
2. @JsonIgnoreProperties
@JsonIgnoreProperties 和 @JsonIgnore 的作用相同,都是告诉 Jackson 该忽略哪些属性,不同之处是 @JsonIgnoreProperties 是类级别的,并且可以同时指定多个属性。这里采用和上面同样的例子,修改java pojo 如下
1
2
3
4
5
6
7
8
9
10
|
package org.lifw.jackosn.annotation;
com.fasterxml.jackson.annotation.JsonIgnoreProperties;
@JsonIgnoreProperties (value={ "desc" })
public class SomeEntity {
private String name;
@JsonIgnore
private String desc;
//getter,setter 等省略
}
|
运行测试代码,结果和上面的一样。
3. @JsonIgnoreType
@JsonIgnoreType 标注在类上,当其他类有该类作为属性时,该属性将被忽略。
1
2
3
4
5
6
7
8
9
10
11
12
|
package org.lifw.jackosn.annotation;
import com.fasterxml.jackson.annotation.JsonIgnoreType;
@JsonIgnoreType
public class SomeOtherEntity {
private Long id;
public Long getId() {
return id;
}
public void setId(Long id) {
this .id = id;
}
}
|
1
2
3
4
5
|
public class SomeEntity {
private String name;
private String desc;
private SomeOtherEntity entity;
}
|
SomeEntity 中的 entity 属性在json处理时会被忽略。
4. @JsonProperty
@JsonProperty 可以指定某个属性和json映射的名称。例如我们有个json字符串为{"user_name":"aaa"},而java中命名要遵循驼峰规则,则为userName,这时通过@JsonProperty 注解来指定两者的映射规则即可。这个注解也比较常用。
1
2
3
4
5
6
7
|
import com.fasterxml.jackson.annotation.JsonProperty;
public class SomeEntity {
@JsonProperty ( "user_name" )
private String userName;
// ...
}
|
二、只在序列化情况下生效的注解
1. @JsonPropertyOrder
在将 java pojo 对象序列化成为 json 字符串时,使用 @JsonPropertyOrder 可以指定属性在 json 字符串中的顺序。
1
2
3
4
5
6
7
8
|
import com.fasterxml.jackson.annotation.JsonPropertyOrder;
@JsonPropertyOrder (value={ "desc" , "name" })
public class SomeEntity {
private String name;
private String desc;
//..
}
|
2. @JsonInclude
在将 java pojo 对象序列化成为 json 字符串时,使用 @JsonInclude 注解可以控制在哪些情况下才将被注解的属性转换成 json,例如只有属性不为 null 时。
1
2
3
4
5
6
7
|
import com.fasterxml.jackson.annotation.JsonInclude;
@JsonInclude (JsonInclude.Include.NON_NULL)
public class SomeEntity {
private String name;
private String desc;
}
|
与之对应的老版本使用:
@JsonSerialize(include = JsonSerialize.Inclusion.ALWAYS)
上述例子的意思是 SomeEntity 的所有属性只有在不为 null 的时候才被转换成 json,如果为 null 就被忽略。该注解也可以加在某个字段上。另外还有很多其它的范围,例如 NON_EMPTY、NON_DEFAULT等,这些在不同版本的 jackson 中含义不同,具体请参考 jackson api,比较混乱。
三、是在反序列化情况下生效的注解
1. @JsonSetter
@JsonSetter 标注于 setter 方法上,类似 @JsonProperty ,也可以解决 json 键名称和 java pojo 字段名称不匹配的问题。
1
2
3
4
5
6
7
8
9
|
import com.fasterxml.jackson.annotation.JsonSetter;
public class SomeEntity {
private String desc;
@JsonSetter ( "description" )
public void setDesc(String desc) {
this .desc = desc;
}
}
|
上述例子中在将 json 字符串转换成 SomeEntity 实例时,会将 json 字符串中的 description 字段赋值给 SomeEntity 的 desc 属性。
dropwizard常用属性注解相关推荐
- [JAVA EE] Thymeleaf 高级用法:模板布局,带参数的引用片段,表单验证,常用校验注解
模板布局 公共部分通常定义为模板布局:如页眉,页脚,公共导航栏.菜单等. 模板布局定义方法 布局页中用 th:fragment 定义模板片段,其他页面用 th:insert 引用片段 例如:foote ...
- Springmvc的handler method参数绑定常用的注解
转自:http://blog.longjiazuo.com/archives/1149 1. 简介: handler method参数绑定常用的注解,我们根据他们处理的Request的不同内容部分分为 ...
- spring ,springmvc的常用标签注解
一:spring常用的注解: @Configuration把一个类作为一个IoC容器,它的某个方法头上如果注册了@Bean,就会作为这个Spring容器中的Bean. @Scope注解 作用域 @La ...
- java 注解object_JPA之常用 基本注解
1.常用基本注解 @Entity @Table @Basic @Column @GeneratedValue @Id 2.特殊注解 @Transient @Temporal 用 table 来生成主键 ...
- JPA+Hibernate中常用的注解
JPA+Hibernate中常用的注解 JPA是Java Persistence API的简称,中文名Java持久层API,是JDK 5.0注解或XML描述对象-关系表的映射关系,并将运行期的实体[对 ...
- 第二章 @Entity实例里常用的注解详解
一.javax.persistence概况介绍 虽然Spring Data JPA已经帮我们对数据的操作封装得很好了, 约定大于配置思想,帮我们默认了很多东西. JPA(Java持久性API) 是存储 ...
- 常用校验注解@NotEmpty,@NotBlank,@NotNull,@Valid,@Validated用法区别,以及搭配 BindingResult使用,嵌套验证等《使用|CSDN创作打卡》
文章目录 一.常用注解@NotEmpty,@NotBlank,@NotNull介绍 二.@Valid与@Validated的使用与区别 2.1@Valid与@Validated区别: 2.2嵌套验证 ...
- springMvc常用的注解有哪些?
springMvc常用的注解有哪些? 1.@Controller @Controller 用于标记在一个类上,使用它标记的类就是一个SpringMVC Controller 对象.分发处理器将会扫描使 ...
- selenium提取数据之driver对象的常用属性和方法
selenium提取数据之driver对象的常用属性和方法 在使用selenium过程中,实例化driver对象后,driver对象有一些常用的属性和方法 driver.page_source 当前标 ...
最新文章
- int、bigint、smallint 和 tinyint
- leetcode 58. Length of Last Word 题解【C++/Java/Python/JS】
- “内卷化”的快手与抖音——2020年短视频的“无聊经济”往何处去
- 精确控制div的高度
- Linux运维工程师的十个基本技能点
- 36.rustc编译参数.txt
- 企业管理软件开发不能割裂各系统的功能
- mysql 举例_MySQL 语句举例(一)
- html 图片上放置按钮,用CSS在图片上再加一个小按钮
- 130 行代码模仿火爆抖音的“蚂蚁呀嘿”特效,你学会了吗?
- Codeforces Beta Round #7 C. Line 扩展欧几里德
- python读取dat文件写入表格_Pandas:外部文件数据导入/ 读取 (如 :csv、txt、tsv、dat、excel文件)、文件存储(to_csv、to_excel)...
- php链接mysql数据库
- selenium python grid
- C#winform两个或多个panel重叠布局时如何显示一个
- 字体和font-family对照表
- STM32HAL库IIC驱动SH1108芯片OLED屏
- windows server 2008共享文件夹
- vue路由报错Avoided redundant navigation
- 树莓派-内核启动报错mmc0: error -5 whilst initialising MMC card
热门文章
- 视频帧差法实例(matlab实现)
- linux 设置.cshrc 使cd后直接ll显示文件和目录
- 毕业设计 基于java web的户籍管理系统的设计与实现
- 要求微信支付的勒索病毒作者被捕;网络游戏道德委员会近期成立
- ComponentOne For WPF学习心得-实时数据
- 笔记十八:帮助别人可能会发生的问题
- mysql goldengate_Goldengate完成Mysql到Mysql的数据同步
- python django + uwsgi+Nginx宝塔centos7的部署方案
- GitHub镜像站下载文件或克隆仓库
- python:pygame的各种使用方法