如何从jersey servlet中的curl请求中提取参数? [英] How to extract parameters from curl request in jersey servlet?
问题描述
我正在向我的泽西servlet发送curl post restful请求,格式为
I am making a curl post restful request to my jersey servlet in the form
curl -i -X POST -d "debit_user_id=/custome/mobile_number:917827448775"http://localhost:8080/switch/apikongcall.do/transactions
$ b b
我需要在我的servlet中获取debit_user_id,我的Post方法的代码是
I need to fetch the debit_user_id in my servlet, code for my Post method is
@POST
//@Path("/transactions")
//@Consumes(MediaType.APPLICATION_JSON)
public Response createTrackInJSON(@QueryParam("debit_user_id") String debit_user_id) {
//Log logger = null;
this.logger = LogFactory.getLog(getClass());
this.logger.info("Inside post method"+debit_user_id);
String response = debit_user_id;
//String response = "testParam is: " + recipient_id + "\n";
//String result = "Track saved : " + track;
return Response.status(200).entity(response).build();
但我的debit_user_id是空的。是正确的方式使curl restful请求或我提取它在我的servlet中的方式是错误的。
我是新的jax-rs。感谢您提供帮助。
But my debit_user_id is coming as null. Is it the correct way to make the curl restful request or the way I am extracting it in my servlet is wrong. I am new to jax-rs. Thanks in advance for the help.
推荐答案
curl的-d选项传递一个url编码的表单参数。您必须将@QueryParam更改为@FormParam,使给定的Curl命令工作。此外,只需将参数名称指定为mobile_number,而不使用您在curl命令中使用的路径,例如:
The -d option to curl passes in a url encoded form parameter. You have to change @QueryParam to @FormParam to make the given Curl command work. Also, just specify the parameter name as mobile_number without the pathing that you used in you curl command, like so:
curl -i -X POST -d "debit_user_id=mobile_number:917827448775" http://localhost:8080/switch/apikongcall.do/transactions
映射到
@POST
@Consumes(MediaType.APPLICATION_FORM_URLENCODED)
public Response createTrackInJSON(@FormParam("mobile_number") String debit_user_id) {
...
}
如果你确实需要一个查询参数,你的curl命令将需要改变:
If you do in fact want a query parameter, your curl command would need to change:
curl -i -X POST http://localhost:8080/switch/apikongcall.do/transactions?mobile_number=917827448775
出于安全原因,最好在邮件正文中保留手机号码,因此我将使用FormParam而不是QueryParam。
For security reasons, it's probably better to keep the mobile number in the message body, so I'd use the FormParam instead of the QueryParam.
这篇关于如何从jersey servlet中的curl请求中提取参数?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!