我有一个接收POST请求的REST服务。 userPrincipal是必需参数,因为它在其他API中使用。无论如何,我似乎无法将其传递给HttpServletRequest对象。我怎样才能将userPrincipal值传递给他?我是否必须在标题,参数或其他地方添加它?两者似乎都不起作用。
REST服务代码:
@POST
@Path("{id}/dothings")
@Produces("application/json")
@Consumes("multipart/form-data")
public Response doThings(@Context HttpServletRequest request, @PathParam("id") String id, MultipartFormDataInput payload) {
String username = request.getUserPrincipal() != null ? request.getUserPrincipal().getName() : null;
//username is always null
doSomething(username, payload);
}
Ajax调用代码:
var param = new FormData();
param.append('param1' , value1);
param.append('param2' , value2);
$.ajax({
url: findUrl("dothings"),
dataType : "application/json",
contentType:false,
processData:false,
headers: {
"Authorization" : authAdmin, //string containing "Basic "+ base64encoding(username+":"+password), for authentication
"Accept" : "application/json"
},
type:"POST",
data: param,
success: function(data) {
alert("executed!");
},
error : function(jqXHR, error, errorThrown) {
alert("Error while completing task: " + errorThrown);
}
});