Alex Rivera | Logout

How do I access the HTTP request?

Asked 2011-02-25T15:03:05.787
34

Say normally I have a REST method in Java

@POST 
    @Path("/test")
    @Produces(MediaType.APPLICATION_JSON)
    public String showTime(@FormParam("username") String userName) {

:
:
:
}

which is fine. However, I'm wondering is there a way I can access the full HTTP request with Jersey such as

@POST 
    @Path("/test")
    @Produces(MediaType.APPLICATION_JSON)
    public String showTime(@FormParam("username") String userName,@XXXXXX String httpRequest) {

:
:
:
}

where some annotation would give me the full HTTP request to store in a variable. I have tried using @POST but it doesn't seem to work. Any suggestions?

Edit
Report

2 Answers

56

You can use the @Context annotation:

@POST 
@Path("/test")
@Produces(MediaType.APPLICATION_JSON)
public String showTime(
    @FormParam("username") String userName,
    @Context HttpServletRequest httpRequest
) {
    // The method body
}
answered 2011-02-25T15:09:22.413
1

If you want to get the request body, you could use the tip lined out in this post: How to get full REST request body using Jersey?

If you need to know more about the request itself, you could try the @Context annotation as mentioned by sdorra.

answered 2011-02-25T15:13:18.230

Your Answer