spring开发篇二:@RequestParam和@RequestBody与前端Get和Post请求传参详解附中文乱码解决方法

1.先入为主:

1.1.在spring的controller中注解写法规则:

(a)同一个请求中,只能有一个@RequestBody;

(b)同一个请求中,可以有多个@RequestParam;

(c)同一个请求中,可以同时存在@RequestBody和@RequestParam;


1.2.前端后参数传入和接收规则:

(a)@RequestParam()指定的参数类型:普通元素、数组、集合等等

(b)当@RequestBody与@RequestParam()同时使用时,原SpringMVC接收参数的机制不变,RequestBody接收的是请求体里面的数据;

而RequestParam接收的是请求行中key-value参数它会被springMVC的切面进行处理从而可以用普通元素、数组、集合、对象等接收

(c)如果参数是放在请求体中:后台要用@RequestBody才能接收到

(d)如果参数放在请求行以?key=value形式传递:后台要用@RequestParam才能接收,或则形参不加@RequestParam注解也能接收。
         例如:
              @RequestParam() String xx             前端xx=StringValue

                                              String xx             前端xx=StringValue
              @RequestParam() int xx                   前端xx=intValue
              @RequestParam() Integer xx           前端xx=intValue
              @RequestParam() double xx           前端xx=10.12
              @RequestParam() boolean xx          前端xx=true false
              @RequestParam() Map xx                前端xx={k1=12,ssd="sd"}
              @RequestParam() List xx                 前端xx={k1=12,ssd="sd00"},{k1=1223,ssd="sd11"}.......


(e)如果参数前指定注解@RequestParam(value="key"),那么前端请求行中必须有对应的key(login.do?key=xxxx)

     例如:              

            public String login(@RequestParam(value = "token",required = false)String token)

            其中,required默认是true,请求行参数中必须有token=xx否则报400错误,若required设置为false则可以不传token。

(f)如果参数前指定注解@RequestParam(value="key"),那么就前端请求行中可以有也可以没有对应的key名字,若有key,则会自动匹配;若没有,请求也能正确发送。

(g)如果参数指定注解@RequestBody且参数是一个POJO对象或Map<String,Object>,那么,请求头必须设置application/json且前端请求体数据格式的字段名称必须与POJO类字段名称一致,否则报错400,前端传入的参数字段数量必须少于等于POJO类的字段数量

 

2.实验过程:

http请求规范说明:
常规操作下,Get在
请求行以?key=value形式传递参数(value可以是:[1,2,3],{a=s,k=oom,p="x"},{as:"xx",moo:00}等等);Post请求在请求体传递参数(string、json均可)。而经测试,Get其实也可以在请求体中传递参数的,但由于可能存在某些情况
下服务器不支持,如:缓存代理服务器。因此,我们通常在请求体中传递参数还是会使用Post请求方式。

上述引申的两个概念(请求行和请求体)请自行问度娘。

因此,下面的实验场景Get方式均不在请求体中传参。而post方式可在请求行也可在请求体传参。(牢记!)


前后端处理请求的各个场景:


场景1:


后台:
@ResponseBody
public String testAny1(String token)

请求:
get无参数访问:访问正常,响应正常,接收参数token为null
post无参数访问:访问正常,响应正常,接收参数token为null
get传参访问,请求行?token=xxx访问:访问正常,响应正常,接收参数token正常
post传参访问,请求行参数?token=xxx访问:访问正常,响应正常,接收参数token正常
post传参访问,请求体参数token访问:访问正常,响应正常,接收参数token为null
post传参访问,请求体参数{token:xxx}访问:访问正常,响应正常,接收参数token为null


正确使用小结:参数未指定任何注解时,请求使用Get或Post均可,token必须在请求行以?token=xx方式传递才能获取到,若不传递token参数不会报错。
注意:请求头使用默认即可,若设置其他请求头也不会报错

场景2:


