简体   繁体   中英

@context httpServletRequest request for post method in jersey is null

I am getting multipart msg from android client. I am using jersey webservice to receive that multipart data. I can able to retrieve multipart data. but I can't able to use @Context HttpServletRequest request to get user id. My android client is,

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);

my jersey service is

@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);

the get session method is

protected String getSession(String token, HttpServletRequest req)
        throws ServletException, IOException {

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

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

    return value;
}

Here i am passing the request and token generated to retrieve user id. it works for get method. but for post method i am getting null value. pl help me. how to get request for post method in jersey.

What you are now doing is getting the context.getAttribute() from the ServletContext itself not from the HttpServletRequest. So it should be something like this: String value = (String) req.getAttribute(token) . Also keep in mind .getAttribute() method returns null if no attribute of the given name exists.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM