今天同事写了一个接口,接收的参数的post提交的表单数据:
a=123
但是怎么都接收不到,接收代码如下:
@RequestMapping(value ="/test", method = RequestMethod.POST)
public ErrorInfo test( A a, HttpServletRequest request){
System.out.println("我是请求参数");
System.out.println("-------------" +request.getParameter("a"));
System.out.println("-------------" + a.getA());
return null;
}
后来改成:
@RequestMapping(value ="/test", method = RequestMethod.POST)
public ErrorInfo test( String a){
System.out.println("我是请求参数");
//System.out.println("-------------" +request.getParameter("a"));
System.out.println("-------------" + a);
return null;
}
仍然接收不到,输出结果仍然为null;
把请求改为get请求,以上两种方式都能接收到参数。
又把代码改成这样:
@RequestMapping(value ="/test")
public ErrorInfo test(@RequestParam(value = "a", required = true) String a){
System.out.println("我是请求参数");
//System.out.println("-------------" +request.getParameter("bizData"));
System.out.println("-------------" + a);
return null;
}
结果报错了:
Error 400 Required String parameter 'a' is not presentHTTP ERROR 400
Problem accessing /xxx. Reason:
Required String parameter 'a ' is not present
Powered by Jetty:// 9.4.1.v20170120
想不到还有什么原因了,于是就换了容器,原来使用的是
jetty-maven-plugin
于是换成了weblogic , 结果第一种和第二种写法都可以接受到参数,第三种没有测试;
然后又换成了tomcat9,结果三种都不行;
又换成jetty9,三种都不行。
实在是想不出来了,希望知道的朋友解释一下。
补充:
测试工具用的是 junit httpclient 火狐http测试插件(类似谷歌插件postman)
测试代码如下:
junit测试
@Test
public void dtbCallbackNotifiyTest(){
try {
String url = "http://localhost:8091/xxx/xxx/test";
List formParameters = new ArrayList();
formParameters.add(new BasicNameValuePair("a", "123"));
UrlEncodedFormEntity urlEncodedFormEntity = new UrlEncodedFormEntity(formParameters, "utf-8");
HttpPost httpPost = new HttpPost(url);
httpPost.addHeader("Content-Type", "application/x-www-form-urlencoded;charset=UTF-8");
httpPost.setEntity(urlEncodedFormEntity);
System.out.println("-----------"+httpPost.getEntity());
closeableHttpResponse = this.closeableHttpClient.execute(httpPost);
} catch (Exception e) {
e.printStackTrace();
}
}
插件测试:
数据格式必须是key=value&key=value, 这种格式
测试截图: