`
zzc1684
  • 浏览: 1192508 次
  • 性别: Icon_minigender_1
  • 来自: 广州
文章分类
社区版块
存档分类
最新评论

初步了解jackson,简单的使用jackson的annotation

阅读更多

@JsonAutoDetect (class)

这是作用于类的annotation,主要用于指明该类使用annotation,并且可以自动侦测getter,setter,构造方法,以便生成json对象

@JsonIgnore (method/field):作用于方法或字段,用来表明,当生成json的时候忽略有该annotation的方法或字段

 

如题,以一个用户对象为例子:

 

Java代码 复制代码
  1. @Entity  
  2. @Cache(usage = CacheConcurrencyStrategy.READ_WRITE)   
  3. @JsonAutoDetect  
  4. /**  
  5. * 在此标记不生成json对象的属性,这里我标记了两个属性一个hibernateLazyInitializer属性,为什么要标记这个  
  6. * 属性参考前面的博文,一个password属性,出于安全这个当然不能转换成json对象了,毕竟json是在前台调用的,  
  7. * 如果你想转换的时候忽略某个属性,可以在后面继续加上  
  8. */  
  9. @JsonIgnoreProperties(value = {"hibernateLazyInitializer""password"})   
  10. public class User   
  11. {   
  12.     private Long id;   
  13.     private String name;   
  14.     private String password;   
  15.     private String email;   
  16.     private Date createAt;   
  17.     @Id  
  18.     @GeneratedValue(strategy = GenerationType.IDENTITY)   
  19.     public Long getId() {   
  20.         return id;   
  21.     }   
  22.   
  23.     public void setId(Long id) {   
  24.         this.id = id;   
  25.     }   
  26.     /**  
  27.     * 转换日期对象的输出格式,CustomDateSerializer 代码参考前面的博文      
  28.         */  
  29.     @JsonSerialize(using = CustomDateSerializer.class)   
  30.     public Date getCreateAt() {   
  31.             return createAt;   
  32.     }   
  33.   
  34.     public void setCreateAt(Date createAt) {   
  35.             this.createAt = createAt;   
  36.     }   
  37.     /**  
  38.     * 其他的getter和setter省略  
  39.     */  
  40. }  
Java代码  收藏代码
  1. @Entity  
  2. @Cache(usage = CacheConcurrencyStrategy.READ_WRITE)  
  3. @JsonAutoDetect  
  4. /** 
  5. * 在此标记不生成json对象的属性,这里我标记了两个属性一个hibernateLazyInitializer属性,为什么要标记这个 
  6. * 属性参考前面的博文,一个password属性,出于安全这个当然不能转换成json对象了,毕竟json是在前台调用的, 
  7. * 如果你想转换的时候忽略某个属性,可以在后面继续加上 
  8. */  
  9. @JsonIgnoreProperties(value = {"hibernateLazyInitializer""password"})  
  10. public class User  
  11. {  
  12.     private Long id;  
  13.     private String name;  
  14.     private String password;  
  15.     private String email;  
  16.     private Date createAt;  
  17.     @Id  
  18.     @GeneratedValue(strategy = GenerationType.IDENTITY)  
  19.     public Long getId() {  
  20.         return id;  
  21.     }  
  22.   
  23.     public void setId(Long id) {  
  24.         this.id = id;  
  25.     }  
  26.     /** 
  27.     * 转换日期对象的输出格式,CustomDateSerializer 代码参考前面的博文     
  28.         */  
  29.     @JsonSerialize(using = CustomDateSerializer.class)  
  30.     public Date getCreateAt() {  
  31.             return createAt;  
  32.     }  
  33.   
  34.     public void setCreateAt(Date createAt) {  
  35.             this.createAt = createAt;  
  36.     }  
  37.     /** 
  38.     * 其他的getter和setter省略 
  39.     */  
  40. }  

 

至于中间的什么service,dao都大同小异就不记录了

转到struts2 看看一个用jackson返回json对象的action是如何写的

Java代码 复制代码
  1. @Namespace("/security/user")   
  2. public class UserAction extends ActionSupport   
  3. {   
  4.     @Action("list")   
  5.     public String list() throws Exception {   
  6.             // 取得所有的用户   
  7.             List<User> list = userService.getAll();   
  8.             response = ServletActionContext.getResponse();   
  9.             // jackson   
  10.             ObjectMapper mapper = new ObjectMapper();   
  11.             // 把取得的用户list写入response   
  12.             mapper.writeValue(response.getWriter(), list);   
  13.             return null;   
  14.     }   
  15. }  
Java代码  收藏代码
  1. @Namespace("/security/user")  
  2. public class UserAction extends ActionSupport  
  3. {  
  4.     @Action("list")  
  5.     public String list() throws Exception {  
  6.             // 取得所有的用户  
  7.             List<User> list = userService.getAll();  
  8.             response = ServletActionContext.getResponse();  
  9.             // jackson  
  10.             ObjectMapper mapper = new ObjectMapper();  
  11.             // 把取得的用户list写入response  
  12.             mapper.writeValue(response.getWriter(), list);  
  13.             return null;  
  14.     }  
  15. }  

 这样我们在浏览器访问http://yourdomain/security/user/list就可以返回一个包含所有用户信息的json数组

 

hibernate延时加载

因为jsonplugin用的是java的内审机制.hibernate会给被管理的pojo加入一个 hibernateLazyInitializer属性,jsonplugin会把hibernateLazyInitializer也拿出来操作,并读 取里面一个不能被反射操作的属性就产生了这个异常.  

 

不过我用的是jackson来转json,所以想到了用annotation来排除hibernateLazyInitializer 这个属性

在你的pojo类声明加上:

 

Java代码 复制代码
  1. @JsonIgnoreProperties(value={"hibernateLazyInitializer"})  

 

转换格式设置

近日,使用Jackson转化JSON对象的时候,显示的时候,日期始终显示不正确,输出的日期是一串数字代表的时间戳,不符合要求,所以想到Jackson应当有方法设置输出的日期格式。后来一查果然有两种方式来实现:

1.普通的方式:

默认是转成timestamps形式的,通过下面方式可以取消timestamps。

Java代码 复制代码
  1. objectMapper.configure(SerializationConfig.Feature.WRITE_DATES_AS_TIMESTAMPS, false);  
Java代码  收藏代码
  1. objectMapper.configure(SerializationConfig.Feature.WRITE_DATES_AS_TIMESTAMPS, false);  

 这样将使时间生成使用所谓的use a [ISO-8601 ]-compliant notation, 输出类似如下格式的时间: "1970-01-01T00:00:00.000+0000".

当然也可以自定义输出格式:

Java代码 复制代码
  1. objectMapper.getSerializationConfig().setDateFormat(myDateFormat);  
Java代码  收藏代码
  1. objectMapper.getSerializationConfig().setDateFormat(myDateFormat);  
Java代码 复制代码
  1. myDateFormat对象为java.text.DateFormat,具体使用清查java API  
Java代码  收藏代码
  1. myDateFormat对象为java.text.DateFormat,具体使用清查java API  

2.annotaion的注释方式:

先定义自己需要的格式,例如:

Java代码 复制代码
  1. import java.io.IOException;   
  2. import java.text.SimpleDateFormat;   
  3. import java.util.Date;   
  4. import org.codehaus.jackson.JsonGenerator;   
  5. import org.codehaus.jackson.JsonProcessingException;   
  6. import org.codehaus.jackson.map.JsonSerializer;   
  7. import org.codehaus.jackson.map.SerializerProvider;   
  8.   
  9. /**  
  10.  *  java日期对象经过Jackson库转换成JSON日期格式化自定义类  
  11.  * @author godfox  
  12.  * @date 2010-5-3  
  13.  */  
  14. public class CustomDateSerializer extends JsonSerializer<Date> {   
  15.   
  16.         @Override  
  17.         public void serialize(Date value, JsonGenerator jgen, SerializerProvider provider) 
  18. throws IOException, JsonProcessingException {   
  19.                 SimpleDateFormat formatter = new SimpleDateFormat("yyyy-MM-dd");   
  20.                 String formattedDate = formatter.format(value);   
  21.                 jgen.writeString(formattedDate);   
  22.         }   
  23. }  
Java代码  收藏代码
  1. import java.io.IOException;  
  2. import java.text.SimpleDateFormat;  
  3. import java.util.Date;  
  4. import org.codehaus.jackson.JsonGenerator;  
  5. import org.codehaus.jackson.JsonProcessingException;  
  6. import org.codehaus.jackson.map.JsonSerializer;  
  7. import org.codehaus.jackson.map.SerializerProvider;  
  8.   
  9. /** 
  10.  *  java日期对象经过Jackson库转换成JSON日期格式化自定义类 
  11.  * @author godfox 
  12.  * @date 2010-5-3 
  13.  */  
  14. public class CustomDateSerializer extends JsonSerializer<Date> {  
  15.   
  16.         @Override  
  17.         public void serialize(Date value, JsonGenerator jgen, SerializerProvider provider) 
  18. throws IOException, JsonProcessingException {  
  19.                 SimpleDateFormat formatter = new SimpleDateFormat("yyyy-MM-dd");  
  20.                 String formattedDate = formatter.format(value);  
  21.                 jgen.writeString(formattedDate);  
  22.         }  
  23. }  

 然后在你的POJO上找到日期的get方法

Java代码 复制代码
  1. @JsonSerialize(using = CustomDateSerializer.class)   
  2.        public Date getCreateAt() {   
  3.                return createAt;   
  4.        }  
 

 

更多可以参考:http://wiki.fasterxml.com/JacksonAnnotations

分享到:
评论

相关推荐

Global site tag (gtag.js) - Google Analytics