優(yōu)雅的接收參數(shù)

背景:在開發(fā)中特別是做保存邏輯的時候,后端需要接收很多業(yè)務(wù)參數(shù)。
html頁面(上傳的參數(shù)js對象和Java對象對應(yīng),js數(shù)組和Java集合對應(yīng),字段名字也要對應(yīng)):

<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<title>Insert title here</title>
<script src='jquery-2.1.4.min.js'></script>
</head>
<body>
    <button onclick='send()'>try</button>
    
    <script>
        function send(){
            var obj = new Object();
            
            // 基本信息
            var baseInfo = new Object();
            baseInfo.name = 'tianye';
            baseInfo.age = 18;
            baseInfo.sex = '男';
            obj.baseInfo = baseInfo;
            
            // 折扣優(yōu)惠
            var discount = new Array();
            var discountChild1 = new Object();
            discountChild1.discountIds = '1,2,3';
            discountChild1.productIds = '12,23,45,23';
            var discountChild2 = new Object();
            discountChild2.discountIds = '12,222,322';
            discountChild2.productIds = '122,233,455,2366';
            discount.push(discountChild1);
            discount.push(discountChild2);
            obj.discount = discount;
            
            // 樓棟別名
            var product = new Array();
            var productChild1 = new Object();
            var productChild2 = new Object();
            productChild1.id = 222;
            productChild1.name = 'sasfds';
            productChild1.sort = 2;
            productChild2.id = 333;
            productChild2.name = 'sfdsfdsfsadf';
            productChild2.sort = 1;
            product.push(productChild1,productChild2);
            obj.product = product;
            
            console.log(obj);
            console.log(JSON.stringify(obj));
            
            $.ajax({
                url: 'http://localhost:8080/ajax/test1',
                type: 'post',
                contentType: 'application/json;charset=utf-8',
                data: JSON.stringify(obj),
                success: function(data){
                    console.log(data);
                    console.log(JSON.stringify(data));
                }
            });
        }
    </script>
 
</body>
</html>

Controller處理器:

package com.example.demo.web;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import com.example.demo.domain.AjaxDTO;

@Controller
@RequestMapping("/ajax")
public class AjaxController {
    
    @RequestMapping("/test1")
    @ResponseBody
    public AjaxDTO test1(@RequestBody AjaxDTO AjaxDTO) {
        System.out.println(AjaxDTO.getBaseInfo());
        System.out.println(AjaxDTO.getDiscount());
        System.out.println(AjaxDTO.getProduct());
        return AjaxDTO;
    }

}

實體Bean

package com.example.demo.domain;
import java.util.List;
public class AjaxDTO {
    
    private BaseInfo baseInfo;
    
    private List<Discount> discount;
    
    private List<Product> product;

    // 忽略get,set方法
}


package com.example.demo.domain;
public class BaseInfo {
    
    private String name;
    
    private Integer age;
    
    private String sex;

    // 忽略get,set方法
}


package com.example.demo.domain;
public class Discount {
    
    private String discountIds;
    
    private String productIds;

    // 忽略get,set方法
}


package com.example.demo.domain;
public class Product {
    
    private Integer id;
    
    private String name;
    
    private Integer sort;

    // 忽略get,set方法
}


?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請聯(lián)系作者
【社區(qū)內(nèi)容提示】社區(qū)部分內(nèi)容疑似由AI輔助生成,瀏覽時請結(jié)合常識與多方信息審慎甄別。
平臺聲明:文章內(nèi)容(如有圖片或視頻亦包括在內(nèi))由作者上傳并發(fā)布,文章內(nèi)容僅代表作者本人觀點,簡書系信息發(fā)布平臺,僅提供信息存儲服務(wù)。

相關(guān)閱讀更多精彩內(nèi)容

友情鏈接更多精彩內(nèi)容