后台:
@ResponseBody
public String testAny2(@RequestParam String token)

请求:
get无参数访问:报错400,(HTTP Status 400 - Required String parameter 'token' is not present)
post无参数访问:报错400,(HTTP Status 400 - Required String parameter 'token' is not present)
get传参访问,请求行?token=xxx访问:访问正常,响应正常,接收参数token正常
post传参访问,请求行参数?token=xxx访问:访问正常,响应正常,接收参数token正常
post传参访问,请求体参数token访问:报错400,(HTTP Status 400 - Required String parameter 'token' is not present)
post传参访问,请求体参数{token:xxx}访问:报错400,(HTTP Status 400 - Required String parameter 'token' is not present)

正确使用小结:参数指定@RequestParam注解时,请求使用Get或Post均可,token必须传递且在请求行以?token=xx方式传递才不会报错且后台才能获取到。
注意:请求头使用默认即可,若设置其他请求头也不会报错

场景3:


后台:
@ResponseBody
public String testAny3(@RequestParam("token") String token)

请求:
get无参数访问:报错400,(HTTP Status 400 - Required String parameter 'token' is not present)
post无参数访问:报错400,(HTTP Status 400 - Required String parameter 'token' is not present)
get传参访问,请求行?token=xxx访问:访问正常,响应正常,接收参数token正常
post传参访问,请求行参数?token=xxx访问:访问正常,响应正常,接收参数token正常
post传参访问,请求体参数token访问:报错400,(HTTP Status 400 - Required String parameter 'token' is not present)
post传参访问,请求体参数{token:xxx}访问:报错400,(HTTP Status 400 - Required String parameter 'token' is not present)

正确使用小结:参数指定@RequestParam("token")注解时,请求使用Get或Post均可,token必须传递且在请求行以?token=xx方式传递才不会报错且后台才能获取到。
注意:请求头使用默认即可,若设置其他请求头也不会报错

场景4:


后台:
@ResponseBody
public String testAny4(@RequestBody String params)

请求:
get无参数访问:访问正常,响应正常,接收参数params为""
post无参数访问:访问正常,响应正常,接收参数params为""
get传参访问,请求行?params=xxx访问:访问正常,响应正常,接收参数为""
post传参访问,请求行?params=xxx访问:访问正常,响应正常,接收参数为""
post传参访问,请求体参数params(String)访问:访问正常,响应正常,接收参数为String值
post传参访问,请求体参数params(json)访问:访问正常,响应正常,接收参数为jsonString值

设置请求头Content-Type:application/json,报错:报错400,The request sent by the client was syntactically incorrect.
这里使用使用text请求头或默认请求头

正确使用小结:参数指定@RequestBody注解时,请求使用Get或Post均可,params必须在请求体传递后台才能获取到。(推荐post方式发送带有请求体参数请求)
注意:请求头使用默认即可,若设置Content-Type:application/json或Content-Type:text/html请求头会报错400,设置为其余请求头不会报错。

场景5:


后台:
@ResponseBody
public RequestPagination testAny5(@RequestBody RequestPagination params)

请求:
get无参数访问:肯定报错415或400,
post无参数访问:肯定报错415或400,
get传参访问,请求行?params=xxx访问:肯定报错415或400,
post传参访问,请求行?params=xxx访问:肯定报错415或400,
post传参访问,请求体参数params(String)访问:肯定报错415或400,
post传参访问,请求体参数params(json)访问:可能报错415或400,

未设置请求头Content-Type:application/json:报错415,The server refused this request because the request entity is in a format not supported by the requested resource for the requested method
设置请求头Content-Type:application/json,但不在请求体传参或传参格式不正确则报错:报错400,The request sent by the client was syntactically incorrect.

请求头Content-Type:application/json,设置后,在请求体传入正确格式json(字段与RequestPagination字段对应)才能访问正常,响应正常且参数接收正常。

