简体   繁体   中英

How can I retrieve HttpServletRequest data while using Jersey

I know that this is an easy one if I am not using Jersey and would use something like this:

Enumeration<String> params = request.getParameterNames();
while(params.hasMoreElements()){
    String paramName = (String)params.nextElement();
    System.out.println("Parameter Name - "+paramName+", Value - "+request.getParameter(paramName)); 
}

params = request.getHeaderNames();
while(params.hasMoreElements()){
    String paramName = (String)params.nextElement();
    System.out.println("Header Name - "+paramName+", Value - "+request.getHeader(paramName));
}

params = request.getAttributeNames();
while(params.hasMoreElements()){
    String paramName = (String)params.nextElement();
    System.out.println("Attribute Name - "+paramName+", Value - "+request.getAttribute(paramName));
}

I am also aware that I can do this and be done with it.

@FormParam("location") String location

But what if I do want to dump all the contents of the form submitted via POST?

The problem is that I am using Jersey as the implementation of JAX-RS and using the code above outputs this:

Attribute Name - org.glassfish.jersey.server.spring.scope.RequestContextFilter.REQUEST_ATTRIBUTES, Value - org.glassfish.jersey.server.spring.scope.JaxrsRequestAttributes@11e035a
Attribute Name - org.glassfish.jersey.message.internal.TracingLogger, Value - org.glassfish.jersey.message.internal.TracingLogger$1@16e45c8

I am guessing that my data is contained here: JaxrsRequestAttributes I am not sure though.

I know I am missing something here. This isn't supposed to be difficult isn't it?


UPDATE

As suggested Sotirios,

This is the code get the dump of the form.

try {
    InputStream is = request.getInputStream();
    int i;
    char c;
    while((i=is.read())!=-1)
        {
        // converts integer to character
        c=(char)i;

        // prints character
        System.out.print(c);
        }

    } catch (IOException e) {
        // TODO Auto-generated catch block
        e.printStackTrace();
    }

In order for this to work, I had to remove @FormParam in my parameter and leave out @Context HttpSerlvetRequest request.

Are there no other way to output this in a more elegant way with out the need to remove @FormParam? Maybe get the values from JaxrsRequestAttributes?

I tried to create a variable JaxrsRequestAttributes but it's a default class a can not access it directly.

Based on Sotirios comment's, here's the answer:

Here's the method signature

public Response authenticateUser(MultivaluedMap<String, String> form)

Iterator<String> it = form.keySet().iterator();
while(it.hasNext()){
    String theKey = (String)it.next();
    System.out.println("Parameter Name - "+theKey+", Value - "+form.getFirst(theKey)); 
}
System.out.println(form);

The HttpServletRequest can be accessed using :

@Context
private HttpServletRequest request;

Isn't that enough?

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