Java 使用 Spring MVC 和 Jackson 将日期作为 JSON 传递
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/21505997/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Passing a date as JSON with Spring MVC and Hymanson
提问by Scott Woodward
I have a class with a java.util.Date field that I wish to pass from a client to a Spring controller. The controller returns HTTP 415 whenever I make the request. I have tried adding a custom serializer as seen in many other questions I've been able to find. The custom serializer works, in that my controller which retrieves resource retrieves them in the custom format but the controller will not acknowledge the JSON. If I remove the date entirely, the controller works so I know the issue is with that field.
我有一个带有 java.util.Date 字段的类,我希望将其从客户端传递给 Spring 控制器。每当我发出请求时,控制器都会返回 HTTP 415。我已经尝试添加自定义序列化程序,如我能找到的许多其他问题所示。自定义序列化程序工作,因为我的控制器检索资源以自定义格式检索它们,但控制器不会确认 JSON。如果我完全删除日期,控制器就会工作,所以我知道问题出在该字段上。
Ideally, I want to receive them in the default long representation, but I can't get the controller to accept either format.
理想情况下,我希望以默认的长表示形式接收它们,但我无法让控制器接受任何一种格式。
Controller
控制器
@RequestMapping(method = RequestMethod.POST)
@ResponseBody
public ResponseEntity<String> addEvent(ModelMap model, @RequestBody Event event)
{
eventService.saveEvent(event);
return new ResponseEntity<String>(HttpStatus.CREATED);
}
The class to be serialized (getters and setter omitted, though I also tried the annotation there.
要序列化的类(省略了 getter 和 setter,尽管我也在那里尝试了注释。
public class Event implements Serializable
{
private static final long serialVersionUID = -7231993649826586076L;
private int eventID;
private int eventTypeID;
@JsonSerialize(using = DateSerializer.class)
private Date date;
Serializer
序列化器
private static final SimpleDateFormat dateFormat = new SimpleDateFormat("MM-dd-yyyy");
@Override
public void serialize(Date date, JsonGenerator gen, SerializerProvider provider)
throws IOException, JsonProcessingException {
String formattedDate = dateFormat.format(date);
gen.writeString(formattedDate);
}
And the JSON retrieved by my GET controller (I'll be more precise when I can get it working at all)
以及由我的 GET 控制器检索的 JSON(当我完全可以让它工作时,我会更精确)
{"eventID":1,"eventTypeID":2,"date":"02-01-2014"}
采纳答案by MattR
You have a serializer, but no deserializer, so it's only working one way...
你有一个序列化器,但没有反序列化器,所以它只能以一种方式工作......
You also need:
您还需要:
@JsonDeserialize(using = DateDeserializer.class)
(with a DateDeserializer using the same date format).
(使用使用相同日期格式的 DateDeserializer)。
Why there isn't a single interface for both is a mystery to me :-)
为什么两者都没有一个单一的界面对我来说是个谜:-)
回答by javafan
Instead of string, just pass date object from jsp as below.
而不是字符串,只需从 jsp 传递日期对象,如下所示。
var date = new Date();
var formData = {'date':date};
And in the dto, make variable of type java.util.Date.
在 dto 中,创建 java.util.Date 类型的变量。