Angular Js在Java Spring mvc中发布日期错误请求 [英] Angular Js post date Bad Request in Java Spring mvc
问题描述
在Java Spring MVC项目中,我将对象发布到@RestController,并且我发布的对象具有日期属性。如果我删除此属性,该帖子可以成功运行。但是使用date属性,它会返回400个错误请求。
在dto中,日期是java.util.Date
In Java Spring MVC project, I post an object to a @RestController and the object I post has an date property. If I remove this property, the post works successfully. But with the date property, it returns a 400 bad request. In the dto the Date is java.util.Date
控制器中的方法:
@RequestMapping(value = "/users/createPetition", method = RequestMethod.POST)
public @ResponseBody PetitionDTO addPetition(@RequestBody PetitionRequestDTO petitionDto, Model model) { ...
PetitionRequestDTO
PetitionRequestDTO
public class PetitionRequestDTO {
private Long userId;
private Long categoryId;
private String title;
private String description;
private Date initialDate;
private String address; //getters setters
角度js调用
if ($scope.petitionForm.$valid) {
$http.post(getCompletePath("users/createPetition"), JSON.stringify($scope.newPetition))
.success(function (petition) {
}).error(function (data, status, headers, config) {
});
在js中,日期具有下一个值:Thu Mar 19 2015 00:00:00 GMT- 0300(阿根廷标准时间)
In the js the date has the next value: Thu Mar 19 2015 00:00:00 GMT-0300 (Argentina Standard Time)
完整的json是:
"{"selectedCategory":{"id":3,"name":"Plomero","description":"Plomeria"},"name":"aaa","title":"bbb","description":"ccc","initialDate":"2015-03-19T03:00:00.000Z","address":"asd","categoryId":3}"
推荐答案
您需要确保JSON日期的JS代码中有更好的格式。这里有一个你应该考虑的讨论 - 正确。 JSON日期格式。
You need to ensure a better format in your JS code for the JSON Date. There is a discussion here that you should consider - The "right" JSON date format.
按顺序执行此操作后,您需要在Spring MVC中使用相应的日期时间格式化程序才能将JSON日期字符串转换为日期对象 - spring mvc date format with form:input ;这是另一个示例。
Once you have this in order you need a corresponding Date Time Formatter in Spring MVC to be able to convert the JSON Date String into Date Object - spring mvc date format with form:input; and here's another example.
这篇关于Angular Js在Java Spring mvc中发布日期错误请求的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!