2

I have a Jersey rest webservice that runs on tomcat 6. I have a @Post method that consumes Multipart:

@Path("identify")   
@POST
@Consumes(MediaType.MULTIPART_FORM_DATA)
@Produces(MediaType.APPLICATION_XML)
    public String identifyReception(com.sun.jersey.multipart.MultiPart multiPart) throws Exception {

I would like to get the client IP address inside this method. I found out that you can use @Context HttpServletRequest inside Get method. But in post method I need the multipart input argument. I have also found out that tomcat does not support Servlet specification...

Is there another way I can do it?

srd.pl
  • 571
  • 10
  • 22
  • I don't see a direct way in the api, but have you tried getHeaders(). ie. multiPart.getBodyParts().get(0).getHeaders(); then search through the list of headers for the IP/Host – Sean May 27 '11 at 14:04

1 Answers1

3

I found out that you can use @Context HttpServletRequest inside Get method. But in post method I need the multipart input argument.

I don't think the two are mutually exclusive. Let your method take two arguments (I don't think it matters what order they're in) and annotate one of them with @Context. I believe that will work whether it's a get, post, whatever. You could also just annotate a field with @Context and Jersey will initialize it for you before it calls your @GET method.

Tyler
  • 21,762
  • 11
  • 61
  • 90
  • I tried this out on my own during the weekend before I saw your asnwer and I think this actually works but I need more testing. But Thanks :> – srd.pl May 30 '11 at 15:33
  • I tried to use `@Context HttpServletRequest inside Get` method but get `Type mismatch: cannot convert from Context to Annotation` error. Why? – Greenhand Sep 20 '13 at 13:00