正确使用小结:参数指定@RequestBody注解且参数为pojo对象时,请求使用Get或Post均可,params必须在请求体并且以json形式保持字段与后台参数pojo对象字段一致,才不会报错后台才能获取到。(推荐post方式发送带有请求体参数请求)。如果请求体中某个字段value没有值,那么可以不写入请求体的json中。
注意:请求头使用Content-Type:application/json,若设置其他请求头会报错415。

场景6:


后台:
@ResponseBody
public String testAny6(@RequestBody String params,@RequestParam("token") String token)

请求:
设置请求头Content-Type:application/json:
get无参数访问:肯定报错400,The request sent by the client was syntactically incorrect
post无参数访问:肯定报错400,The request sent by the client was syntactically incorrect
get传参访问,请求行?params=xxx访问:肯定报错400,The request sent by the client was syntactically incorrect
post传参访问,请求行?params=xxx访问:肯定报错400,The request sent by the client was syntactically incorrect
post传参访问,请求体参数params(String)访问:肯定报错400,The request sent by the client was syntactically incorrect
post传参访问,请求体参数params(json)访问:肯定报错400,The request sent by the client was syntactically incorrect

设置请求头Content-Type:text 或 Content-Type:text/plain 或 默认:
get无参数访问:HTTP Status 400 - Required String parameter 'token' is not present,
post无参数访问:HTTP Status 400 - Required String parameter 'token' is not present
get传参访问,请求行?token=xxx&params=xx访问:正常访问、响应、接收参数params为""
post传参访问,请求行?token=xxx&params=xxx访问:正常访问、响应、接收参数params为""
post传参访问,请求体参数params(String)且请求行参数token=xxx访问:正常访问、响应、接收参数正常。
post传参访问,请求体参数params(json)且请求行参数token=xxx访问:正常访问、响应、接收参数正常。


正确使用小结:同时存在参数指定@RequestBody注解,和参数指定@RequestParam("xxx")注解时候,请求使用Get或Post均可,params必须在请求体传递并且在请求行以?token=xx传递,才不会报错后台才能获取到两参数。(推荐post方式发送带有请求体参数请求)
注意:请求头使用默认即可,若设置Content-Type:application/json或Content-Type:text/html请求头会报错400,设置为其余请求头不会报错。

场景7:


后台:
@ResponseBody
public String testAny7(@RequestBody RequestPagination params,@RequestParam("token") String token)

请求:
设置请求头Content-Type:application/json:
get无参数访问:肯定报错400,The request sent by the client was syntactically incorrect
post无参数访问:肯定报错400,The request sent by the client was syntactically incorrect
get传参访问,请求行?params=xxx访问:肯定报错400,The request sent by the client was syntactically incorrect
post传参访问,请求行?params=xxx访问:肯定报错400,The request sent by the client was syntactically incorrect
post传参访问,请求体参数params(String)访问:肯定报错400,The request sent by the client was syntactically incorrect
post传参访问,请求体参数params(json)访问:肯定报错400,The request sent by the client was syntactically incorrect
post传参访问,请求体参数params(json且字段与RequestPagination字段对应),且请求行参数token=xxx访问才能访问正常,响应正常且参数接收正常。


设置请求头Content-Type:text 或 Content-Type:text/plain 或 默认:
get无参数访问:415,The server refused this request because the request entity is in a format not supported by the requested resource for the requested method
post无参数访问:同上
get传参访问,请求行?token=xxx&params=xx访问:同上
post传参访问,请求行?token=xxx&params=xxx访问:同上
post传参访问,请求体参数params(String)且请求行参数token=xxx访问:同上
post传参访问,请求体参数params(json)且请求行参数token=xxx访问:同上


正确使用小结:同时存在参数指定@RequestBody注解且为pojo对象,和参数指定@RequestParam("xxx")注解时候,请求使用Get或Post均可,params必须在请求体并且以json形式保持字段与后台参数pojo对象字段一致并且在请求行以?token=xx传递,才不会报错后台才能获取到两参数。(推荐post方式发送带有请求体参数请求)。如果请求体中某个字段value没有值,那么可以不写入请求体的json中。
注意:请求头使用Content-Type:application/json,若设置其他请求头会报错415。

