简体   繁体   English

使用Jersey时如何检索HttpServletRequest数据

[英]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: 我知道,如果我不使用Jersey,这会很容易,并且会使用类似以下的内容:

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? 但是,如果我确实想转储通过POST提交的表单的所有内容怎么办?

The problem is that I am using Jersey as the implementation of JAX-RS and using the code above outputs this: 问题是我使用Jersey作为JAX-RS的实现,并且使用上面的代码输出:

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. 我猜我的数据包含在这里:JaxrsRequestAttributes我不确定。

I know I am missing something here. 我知道我在这里缺少什么。 This isn't supposed to be difficult isn't it? 这不应该很难吗?


UPDATE UPDATE

As suggested Sotirios, 如建议的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. 为了使它起作用,我必须在参数中删除@FormParam,而忽略@Context HttpSerlvetRequest请求。

Are there no other way to output this in a more elegant way with out the need to remove @FormParam? 没有其他方法可以更优雅地输出此内容,而无需删除@FormParam? Maybe get the values from JaxrsRequestAttributes? 也许从JaxrsRequestAttributes获取值?

I tried to create a variable JaxrsRequestAttributes but it's a default class a can not access it directly. 我试图创建一个变量JaxrsRequestAttributes,但这是一个默认类,无法直接访问它。

Based on Sotirios comment's, here's the answer: 根据Sotirios的评论,这是答案:

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 : 可以使用以下命令访问HttpServletRequest:

@Context
private HttpServletRequest request;

Isn't that enough? 这还不够吗?

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 如何从 HttpServletRequest 中检索 RequestMappingInfo? - How can I retrieve RequestMappingInfo from HttpServletRequest? 如何从 Struts2 中的 HttpServletRequest 检索多部分/表单数据 - How to retrieve multipart/form-data from HttpServletRequest in Struts2 如何在java中从HttpServletRequest检索原始帖子数据 - How to retrieve raw post data from HttpServletRequest in java 如何从ServletContext获取HttpServletRequest? - How can I get HttpServletRequest from ServletContext? 如何在HttpSessionListener中获取HttpServletRequest? - How can I get HttpServletRequest when in an HttpSessionListener? 如何使用Jersey和Java EE 5检索ServletRequest - How to retrieve ServletRequest using Jersey and Java EE 5 如何使用Jsoup从html检索数据 - How can I retrieve data from html using Jsoup 如何使用 spring-data 检索 mongodb 集合? - How can I retrieve a mongodb collection using spring-data? 使用 Java Web 服务时如何访问 HttpServletRequest 对象 - How can I get access to the HttpServletRequest object when using Java Web Services 如何使用 spring 引导从 HttpServletRequest 获取位置(城市、国家/地区..)? - How can I get the location(city,country.. ) from HttpServletRequest using spring boot?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM