java - HttpServletRequest.getParamter() return null in CXF Restful service(Post) -
i wrote web api using apache cxf. when use httpservletrequest.getparamter() in post method, return null.here code:
@path("/") public class tokenservice extends digiwinbaseservice { private static void printrequest(httpservletrequest httprequest) { system.out.println("\n\n headers"); enumeration headernames = httprequest.getheadernames(); while (headernames.hasmoreelements()) { string headername = (string) headernames.nextelement(); system.out.println(headername + " = " + httprequest.getheader(headername)); } system.out.println("\n\n parameters"); enumeration params = httprequest.getparameternames(); while (params.hasmoreelements()) { string paramname = (string) params.nextelement(); system.out.println(paramname + " = " + httprequest.getparameter(paramname)); } system.out.println("\n\n row data"); system.out.println(extractpostrequestbody(httprequest)); } private static string extractpostrequestbody(httpservletrequest request) { if ("post".equalsignorecase(request.getmethod())) { scanner s = null; try { s = new scanner(request.getinputstream(), "utf-8").usedelimiter("\\a"); } catch (ioexception e) { e.printstacktrace(); } return s.hasnext() ? s.next() : "null"; } return "null"; } @post @consumes("application/x-www-form-urlencoded") public response authorize(@formparam("param") string param, @formparam("param2") string param2,@context httpservletrequest httprequest) throws oauthsystemexception { printrequest(httprequest); system.out.println("param:"+param); system.out.println("param2:"+param2); return response.status(httpservletresponse.sc_ok).entity("ok").build(); } }
here test code
public class httpclienttest { public static void main(string[] args) throws exception{ string url4 = "/api/services/test"; string host = "127.0.0.1"; httpclient httpclient = new httpclient(); httpclient.gethostconfiguration().sethost(host, 8080, "http"); httpmethod method = postmethod(url4); httpclient.executemethod(method); string response = method.getresponsebodyasstring(); system.out.println(response); } private static httpmethod postmethod(string url) throws ioexception{ postmethod post = new postmethod(url); post.setrequestheader("content-type","application/x-www-form-urlencoded;charset=gbk"); namevaluepair[] param = { new namevaluepair("param","param1"), new namevaluepair("param2","param2"),} ; post.setrequestbody(param); post.releaseconnection(); return post; } }
here print out :
headers content-type = application/x-www-form-urlencoded;charset=gbk user-agent = jakarta commons-httpclient/3.1 host = 127.0.0.1:8080 content-length = 26 parameters row data null param:param1 param2:param2
why parameters null? how can post params using httpservletrequest.getparamter()
cxf consuming post data fill formparams.
https://issues.apache.org/jira/browse/cxf-2993
the resolution "won't fix". in issue, suggest use multivaluedmap recover params, or use httpservletrequest
option 1
@post @consumes("application/x-www-form-urlencoded") public response authorize( multivaluedmap<string, string> parametermap, @context httpservletrequest httprequest) throws oauthsystemexception { //parametermap has post parameters
option 2
@post @consumes("application/x-www-form-urlencoded") public response authorize( @context httpservletrequest httprequest) throws oauthsystemexception { //httprequest.getparametermap() has post parameters