场景8:


后台:
@ResponseBody
public String testAny8(@RequestBody String params,String token)

请求:
设置请求头Content-Type:application/json:
get无参数访问:肯定报错400,The request sent by the client was syntactically incorrect
post无参数访问:肯定报错400,The request sent by the client was syntactically incorrect
get传参访问,请求行?params=xxx访问:肯定报错400,The request sent by the client was syntactically incorrect
post传参访问,请求行?params=xxx访问:肯定报错400,The request sent by the client was syntactically incorrect
post传参访问,请求体参数params(String)访问:肯定报错400,The request sent by the client was syntactically incorrect
post传参访问,请求体参数params(json)访问:肯定报错400,The request sent by the client was syntactically incorrect
post传参访问,请求体参数params(json),且请求行参数token=xxx访问:肯定报错400,The request sent by the client was syntactically incorrect


设置请求头Content-Type:text 或 Content-Type:text/plain 或 默认:
get无参数访问:请求访问正常、响应正常。
post无参数访问:请求访问正常、响应正常。
get传参访问,请求行?token=xxx&params=xx访问:请求访问正常、响应正常,token接收正常,params接收为""
post传参访问,请求行?token=xxx&params=xxx访问:请求访问正常、响应正常,token接收正常,params接收为""
post传参访问,请求体参数params(String或json)访问:请求访问正常、响应正常,token接收为null,params接收正常
post传参访问,请求体参数params(String或json)且请求行参数token=xxx访问:请求访问正常、响应正常,token接收为正常,params接收正常

正确使用小结:同时存在参数指定@RequestBody注解,和参数指定token时候,请求使用Get或Post均可,params必须在请求体传递而在请求行中传递?token=xx,才能获取到两参数。(推荐post方式发送带有请求体参数请求),这里值得留意的是?token=xx也可以不传递,不会报错。
注意:请求头使用默认即可,若设置Content-Type:application/json或Content-Type:text/html请求头会报错400,设置为其余请求头不会报错。

场景9:


后台:
@ResponseBody
public String testAny9(@RequestBody String params,@RequestParam String token)

请求:    
设置请求头Content-Type:application/json:
get无参数访问:肯定报错400,The request sent by the client was syntactically incorrect
post无参数访问:肯定报错400,The request sent by the client was syntactically incorrect
get传参访问,请求行?params=xxx访问:肯定报错400,The request sent by the client was syntactically incorrect
post传参访问,请求行?params=xxx访问:肯定报错400,The request sent by the client was syntactically incorrect
post传参访问,请求体参数params(String)访问:肯定报错400,The request sent by the client was syntactically incorrect
post传参访问,请求体参数params(json)访问:肯定报错400,The request sent by the client was syntactically incorrect
post传参访问,请求体参数params(json),且请求行参数token=xxx访问:肯定报错400,The request sent by the client was syntactically incorrect


设置请求头Content-Type:text 或 Content-Type:text/plain 或 默认:
get无参数访问:HTTP Status 400 - Required String parameter 'token' is not present
post无参数访问:HTTP Status 400 - Required String parameter 'token' is not present
get传参访问,请求行?token=xxx&params=xx访问:请求访问正常、响应正常,token接收正常,params接收为""
post传参访问,请求行?token=xxx&params=xxx访问:请求访问正常、响应正常,token接收正常,params接收为""
post传参访问,请求体参数params(String或json)访问:HTTP Status 400 - Required String parameter 'token' is not present
post传参访问,请求体参数params(String或json)且请求行参数token=xxx访问:请求访问正常、响应正常,token接收为正常,params接收正常

