zoukankan      html  css  js  c++  java
  • 浅谈:@RequestBody,@ResponseBody 和 @RequestParam

    1. @RequestBody

    @RequestBody:主要用来接收前端传递给后端的json字符串中的数据的(请求体中的数据)

    GET方式无请求体,所以使用@RequestBody接收数据时,前端不能使用GET方式提交数据,而是用POST方式进行提交。

    使用注解@RequestBody可以将body里面所有的json数据传到后端,后端再进行解析。

    使用场景:前端请求为POST,数据格式为:application/json时,使用@RequestBody

    例:

    axios.post("/news/newsAdd.do",this.news).then((response)=>{
                        //隐藏新增窗口
                        this.dialogFormVisible = false;
                        //判断后台返回的flag值,true表示添加操作成功,false为添加操作失败
                        if(response.data.flag){
                            this.$message({message: response.data.message, type: 'success'});
                            this.init();
                        }else{
                            this.$message.error(response.data.message);
                        }
                  })
    
        @RequestMapping("/newsAdd")
        @ResponseBody
        public ResponseResult newsAdd(@RequestBody News news)
        {
            //创建返回消息字符串
            String message;
            try {
                //调用方法新增
                newsService.newsAdd(news);
                message="新增成功!";
                //返回结果对象
                return new ResponseResult(true,message);
    
            }catch (Exception e) {
    
                e.printStackTrace();
                message="新增失败!";
                //返回结果对象
                return new ResponseResult(false,message);
            }
    
        }
    

    2. @ResponseBody

    @ResponseBody:java对象转为json格式的数据,通常用来返回JSON数据或者是XML数据。作用在方法上,通过Response响应给客户端。

    前台请求过去的json字符串,在使用@RequestBody注解后 被HttpMessageConverter,转换成对应的java对象,然后我们再用@ResponseBody注解,将java对象 以特定的格式(通常都是json)相应给浏览器。

    深入了解@ResponseBody注解原理:

    @RequestBody注解原理

    3. @RequestParam

    @RequestParam: 接受简单类型的属性,也可以接受对象类型。接收的参数是来自requestHeader中,即请求头。通常用于GET请求,@RequestParam也可用于POST、DELETE等请求,但是如果前端使用的是json字符串来传值的话,发送请求会报错,后台接收不到值,为NULL。

    配置参数:

    • required 表示是否必须,默认为 true,必须。
    • defaultValue 可设置请求参数的默认值。
    • value 为接收url的参数名(相当于key值)。

    语法:@RequestParam(value=”参数名”,required=”true/false”,defaultValue=””)

    使用场景: 发送GET请求,url中的?后面添加参数时,简单类型属性。

    注意:一个请求,只能有一个RequestBody; 一个请求,可以有多个RequestParam

    例1:

                     //GET请求,传ID
                       axios.get("/news/DeleteById.do?id="+row.id).then((res)=>{
                        if (res.data.flag)
                        {
                            this.$message({
                                message: res.data.message,
                                type: 'success'
                            });
                            this.init();
                        }else {
                            this.$message({
                                message: res.data.message,
                                type: 'error'
                            });
                        }
                    })
    
    @RequestMapping("/DeleteById")
        @ResponseBody
        public ResponseResult DeleteById(@RequestParam Integer id)
        {
            String message;
            try {
                //调用方法删除
                newsService.DeleteById(id);
                message="删除成功!";
                //返回结果对象
                return new ResponseResult(true, message);
    
            }catch (Exception e) {
    
                e.printStackTrace();
                message="删除失败!";
                //返回结果对象
                return new ResponseResult(false,message);
    
            }
        }
    

    列2:

    init(){
                    var params={pageSize:this.pageSize,
                                currentPage:this.currentPage,
                                newsTitle:this.newsTitle,
                                newsContent:this.newsContent,
                    };
                    axios.get("/news/findPage.do",{params:params}).then((response)=>{
    
                        this.tableData=response.data.data.newsList;
                        this.pageSize=response.data.data.pageInfo.pageSize;
                        this.total=response.data.data.pageInfo.total;
                    });
                }
    
    /**
         * 根据条件分页查询
         * @param pageSize
         * @param currentPage
         * @param newsTitle
         * @param newsContent
         * @return
         */
    @RequestMapping("/findPage")
        @ResponseBody
        public ResponseResult findPage(@RequestParam Integer pageSize,
                                       @RequestParam Integer currentPage,
                                       @RequestParam String newsTitle,
                                       @RequestParam String newsContent)
        {
            略......
        }
    
  • 相关阅读:
    关于接口测试的一些个人总结
    PMP学习考试回顾与心得
    RHCE第2课
    RHCE第1课
    kafka中对于zookeeper的理解和leader选举过程
    基于python的Selenium使用
    安装python第三方包的几种方式
    Vi命令下几种容易混淆的插入方式
    python单元测试框架unittest
    关于SQL子查询的一个问题
  • 原文地址:https://www.cnblogs.com/papercy/p/14810558.html
Copyright © 2011-2022 走看看