java - spring mvc 接收post 表单数据

查看:757
本文介绍了java - 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;
}

结果报错了:

<html>
<head>
<meta http-equiv="Content-Type" content="text/html;charset=utf-8"/>
<title>Error 400 Required String parameter &apos;a&apos; is not present</title>
</head>
<body><h2>HTTP ERROR 400</h2>
<p>Problem accessing /xxx. Reason:
<pre>    Required String parameter &apos;a &apos; is not present</pre></p><hr><a href="http://eclipse.org/jetty">Powered by Jetty:// 9.4.1.v20170120</a><hr/>

</body>
</html>

想不到还有什么原因了,于是就换了容器,原来使用的是

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<NameValuePair> formParameters = new ArrayList<NameValuePair>();
             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, 这种格式

测试截图:


解决方案

POSTMAN模拟请求的?还是自己写了个form表单?
1.你的A对象里有 a这个属性这个字段么?
2.第二个test方法没写错么?


按照你的请求工具来看,参照postman,我没猜错的话

content to send 这个选项卡下,填写的应该是,json格式的字符串,然后后台你需要用@RequestBody来接受。

所以你应该用 Parameters 这个选项卡下的请求参数名和值


测试1,使用错误,使用Parameters会在地址栏后面加参数,去掉就相当于没有参数,如果改为GET,则不会进入请求

测试2,@RequestBody,json格式

测试3,不同的请求头

我一直忘了一个东西,请求头....
我要吐槽下,图片编辑真麻烦


demo-百度网盘

这篇关于java - spring mvc 接收post 表单数据的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