Java 使用Genson和Jersey解析帖子中的JSON日期
快速介绍(可以跳过): 大家好,关于这个话题有很多问题和答案,但有时候解决方案很简单,你甚至都不去想,因为我已经浪费了很多时间,所以我想发布一个所有答案的补充 问题: 您有一个JSON REST服务,该服务处理POST请求以保存JSON对象,但该对象包含一个日期字段,而Genson不会对该字段进行开箱解析 Java对象:Java 使用Genson和Jersey解析帖子中的JSON日期,java,rest,date,jersey,genson,Java,Rest,Date,Jersey,Genson,快速介绍(可以跳过): 大家好,关于这个话题有很多问题和答案,但有时候解决方案很简单,你甚至都不去想,因为我已经浪费了很多时间,所以我想发布一个所有答案的补充 问题: 您有一个JSON REST服务,该服务处理POST请求以保存JSON对象,但该对象包含一个日期字段,而Genson不会对该字段进行开箱解析 Java对象: public class MyObj { // The field you want to serialize/deserialize private Dat
public class MyObj {
// The field you want to serialize/deserialize
private Date date;
// Constructor with no arguments needed by Genson
public MyObj() {}
}
@Path("/api/my-obj")
public class MyObjAPI {
@POST
@Consumes("application/json")
public Response insert(MyObj myObj) {
// do what you want with myObj, it's ready to use with the date
return Response.created('url/to/created/object').build();
}
}
// This is the JSON Object to POST
var myObj = {
date: new Date()
};
$.ajax({
method: 'POST',
url: '/api/my-obj',
data: JSON.stringify(myObj),
dataType: 'json',
processData: false,
contentType: 'application/json'
});
球衣的剩余服务:
public class MyObj {
// The field you want to serialize/deserialize
private Date date;
// Constructor with no arguments needed by Genson
public MyObj() {}
}
@Path("/api/my-obj")
public class MyObjAPI {
@POST
@Consumes("application/json")
public Response insert(MyObj myObj) {
// do what you want with myObj, it's ready to use with the date
return Response.created('url/to/created/object').build();
}
}
// This is the JSON Object to POST
var myObj = {
date: new Date()
};
$.ajax({
method: 'POST',
url: '/api/my-obj',
data: JSON.stringify(myObj),
dataType: 'json',
processData: false,
contentType: 'application/json'
});
带有jQuery的javascript客户端:
public class MyObj {
// The field you want to serialize/deserialize
private Date date;
// Constructor with no arguments needed by Genson
public MyObj() {}
}
@Path("/api/my-obj")
public class MyObjAPI {
@POST
@Consumes("application/json")
public Response insert(MyObj myObj) {
// do what you want with myObj, it's ready to use with the date
return Response.created('url/to/created/object').build();
}
}
// This is the JSON Object to POST
var myObj = {
date: new Date()
};
$.ajax({
method: 'POST',
url: '/api/my-obj',
data: JSON.stringify(myObj),
dataType: 'json',
processData: false,
contentType: 'application/json'
});
解决方案:
public class MyObj {
// The field you want to serialize/deserialize
private Date date;
// Constructor with no arguments needed by Genson
public MyObj() {}
}
@Path("/api/my-obj")
public class MyObjAPI {
@POST
@Consumes("application/json")
public Response insert(MyObj myObj) {
// do what you want with myObj, it's ready to use with the date
return Response.created('url/to/created/object').build();
}
}
// This is the JSON Object to POST
var myObj = {
date: new Date()
};
$.ajax({
method: 'POST',
url: '/api/my-obj',
data: JSON.stringify(myObj),
dataType: 'json',
processData: false,
contentType: 'application/json'
});
Genson提供了一种定义日期如何(反)序列化的简单方法,这是您在任何地方都可以找到的解决方案:
@Provider
public class GensonProvider implements ContextResolver<Genson> {
private final Genson genson = new GensonBuilder()
.useDateAsTimestamp(false)
.useDateFormat(new SimpleDateFormat("yyyy-MM-dd'T'HH:mm:ss.SSS'Z'"))
.create();
@Override
public Genson getContext(Class<?> type) {
return genson;
}
}
而不是:
import java.sql.Date;
我自己解决了这个问题,但在StackOverflow的任何地方都找不到,这是一个愚蠢的错误,太简单了,你想不起来(像我一样),我希望它能帮助别人。顺便说一句,Genson也使用带参数的构造函数。只需通过GensonBuilder.useConstructorWithArguments启用它(true)