【问题标题】:@context httpServletRequest request for post method in jersey is null@context httpServletRequest 请求球衣中的 post 方法为空
【发布时间】:2015-01-08 10:40:04
【问题描述】:

我正在从 android 客户端获取多部分消息。我正在使用 jersey webservice 来接收该多部分数据。我可以检索多部分数据。但我无法使用@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 方法,我得到空值。请帮助我。如何在球衣中获取 post 方法的请求。

【问题讨论】:

    标签: java android jersey-2.0 jersey-1.0


    【解决方案1】:

    您现在所做的是从 ServletContext 本身而不是从 HttpServletRequest 获取context.getAttribute()。所以它应该是这样的:String value = (String) req.getAttribute(token)。另请记住,如果不存在给定名称的属性,则 .getAttribute() 方法返回 null。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2015-04-10
      • 2013-02-12
      • 2018-06-26
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多