正确使用小结:同时存在参数指定@RequestBody注解,和参数指定token时候,请求使用Get或Post均可,params必须在请求体传递而在请求行中传递?token=xx,才能获取到两参数。(推荐post方式发送带有请求体参数请求)。
注意:请求头使用默认即可,若设置Content-Type:application/json或Content-Type:text/html请求头会报错400,设置为其余请求头不会报错。

 

3.补充说明响应返回场景

场景1:String + @ResponseBody

@ResponseBody
public String testAny9()

将以字符串String形式传给HttpServletResponse返回给前端

 

场景2:对象 + @ResponseBody

@ResponseBody
public User testAny9()

先自动转换成JSON形式传给HttpServletResponse返回给前端,json对象

场景3:String
public String testAny9()

寻找视图进行跳转

场景4:void + request+response
public String testAny9(HttpServletRequest request,HttpServletResponse response)

方法体中将使用request获取参数、response跳转或输出数据给前端

场景5:ModelAndView

public ModelAndView(){

    List<String> dataList = new ArrayList<String>(); 
    ModelAndView modelAndView = new ModelAndView();
    //将数据放入modelAndView对象
    modelAndView.addObject("dataList", dataList);
    //将返回的逻辑视图名称放入modelAndView对象
    modelAndView.setViewName("home");

    return modelAndView;
}

前端通过request.getAttribute("dataList")或${dataList}获取传递的数据

 

 

总结

400错误:参数格式不对,例如:{id:"dds"} 正解:{"id":"dds"};例如:后台封装的对象没有参数中的字段等等。

406错误:后台返回给前端的是对象而非字符串,需配置下json的转换。

 

4.中文乱码问题


后台代码:
@RequestMapping(value = "/demo1")
@ResponseBody
public String demo1(){
    return "我是中文测试";
}

就是这样一个简单的方法,直接返回字符串, 但是在界面上就发现中文乱码了,配置文件中<mvc:annotation-driven/>的形式开启的.

其实这个,也不涉及到Json字符串的乱码问题,因为没有使用到json的HttpMessageConverter.

 
解决方案一:

<mvc:annotation-driven conversion-service="conversionService">
    <mvc:message-converters register-defaults="true">
        <bean class="org.springframework.http.converter.StringHttpMessageConverter">
            <property name="defaultCharset" value="UTF-8"/>
            <property name="writeAcceptCharset" value="false"/>
        </bean>
    </mvc:message-converters>
</mvc:annotation-driven>

说明:替换其中的StringHttpMessageConverter,它是用来将String写到response中;

修改的两个属性说明:defaultCharset改为UTF-8(项目字符集)即可正确显示中文,因为默认是ISO-8859-1格式;

writeAcceptCharset修改为false,即可看到响应头清爽很多,节省资源;

 
解决方案二:

@RequestMapping(value = "/demo1",produces = {"text/plain;charset=utf-8","text/html;charset=utf-8"})
    @ResponseBody
    public String demo1(){
        return "我是中文测试";
    }

说明:指定响应的字符集为utf-8,就不会再用StringHttpMessageConverter的字符集了;

 
解决方案三:

@RequestMapping(value = "/demo1",produces = {"application/json;charset=utf-8"})
@ResponseBody
public String demo1() throws JsonProcessingException {
    return new ObjectMapper().writeValueAsString("我是中文测试");
}

说明:自己转换成JSON写回客户端,因为返回值类型还是写的String,所以仍然使用的是StringHttpMessageConverter,
但是return  “我是中文测试” ; 这个返回值不是JSON类型的,
客户端无法解析(即使指定了produces也没用);所以需要我们手动转换成JSON格式字符串,这样客户端接收到的就是JSON格式的响应了.

 
 
比较说明:
解决方案一、二返回给浏览器的类型都是text/plain或 text/html类型的文本,
而解决方案三返回的是application/json类型,但是返回结果也不是JSON,所以在我看来三种方式区别都不大吧。

 

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值