[英]Is it possible to get POST parameters from REST request?
是否可以從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
我只能使用GET嗎?
@羅馬·沃特納你的答案就是它。 我需要注入MultivaluedMap而不是在方法調用時進行構造。
碼:
@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);
}
}
我現在可以獲取參數了!
如果通過“ POST參數”表示“查詢參數”,則需要uriInfo.getQueryParameters()
。 如果不是,則需要解釋您的實際意思。
如果您使用的是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
...
}
對於傑森
@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.