Neo4j: custom parse string to POJO with jackson json

本文介绍如何简化Neo4j客户端发送的JSON数据,并通过自定义序列化解析器来简化数据处理流程。通过调整POJO模型和引入Jackson JAXRS JSON提供者,实现客户端只需发送日期字符串或Unix时间戳,而无需明确包含所有日期字段。同时,展示如何创建一个自定义序列化解析器来将这些日期字符串转换为实际日期对象。最后,验证了此方法的有效性并通过示例代码进行了演示。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

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

 

 

 

 

 

 

 

 

 

 

 

 

 

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值