`

Neo4j: custom parse string to POJO with jackson json

 
阅读更多

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

 

 

 

 

 

 

 

 

 

 

 

 

 

 

  • 大小: 17.9 KB
分享到:
评论

相关推荐

    vue-neo4j:vue使用D3对neo4j进行可视化源码开发技术-其它

    Neo4j Vue 安装步骤 克隆或下载该库到本地 下载neo4j并安装到本地,启动neo4j服务 进入到该工程的根目录,输入命令:npm run update进行npm包的导入 使用命令:npm run dev启动该工程 默认端口是:8081 neo4j账号...

    vue-neo4j:vue使用D3对neo4 j进行可视化-源码开发技术-其它

    Neo4j Vue 安装步骤 克隆或下载该库到本地 下载neo4j并安装到本地,启动neo4j服务 进入到该工程的根目录,输入命令:npm run update进行npm包的导入 使用命令:npm run dev启动该工程 默认端口是:8081 neo4j账号...

    movies-java-spring-data-neo4j:使用Spring Data Neo4j的Neo4j电影示例

    电影示例应用 如何一起使用Spring Boot,Spring Data和Neo4j。 Spring Data Neo4j可以在基于Spring的应用程序中方便地集成Neo4j。 它提供了对象图映射(OGM)功能以及Spring Data项目共有的其他功能。 笔记该项目...

    sails-restful-neo4j:SailsWaterline RESTful Neo4j 适配器

    connections = { default : 'restful-neo4j' , restful - neo4j : { module : 'sails-restful-neo4j' , type : 'json' , // expected response type (json | string | http) host : 'foo.myneo4j.com' , //

    bitnami-docker-neo4j:Neo4j的Bitnami Docker映像

    DR $ docker run --name neo4j bitnami/neo4j:latestDocker撰写$ curl -sSL https://raw.githubusercontent.com/bitnami/bitnami-docker-neo4j/master/docker-compose.yml &gt; docker-compose.yml$ docker-compose up...

    neo4j:所有人的图表

    neo4j:所有人的图表

    email2neo4j:使用一个简单的命令将您的电子邮件导入图表

    imap2neo4j: imap2neo4j imapServer imapUsername imapPassword imapMailbox neo4jServer [neo4jUsername] [neo4jPassword] [paging, eg import by batches of 1000] [specific range of messages, eg 50

    Nested-Class-Models-Rest-Api-Neo4j:在休息环境中作为 neo4j 扩展实现的嵌套类模型

    通过在 conf/neo4j-server.properties 中添加一行来配置 Neo4j: org.neo4j.server.thirdparty_jaxrs_classes=org.neo4j.example.unmanagedextension=/example 启动 Neo4j 服务器。 写评论 curl ...

    Sublime-Neo4j:用于运行 Neo4j Cypher 查询的 Sublime 插件

    Sublime Text 2/3 插件:Neo4j Sublime 是最好的跨平台编辑器之一,所以我想为什么不创建一个简单的 Neo4j 插件呢?... 编辑用户名和密码(默认为neo4j:neo) 结果显示在控制台中(Ctrl+~)打开C

    egg-neo4j:Neo4j鸡蛋插件

    egg-neo4j Neo4j用于鸡蛋框架。安装$ npm i egg-neo4j --save用法// {app_root}/config/plugin.jsexports . neo4j = { enable : true , package : 'egg-neo4j' ,} ;配置// {app_root}/config/config.default....

    NBD_Tutorials_Neo4J:PJATK NDB教程的Neo4J分配

    NBD_Tutorials_Neo4J:PJATK NDB教程的Neo4J分配

    schemaless-graphql-neo4j:将无类型和动态GraphQL查询转换为Cypher

    schemaless-graphql-neo4j 将无类型的动态GraphQL查询转换为Cypher。 签出,以更好地查看您可以编写的查询。入门$ npm install schemaless-graphql-neo4j :warning: 图书馆尚未发布操场您可以开始使用开发人员游乐场...

    linkedin_to_neo4j:Linkedin_to_neo4j

    为什么这个 ? 您可能需要对您的linkedin 网络进行分析/监控。 我个人使用它来更好地可视化我的网络并对其进行一些数据分析。如何使用您必须通过在其中创建应用程序来设置您的 linkedin API 密钥 git clone ...

    vue-neo4j:vue使用D3对neo4j进行可视化

    Neo4j Vue安装步骤克隆或下载该库到本地下载neo4j并安装到本地,启动neo4j服务进入到该工程的根目录,输入命令:npm run update进行npm包的导入使用命令:npm run dev启动该工程默认端口是:8081 neo4j账号信息: ...

    Beginning.Neo4j.1484212

    This book will take you from the installation of Neo4j through to building a full application with Neo4j at its heart, and everything in between. Using this book, you'll get everything up and ...

    docker-neo4j:运行 Neo4j 容器的镜像

    Neo4j Neo4j 是一个高度可扩展、健壮(完全 ACID)的原生图形数据库。 Neo4j 被全球数以千计的领先企业、初创公司、企业和政府用于任务关键型应用程序。 使用存储库上的 Dockerfile,您就可以使用 docker neo4j ...

    node-neo4j:用于Node.js的Neo4j REST API包装器

    适用于Node.js的Neo4j REST API包装器 主分支: 开发分支: 安装 npm install node-neo4j --save 用法 为了使用该库,您必须创建一个应用并添加或在本地安装它。 如果您使用的是OS XI,则强烈建议通过安装Neo4j。 ...

    gorm-neo4j:Neo4j的GORM

    适用于Neo4j的GORM 该项目使用Bolt Java驱动程序为Neo4j 3.x图形数据库实现 。 有关更多信息,请参见以下链接: 对于当前的开发版本,请参见以下链接:

    neo4j:Neo4j的Hackolade插件

    Neo4j 用于将Neo4j图形数据库作为Hackolade数据建模目标的插件。 Hackolade通过插件体系结构公开了其核心数据建模引擎。 每个插件都将Hackolade数据建模功能应用于特定的目标技术,无论是静态数据(数据库)还是...

    stackoverflow-neo4j:在neo4j中导入stackoverflow

    在目录中您可以在Linux上使用dtrx 你需要sudo pip3 install xmltodict python3 to_csv.py extracted/以在csvs/获取csvs/ sh import.sh导入neo4j中的csvs 假设neo4j在../neo/目录中该脚本假定您要删除旧数据库(最后...

Global site tag (gtag.js) - Google Analytics