In my project, I provide neo4j extentions to clients which send json string to extention while jackson auto parse the json string to my POJO Model. Howerver, I want to simply the json string sent by client .
The POJO Model class likes
@NodeEntity @XmlRootElement @JsonAutoDetect @JsonIgnoreProperties(ignoreUnknown = true) public class Task { @GraphId private Long nodeId; @Indexed(unique = true) private String taskId; @Indexed(indexType = IndexType.FULLTEXT, indexName = "TaskTile") private String title; @Indexed(indexType = IndexType.FULLTEXT, indexName = "TaskDescription") private String description; // Date startTime; // Date endTime; @Indexed private int startDate; @Indexed private int startTime; @Indexed private int endDate; @Indexed private int endTime; ---
The startDate, startTime,endDate,endTime adopted to search. I dont' want client to explict send these fields in json strings but send by two Date string likes yyyy-MM-dd HH:mm:ss or Unix time 172399299999.
The valueble hints are
http://wiki.fasterxml.com/JacksonHowToCustomDeserializers
http://magicmonster.com/kb/prg/java/spring/webmvc/jackson_custom.html
http://www.baeldung.com/jackson-deserialization
I did it by some post advices. But When I send some post, Neo4j Jersey actually doesn't use jackson to parse the string.
I tried everything I could. Finally, I tried it out.
In pom.xml, We add
<dependency> <groupId>com.fasterxml.jackson.jaxrs</groupId> <artifactId>jackson-jaxrs-json-provider</artifactId> <version>2.2.1</version> </dependency>
In Model , We delete
@XmlRootElement
and Change Json related annotations to
com.fasterxml.jackson.databind.annotation.*
not
org.codehaus.jackson.annotate.*
And The final Modle class is
import org.springframework.data.neo4j.annotation.GraphId; import org.springframework.data.neo4j.annotation.Indexed; import org.springframework.data.neo4j.annotation.Labels; import org.springframework.data.neo4j.annotation.NodeEntity; import org.springframework.data.neo4j.support.index.IndexType; import com.fadeinfadeout.common.TaskDeserializer; import com.fadeinfadeout.common.TaskPayOption; import com.fasterxml.jackson.databind.annotation.JsonDeserialize; @NodeEntity @JsonDeserialize(using = TaskDeserializer.class ) public class Task { @GraphId private Long nodeId; @Indexed(unique = true) private String taskId;
The TaskDeserializer class is
package com.fadeinfadeout.common; import java.io.IOException; import java.text.DateFormat; import java.text.SimpleDateFormat; import java.util.Date; import java.util.logging.Logger; import com.fadeinfadeout.modle.Task; import com.fasterxml.jackson.core.JsonParser; import com.fasterxml.jackson.core.JsonProcessingException; import com.fasterxml.jackson.databind.DeserializationContext; import com.fasterxml.jackson.databind.JsonDeserializer; import com.fasterxml.jackson.databind.JsonNode; import com.fasterxml.jackson.databind.node.TextNode; public class TaskDeserializer extends JsonDeserializer<Task> { private static DateFormat dateFormator = new SimpleDateFormat("yyyyMMdd"); private static DateFormat timeFormator = new SimpleDateFormat("HHmmss"); private static final Logger logger = Logger.getLogger(TaskDeserializer.class.getName()); @Override public Task deserialize(JsonParser jp, DeserializationContext dc) throws IOException, JsonProcessingException { JsonNode node = jp.getCodec().readTree(jp); String taskId = ((TextNode) node.get("taskId")).textValue(); String title = ((TextNode) node.get("title")).textValue(); String description = ((TextNode) node.get("description")).textValue(); String startTimeStr = ((TextNode) node.get("start")).textValue(); // unix time String endTimeStr = ((TextNode) node.get("end")).textValue(); if (startTimeStr != null & endTimeStr != null) { Date sdate = new Date(Long.valueOf(startTimeStr)); Date edate = new Date(Long.valueOf(endTimeStr)); int startDate = Integer.valueOf(dateFormator.format(sdate)); int startTime = Integer.valueOf(timeFormator.format(sdate)); int endDate = Integer.valueOf(dateFormator.format(edate)); int endTime = Integer.valueOf(timeFormator.format(edate)); return new Task(taskId, title, description, startDate, startTime, endDate, endTime); } return new Task(taskId, title, description); } }
When post a request with
curl -i -H 'content-type: application/json' -X POST -d '{"taskId":"3","title":"打炮","description":"约吗", "start":"1427965098585","end":"1427965598585"}' http://localhost:7474/fifo/task/addTask
The deserilizer result is correct
Due to the following posts, I could solve this problem.
http://www.4byte.cn/question/601051/unmanaged-extensions-taking-json-as-parameter.html
https://github.com/FasterXML/jackson-annotations/wiki/Jackson-Annotations
------------------------------------
The other coin of the above problem is custom serialize POJO Model to Json string
http://java.dzone.com/articles/how-serialize-javautildate
http://wiki.fasterxml.com/JacksonSampleDateHandling
References
https://jersey.java.net/nonav/documentation/latest/media.html
http://wiki.fasterxml.com/JacksonHowToCustomSerializers
http://magicmonster.com/kb/prg/java/spring/webmvc/jackson_custom.html
http://wiki.fasterxml.com/JacksonHome
http://wiki.fasterxml.com/JacksonSampleSimplePojoMapper