[英]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.