简体   繁体   English

是否可以从REST请求中获取POST参数?

[英]Is it possible to get POST parameters from REST request?

Is it possible to get POST parameters from REST request? 是否可以从REST请求中获取POST参数?

I tried the following with no success: 我尝试了以下操作,但均未成功:

MultivaluedMap<String, String> params = uriInfo.getQueryParameters();
log.info("Params Size: "+params.size());
Iterator<String> it = params.keySet().iterator();
String theKey = null;
while(it.hasNext()){
    theKey = it.next();
    log.info("Here is a Key: "+theKey);
}

Here is my method signature: 这是我的方法签名:

@POST
@Produces("application/pdf")
@Path("/hello")
public Response producePDF(@FormParam("filename")String fileName, @Context UriInfo uriInfo)

Logs show 0 for "Params Size:" 日志显示“参数大小:”为0

Can I only use a GET? 我只能使用GET吗?

@Roman Vottner Your answer was it. @罗马·沃特纳你的答案就是它。 I needed to inject the MultivaluedMap instead of constructing at method call. 我需要注入MultivaluedMap而不是在方法调用时进行构造。

Code: 码:

@POST
    @Produces("application/pdf")
    @Path("/hello")
    @Consumes("application/x-www-form-urlencoded")
    public Response producePDF(MultivaluedMap<String, String> params)

Iterator<String> it = params.keySet().iterator();
            String theKey = null;
            while(it.hasNext()){
                theKey = it.next();
                log.info("Here is a Key: "+theKey);
                if(theKey.equals("filename")){
                    fileName = params.getFirst(theKey);
                    System.out.println("Key: "+theKey);
                }
            }

I can now get the parameters! 我现在可以获取参数了!

If by "POST parameter" you mean "query parameter", then you want uriInfo.getQueryParameters() . 如果通过“ POST参数”表示“查询参数”,则需要uriInfo.getQueryParameters() If not, you'll need to explain what you actually mean. 如果不是,则需要解释您的实际意思。

If you are using a HTML form, you can try the next: 如果您使用的是HTML表单,则可以尝试下一个:

HTML : HTML

<form action="rest/resource/hello" method="post" target="_blank">
    <fieldset>
        <legend>Download PDF</legend>
        <label>Filename: <input type="text" name="filename" required></label>
        <button>Submit</button>
    </fieldset>
</form>

Java : Java的

@POST
@Path("/hello")
@Consumes(MediaType.APPLICATION_FORM_URLENCODED)
@Produces("application/pdf")
public Response producePDF(@FormParam("filename") String fileName) {

    // Do something

    ...
}

For Json 对于杰森

@POST
@Consumes("application/json")
public void fRestEndPoint(Map<String, String> params) throws IOException, JSONException {
    log.info("Received a f rest call ");
    String output = params.toString();
    log.info(output);

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

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM