@JsonAutoDetect (class)
這是作用於類的annotation,主要用於指明該類使用annotation,並且可以自動偵測getter,setter,構造方法,以便生成json物件
@JsonIgnore (method/field):作用於方法或欄位,用來表明,當生成json的時候忽略有該annotation的方法或欄位
如題,以一個使用者物件為例子:
-
@Entity
-
@Cache(usage = CacheConcurrencyStrategy.READ_WRITE)
-
@JsonAutoDetect
-
-
-
-
-
-
@JsonIgnoreProperties(value = {"hibernateLazyInitializer", "password"})
-
public class User
-
{
-
private Long id;
-
private String name;
-
private String password;
-
private String email;
-
private Date createAt;
-
@Id
-
@GeneratedValue(strategy = GenerationType.IDENTITY)
-
public Long getId() {
-
return id;
-
}
-
-
public void setId(Long id) {
-
this.id = id;
-
}
-
-
-
-
@JsonSerialize(using = CustomDateSerializer.class)
-
public Date getCreateAt() {
-
return createAt;
-
}
-
-
public void setCreateAt(Date createAt) {
-
this.createAt = createAt;
-
}
-
-
-
-
}
-
@Entity
-
@Cache(usage = CacheConcurrencyStrategy.READ_WRITE)
-
@JsonAutoDetect
-
-
-
-
-
-
@JsonIgnoreProperties(value = {"hibernateLazyInitializer", "password"})
-
public class User
-
{
-
private Long id;
-
private String name;
-
private String password;
-
private String email;
-
private Date createAt;
-
@Id
-
@GeneratedValue(strategy = GenerationType.IDENTITY)
-
public Long getId() {
-
return id;
-
}
-
-
public void setId(Long id) {
-
this.id = id;
-
}
-
-
-
-
@JsonSerialize(using = CustomDateSerializer.class)
-
public Date getCreateAt() {
-
return createAt;
-
}
-
-
public void setCreateAt(Date createAt) {
-
this.createAt = createAt;
-
}
-
-
-
-
}
至於中間的什麼service,dao都大同小異就不記錄了
轉到struts2 看看一個用jackson返回json物件的action是如何寫的
-
@Namespace("/security/user")
-
public class UserAction extends ActionSupport
-
{
-
@Action("list")
-
public String list() throws Exception {
-
-
List<User> list = userService.getAll();
-
response = ServletActionContext.getResponse();
-
-
ObjectMapper mapper = new ObjectMapper();
-
-
mapper.writeValue(response.getWriter(), list);
-
return null;
-
}
-
}
-
@Namespace("/security/user")
-
public class UserAction extends ActionSupport
-
{
-
@Action("list")
-
public String list() throws Exception {
-
-
List<User> list = userService.getAll();
-
response = ServletActionContext.getResponse();
-
-
ObjectMapper mapper = new ObjectMapper();
-
-
mapper.writeValue(response.getWriter(), list);
-
return null;
-
}
-
}
這樣我們在瀏覽器訪問http://yourdomain/security/user/list就可以返回一個包含所有使用者資訊的json陣列
hibernate延時載入
因為jsonplugin用的是java的內審機制.hibernate會給被管理的pojo加入一個hibernateLazyInitializer屬性,jsonplugin會把hibernateLazyInitializer也拿出來操作,並讀取裡面一個不能被反射操作的屬性就產生了這個異常.
不過我用的是jackson來轉json,所以想到了用annotation來排除hibernateLazyInitializer 這個屬性
在你的pojo類宣告加上:
-
@JsonIgnoreProperties(value={"hibernateLazyInitializer"})
轉換格式設定
近日,使用Jackson轉化JSON物件的時候,顯示的時候,日期始終顯示不正確,輸出的日期是一串數字代表的時間戳,不符合要求,所以想到Jackson應當有方法設定輸出的日期格式。後來一查果然有兩種方式來實現:
1.普通的方式:
預設是轉成timestamps形式的,通過下面方式可以取消timestamps。
-
objectMapper.configure(SerializationConfig.Feature.WRITE_DATES_AS_TIMESTAMPS, false);
-
objectMapper.configure(SerializationConfig.Feature.WRITE_DATES_AS_TIMESTAMPS, false);
這樣將使時間生成使用所謂的use a [ISO-8601 ]-compliant notation, 輸出類似如下格式的時間:
"1970-01-01T00:00:00.000+0000".
當然也可以自定義輸出格式:
-
objectMapper.getSerializationConfig().setDateFormat(myDateFormat);
-
objectMapper.getSerializationConfig().setDateFormat(myDateFormat);
-
myDateFormat物件為java.text.DateFormat,具體使用清查java API
-
myDateFormat物件為java.text.DateFormat,具體使用清查java API
2.annotaion的註釋方式:
先定義自己需要的格式,例如:
-
import java.io.IOException;
-
import java.text.SimpleDateFormat;
-
import java.util.Date;
-
import org.codehaus.jackson.JsonGenerator;
-
import org.codehaus.jackson.JsonProcessingException;
-
import org.codehaus.jackson.map.JsonSerializer;
-
import org.codehaus.jackson.map.SerializerProvider;
-
-
-
-
-
-
-
public class CustomDateSerializer extends JsonSerializer<Date> {
-
-
@Override
-
public void serialize(Date value, JsonGenerator jgen, SerializerProvider provider) throws IOException, JsonProcessingException {
-
SimpleDateFormat formatter = new SimpleDateFormat("yyyy-MM-dd");
-
String formattedDate = formatter.format(value);
-
jgen.writeString(formattedDate);
-
}
-
}
-
import java.io.IOException;
-
import java.text.SimpleDateFormat;
-
import java.util.Date;
-
import org.codehaus.jackson.JsonGenerator;
-
import org.codehaus.jackson.JsonProcessingException;
-
import org.codehaus.jackson.map.JsonSerializer;
-
import org.codehaus.jackson.map.SerializerProvider;
-
-
-
-
-
-
-
public class CustomDateSerializer extends JsonSerializer<Date> {
-
-
@Override
-
public void serialize(Date value, JsonGenerator jgen, SerializerProvider provider) throws IOException, JsonProcessingException {
-
SimpleDateFormat formatter = new SimpleDateFormat("yyyy-MM-dd");
-
String formattedDate = formatter.format(value);
-
jgen.writeString(formattedDate);
-
}
-
}
然後在你的POJO上找到日期的get方法
-
@JsonSerialize(using = CustomDateSerializer.class)
-
public Date getCreateAt() {
-
return createAt;
-
}