【发布时间】:2014-03-25 01:21:35
【问题描述】:
我正在使用 angularjs $http 服务发布到 Spring REST 端点。这很简单,它按预期工作,但端点返回状态代码 500 内部错误。但是在服务器端,它没有显示任何错误和异常。我正在发送帖子请求如下
$http.post('../bookmarks/add',data,{
cache:false,
headers:{'Content-Type':'application/x-www-form-urlencoded'},
transformRequest:function(data){
if(data===undefined)
return data;
return $.param(data);
},
responseType:'json'
}).success(function(data,status,headers,config){
$('.add-form').hide();
console.log(data);
callback(data,'success');
}).error(function(data,status,headers,config){
console.log(status);
if(status==500)
callback('Server error! Try again later.','error');
});
我可以发送帖子请求。我的 REST 处理程序如下。它调用一个服务来持久化一个对象。
@RequestMapping(value="/add", method=RequestMethod.POST, consumes={"application/x-www-form-urlencoded","application/json", "text/plain", "*/*"})
@ResponseStatus(HttpStatus.OK)
@ResponseBody
public Bookmark saveBookmarks(@RequestParam("caption") String caption,@RequestParam("bookmarkUrl") String bookmarkUrl,
@RequestParam(value="tags",required=false) String tags,@RequestParam(value="category", required=false) String category){
System.out.println(caption+"-"+tags+"-"+category);
//calling service and persisting the object
Bookmark bookmark= bookmakService.saveBookmark(
binder.getNewBookmark(caption,bookmarkUrl, dateGen.getToday(), category, tags)
,getUserIDFromSession());
System.out.println(bookmark);
return bookmark;
}
上述处理程序按预期工作。对象被持久化。但是浏览器控制台显示 500 internal server error 并且服务器没有记录任何错误和异常。一切看起来都是直截了当的。我不知道为什么客户端会收到 500 错误。我正在使用 Apache Tomcat 7。
【问题讨论】:
-
您确定问题不在于您的服务器配置?
-
@JohannesH。感谢您的回复。其他 REST 控制器工作正常。我认为问题发生在杰克逊试图将对象转换为 JSON 时。只是我的意见。但是服务器没有显示任何错误。难以追踪。
-
您可以尝试设置记录器级别以跟踪以下包吗? “org.springframework.web.servlet.mvc”
-
我找到了解决方案。 Bookmark 对象具有 User 对象。我将用户对象设置为空,它工作正常。但书签 json 对象图仍有用户,现在为空。不希望用户出现在 json 对象图中怎么办?
-
我有一个类似的问题,正在尝试弄清楚如何获得有用的日志记录。
标签: spring rest spring-mvc