java 接收post表单,spring mvc 接收post 表单数据

今天同事写了一个接口,接收的参数的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 present

HTTP 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();

}

}

插件测试:

bVR8ym?w=627&h=288

数据格式必须是key=value&key=value, 这种格式

测试截图:

bVR9bO?w=636&h=302

bVR9bT?w=892&h=338

bVR9b5?w=846&h=264

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值