[英]How do I read POST parameters for a RESTful service using Jersey?
I am not using JSON or anything like that. 我没有使用JSON或类似的东西。 I have a simple form to upload a file and I want to read the parameters of the form.
我有一个简单的表单来上传文件,我想读取表单的参数。 The code below is not working as expected.
以下代码无法按预期工作。 It will not show any parameters.
它不会显示任何参数。
@POST
@Consumes(MediaType.APPLICATION_FORM_URLENCODED)
@Path("{appNum}/{docId}/file")
public Response uploadDocFile(
@PathParam("appNum") String appNum,
@PathParam("docId") String docId,
@Context HttpServletRequest req)
{
try {
log.info("POST Parameters:");
Enumeration e = req.getParameterNames();
while(e.hasMoreElements())
{
Object key = e.nextElement();
log.info("Key: " + key);
log.info("Val: " + req.getParameter(key.toString()));
}
} catch (Exception e) {
e.printStackTrace();
return Response.status(Status.INTERNAL_SERVER_ERROR).entity(new StatusResponse(e)).build();
}
return Response.ok().build();
}
FYI, You need to use @FormParam. 仅供参考,您需要使用@FormParam。 Also make sure INPUT HTML types are using name= not id=.
还要确保INPUT HTML类型使用name = not id =。
I have the same problem. 我也有同样的问题。 Using
@FormParam
annotation for individual parameters works, but reading them from HttpServletRequest
injected through @Context
doesn't. 对单个参数使用
@FormParam
注释有效,但是从通过@Context
注入的HttpServletRequest
读取它们不起作用。 I also tried to get the request object/parameters through Guice using Provider<HttpServletRequest>
and @RequestParameters<Map<String, String[]>>
. 我还尝试使用
Provider<HttpServletRequest>
和@RequestParameters<Map<String, String[]>>
通过Guice获取请求对象/参数。 In both cases there were no post parameters. 在这两种情况下都没有帖子参数。
However, it is possible to get a map of parameters by adding a MultivaluedMap<String, String>
parameter to resource method. 但是,可以通过向resource方法添加
MultivaluedMap<String, String>
参数来获取参数映射。 Example: 例:
@POST
public void doSomething(MultivaluedMap<String, String> formParams) {
//...
}
If you are using Jersey RESTful API in JAVA you can look for Parameter Annotations (@*Param) 如果您在JAVA中使用Jersey RESTful API ,则可以查找参数注释(@ * Param)
Example: 例:
Dependency: 相关性:
<dependency>
<groupId>com.sun.jersey</groupId>
<artifactId>jersey-client</artifactId>
<version>1.8</version>
</dependency>
Code: 码:
package yourpack;
import javax.ws.rs.GET;
import javax.ws.rs.Path;
import javax.ws.rs.PathParam;
import javax.ws.rs.core.Response;
@Path("/path_to_data")
public class DataResource {
@GET
@Path("/{param}")
public Response getMsg(@PathParam("param") String urlparam) {
int ok = 200;
String result = "Jersey Data resource: " + urlparam;
return Response.status(ok).entity(result ).build();
}
}
List of annotations: @MatrixParam , @HeaderParam , @CookieParam , @FormParam , @QueryParam , @PathParam 注释列表: @ MatrixParam , @ HeaderParam , @ CookieParam , @ FormParam , @ QueryParam , @ PathParam
At some point of time Jersey ContainerServlet (or other Jersey object during request processing) calls request.getInputStream() or request.getReader() which set 'usingInputStream' or 'usingReader' to TRUE. 在某些时候,Jersey ContainerServlet(或请求处理期间的其他Jersey对象)调用request.getInputStream()或request.getReader(),它将'usingInputStream'或'usingReader'设置为TRUE。 This state prevents populating of parameters map inside the request object.
此状态可防止在请求对象内填充参数映射。 Something like this:
像这样的东西:
parseParameters() {
if (usingInputStream || usingReader) {
return;
} else {
parametersMap.putAll({actual parameters parsing from stream})
}
}
Map getParametersMap() {
return parametersMap;
}
Try putting a break point at the very first entry point (beginning of Jersey ServletContainer.service() method) of your application and evaluate request.getParametersMap() call. 尝试在应用程序的第一个入口点(Jersey ServletContainer.service()方法的开头)放置一个断点并评估request.getParametersMap()调用。 You'll get your parameters.
你会得到你的参数。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.