知乎專欄 | 多維度架構 | | | 微信號 netkiller-ebook | | | QQ群:128659835 請註明“讀者” |
目錄
@JsonIgnore private String entityName = this.getClass().getSimpleName();
預設 json 中的時間格式是這樣的
"createDate":"2018-09-11T07:34:20.106+0000","updateDate":"2018-09-11T07:34:20.106+0000"
@JsonFormat 可以格式化 json 返回的時間格式。
@JsonFormat(pattern = "yyyy-MM-dd HH:mm:ss")
格式化後
"createDate":"2018-09-11 07:42:44","updateDate":"2018-09-11 07:42:44"
解決時區問題,MongoDb 預設使用UTC,顯示時間相差8小時
@JsonFormat(timezone = "GMT+8", pattern = "yyyy-MM-dd HH:mm:ss") private Date createdDate = new Date();
public class Test { @JsonFormat(shape=JsonFormat.Shape.STRING, pattern="yyyy-MMM-dd HH:mm:ss z", timezone="EST") @JsonProperty("pubDate") private Date recentBookPubDate; }
public class Test { @JsonFormat(shape=JsonFormat.Shape.NUMBER) @JsonProperty("birthDate") private Date birthDate; }
{ "birthDate" : 1528702883858 }
package cn.netkiller.json; public class Member { private String name; public Member() { // TODO Auto-generated constructor stub } public Member(String name) { // TODO Auto-generated constructor stub this.name = name; } public String getName() { return name; } public void setName(String name) { this.name = name; } @Override public String toString() { return "Member [name=" + name + "]"; } }
package cn.netkiller.json; import java.io.IOException; import org.springframework.boot.jackson.JsonComponent; import com.fasterxml.jackson.core.JsonGenerator; import com.fasterxml.jackson.core.JsonParser; import com.fasterxml.jackson.core.JsonProcessingException; import com.fasterxml.jackson.core.TreeNode; import com.fasterxml.jackson.databind.DeserializationContext; import com.fasterxml.jackson.databind.JsonDeserializer; import com.fasterxml.jackson.databind.JsonSerializer; import com.fasterxml.jackson.databind.SerializerProvider; import com.fasterxml.jackson.databind.node.TextNode; @JsonComponent public class Json { public Json() { // TODO Auto-generated constructor stub } public static class MemberJsonSerializer extends JsonSerializer<Member> { @Override public void serialize(Member value, JsonGenerator gen, SerializerProvider serializers) throws IOException { // TODO Auto-generated method stub gen.writeStartObject(); gen.writeStringField("member", value.toString()); gen.writeEndObject(); } } public static class MemberJsonDeserializer extends JsonDeserializer<Member> { @Override public Member deserialize(JsonParser p, DeserializationContext ctxt) throws IOException, JsonProcessingException { // TODO Auto-generated method stub TreeNode treeNode = p.getCodec().readTree(p); TextNode member = (TextNode) treeNode.get("member"); return new Member(member.asText()); } } }
package cn.netkiller.json.controller; import cn.netkiller.json.Member; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.web.bind.annotation.GetMapping; import org.springframework.web.bind.annotation.RestController; import com.fasterxml.jackson.core.JsonProcessingException; import com.fasterxml.jackson.databind.JsonMappingException; import com.fasterxml.jackson.databind.ObjectMapper; /** * * @author neo */ @RestController public class SimpleController { @Autowired public ObjectMapper objectMapper; @GetMapping("/") public String home() throws JsonMappingException, JsonProcessingException { String json = "{\"name\":\"netkiller\"}"; Member member = objectMapper.readValue(json, Member.class); System.out.println(member.getName()); return member.getName(); } }