2014-09-03 78 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); 
相關問題