在Jersey中,针对POST方法的HttpServletRequest请求上下文为空。

3

我从安卓客户端接收到了多部分消息。我使用Jersey Web服务来接收这个多部分数据。我能够检索到多部分数据,但是我无法使用@Context HttpServletRequest请求获取用户ID。我的安卓客户端如下:

HttpClient httpClient = new DefaultHttpClient();

Log.e("Picture Upload URL is:", QueryConfig.PROTOCOL+ StaticHelper.HOST + StaticHelper.port+QueryConfig.projectService+QueryConfig.sendProfilePicture);

HttpPost postRequest = new HttpPost(QueryConfig.PROTOCOL+ StaticHelper.HOST + StaticHelper.port+QueryConfig.projectService+QueryConfig.sendProfilePicture);

ByteArrayBody bab = new ByteArrayBody(data,StaticHelper.UserID+".jpg");
MultipartEntity reqEntity = new MultipartEntity(HttpMultipartMode.BROWSER_COMPATIBLE);
reqEntity.addPart("file", bab);
reqEntity.addPart("fileFilename",new StringBody(StaticHelper.UserID+".jpg"));
HttpResponse response = httpClient.execute(postRequest);

我的球衣服务是:
@Path("/mobileUserPictureInsert")
@POST
@Consumes("multipart/*")
@Produces(MediaType.APPLICATION_JSON)
public String save(@Context HttpServletRequest request, MultiPart multiPart)
        throws ParseException {
    BodyPartEntity bpeTokenId = (BodyPartEntity) multiPart.getBodyParts()
            .get(2).getEntity();
    try {
        tokenId = getString(bpeTokenId.getInputStream());
        String userId = "";
        userId = getSession(tokenId, request);

获取会话方法是:
protected String getSession(String token, HttpServletRequest req)
        throws ServletException, IOException {

    String value = (String) context.getAttribute(token);

    LOG.info("Retrive Token Value-->" + value);

    return value;
}

在这里,我正在传递请求和生成的令牌以检索用户ID。对于GET方法它有效,但对于POST方法我得到了空值。请帮助我。如何在Jersey中获取POST方法的请求。

1个回答

0
你现在正在从ServletContext本身获取context.getAttribute(),而不是从HttpServletRequest中获取。因此,应该像这样:String value = (String) req.getAttribute(token)。还要记住,如果没有给定名称的属性,则.getAttribute()方法返回null。

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接