2014-09-03 76 views
1

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

我尝试没有成功如下:

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); 
} 

这里是我的方法签名:

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

日志显示为0 “PARAMS尺寸:”

我只能用一个得到?

+0

什么是'uriInfo'? – 2014-09-03 15:50:47

+0

@SotiriosDelimanolis泽西注射剂,提供有关被调用的URI的信息。 – 2014-09-03 15:52:05

+0

它的'getPathParameters()'方法是做什么的? – 2014-09-03 15:52:41

回答

2

@罗曼·沃特纳你的答案就是这样。我需要注入多值映射,而不是在方法调用中构造。

代码:

@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); 
       } 
      } 

我现在能得到的参数!

0

如果用“POST参数”表示“查询参数”,那么你想要uriInfo.getQueryParameters()。如果没有,你需要解释你的意思。

+0

我有一个html表单,method =“POST”。我切换到getQueryParameters,但没有好处。我猜getQueryParameters是GET?我不知道请求中会包含多少参数,所以我想迭代它们。 – 2014-09-03 16:10:38

0

如果您使用的是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

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

    // Do something 

    ... 
} 
0

对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); 
相关问题