【发布时间】:2018-02-20 10:20:49
【问题描述】:
我有一个使用 Jersey 的 rest web 服务实现,它在发布 XML 时返回一个响应。我在我的 jsp 中使用 jquery ajax 调用将 XML 内容发布到 Web 服务,如下所示。下面的代码工作正常,我得到了 XML 响应并调用了成功函数,我能够根据需要获取新发布的 XML 内容。
但是,我想在下面的成功函数中获取响应标头的值,例如内容类型、状态等。有没有办法我们可以做到这一点。当我使用邮递员进行验证时,我会看到内容类型、状态和其他详细信息。我想在ajax调用的成功函数中获取这些值。
@POST
@Consumes(MediaType.APPLICATION_XML)
@Produces(MediaType.APPLICATION_XML)
public Response postMessage(Message msg, @Context UriInfo uriInfo){
msgService.addMessage(msg);
URI uri = uriInfo.getAbsolutePathBuilder().path(String.valueOf(msg.getId())).build();
Response rs = Response.created(uri)
.entity(msg)
.status(Status.CREATED)
.build();
System.out.println(rs);
return rs;
}
下面是ajax调用:
$.ajax({
type: "POST",
url: "http://localhost:9090/messenger/webapi/messages",
data: message,
contentType: "application/xml; charset=utf-8",
dataType: "xml",
success: function(msg, status) {
$(msg).find("autor").each(function(){
alert($(this)[0].childNodes[0].nodeValue);
});
//Would like to alert the reponse headers like content-type received and status (201 in this case) here..
},
error: function(e){
}
});
任何帮助将不胜感激。
【问题讨论】:
-
谢谢!!我可以通过执行“request.getResponseHeader("Content-type");”来引用这篇文章来获取 Content-type 的值,请告诉我如何获取状态(例如:201 Created)
-
您将状态作为参数,为什么不打印它并查看它的值?您还可以打印“请求”对象中的任何内容,它应该包含状态信息。
-
status 没有提供像 201 created 这样的状态代码。它只是打印出我不想要的“成功”。我尝试了几个选项。请求对象也不包含这个
标签: java rest web-services jersey response