Ajax前后端傳遞數據,@RequestBody @RequestParam 與@PathVariable


本文寫的比較簡略,我也是萌新,見諒!

1,@RequestBody   用來接受前端傳給后端的JSON字符串里的數據

       此時前端只能是POST方式提交,

例子:增加屬性值功能:

前端

 

 

Bean就是JSON數據bean: {id:0,name:'',category:{id:0}},//屬性對象

 

后端:后端可以用一個對象接收前端傳過來的東西

@PostMapping("/properties")

public Object add(@RequestBody Property bean) throws Exception {

    propertyService.add(bean);

    return bean;

}

 

此時有以下要求:

 

 原文:https://blog.csdn.net/f45056231p/article/details/89463704

 

 

 

 

 

2. @RequestParam注解使用

原文:https://blog.csdn.net/sswqzx/article/details/84195043

假如url里面有name=?

前端:

list:function(start){
    var url =  "categories/"+cid+"/"+this.uri+"?start="+start;//@RequestParam收藏的網頁
   
axios.get(url).then(function(response) {
        vue.pagination = response.data;
        vue.beans = response.data.content   ;
    });
},

后端:

@GetMapping("/categories/{cid}/products")//listProduct.html  35行,具體看收藏的網頁@RequestParam
public Page4Navigator<Product> list(@PathVariable("cid") int cid, @RequestParam(value = "start", defaultValue = "0") int start,@RequestParam(value = "size", defaultValue = "5") int size) throws Exception {
    start = start<0?0:start;
    Page4Navigator<Product> page =productService.list(cid, start, size,5 );

    return page;
}

 

 

 

 

3. @PathVariable注解使用

原文:https://blog.csdn.net/sswqzx/article/details/84194979

接收請求路徑中占位符的值

@GetMapping("/products/{id}")
public Product get(@PathVariable("id") int id) throws Exception {
    Product bean=productService.get(id);
    return bean;
}


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM