简体   繁体   中英

Spring Controller request parameter as POJO coming from jQuery get()

Updated:

There might be some who are looking for a way to pass a JSON object as ajax parameter then convert into a POJO in a spring controller. Others might get a 400 Bad Request error if they use Request Method GET. Please use the type POST in both ajax and in spring controller.

JQuery code

var PaxTypeFare = function(paxType, baseFare, totalFeesAndTaxes, totalAmount) {   
    this.paxType = paxType;   
    this.baseFare = baseFare;   
    this.totalFeesAndTaxes = totalFeesAndTaxes;   
    this.totalAmount = totalAmount;   
}   

var SpaceShipTripInfo = function(from, to, fromSchedule, toSchedule){   
    this.from = from;   
    this.to = to;   
    this.fromSchedule = fromSchedule;   
    this.toSchedule = toSchedule;   
};   

...   

var tInfos = new Array();   
tInfos.push(new SpaceShipTripInfo("EARTH", "MOON", "2015-12-21T04:30:00", "2015-12-21T06:50:00"));   
tInfos.push(new SpaceShipTripInfo("MOON", "MARS", "2015-12-21T03:30:00", "2015-12-21T011:10:00"));   
tInfos.push(new SpaceShipTripInfo("VENUS", "KEPLER", "2015-12-21T01:30:00", "2015-12-21T22:30:00"));   
tInfos.push(new SpaceShipTripInfo("EARTH", "SUN", "2015-12-20T02:30:00", "2015-12-29T15:10:00"));   

var adultFares = new PaxTypeFare("ADT", 1000, 300, 1300);   
var childFares = new PaxTypeFare("CHD", 750, 250, 1000);   
var infantFares = null;   

var data = {   
    tripInfos: tInfos,   
    adultFares: adultFares,   
    childFares: childFares,   
    infantFares: infantFares,   
    adultCount: 1,   
    childCount: 1,   
    infantCount: 0   
};   

var url = "/getSelectedTripPrices";   
$.ajax({   
    url: url,   
    data: JSON.stringify(data),   
    contentType: "application/json",   
    type: "POST",  
    success: function (strHtml) {   
        //...   
    } 
});   

JSON data

{   
    "tripInfos":[   
        {   
            "from":"EARTH",   
            "to":"MOON",   
            "fromSchedule":"2015-12-21T04:30:00",   
            "toSchedule":"2015-12-21T06:50:00"   
        },   
        {   
            "from":"MOON",   
            "to":"MARS",   
            "fromSchedule":"2015-12-21T03:30:00",   
            "toSchedule":"2015-12-21T011:10:00"   
        },   
        {   
            "from":"VENUS",   
            "to":"KEPLER",   
            "fromSchedule":"2015-12-21T01:30:00",   
            "toSchedule":"2015-12-21T22:30:00"   
        },   
        {   
            "from":"EARTH",   
            "to":"SUN",   
            "fromSchedule":"2015-12-20T02:30:00",   
            "toSchedule":"2015-12-29T15:10:00"   
        }   
    ],   
    "adultFares":{   
        "paxType":"ADT",   
        "baseFare":"1000",   
        "totalFeesAndTaxes":"300",   
        "totalAmount":"1300.00"   
    },   
    "childFares":{   
        "paxType":"CHD",   
        "baseFare":"750",   
        "totalFeesAndTaxes":"250",   
        "totalAmount":"1000.00"   
    },   
    "infantFares": null,   
    "adultCount":"1",   
    "childCount":"1",   
    "infantCount":"0"     
}   

ItineraryPrice Class

public Class ItineraryPrice {   
    private List<ItineraryPrice.SpaceShipTripInfo> tripInfos;  
    private ItineraryPrice.PaxTypeFare adultFares;  
    private ItineraryPrice.PaxTypeFare childFares;  
    private ItineraryPrice.PaxTypeFare infantFares;  

    private int adultCount;  
    private int childCount;  
    private int infantCount;  

    //Getters and Setters here  

    public static class SpaceShipTripInfo {  
        private String from;  
        private String to;  
        private String fromSchedule;  
        private String toSchedule  
        //Getters and Setters here  
    }  

    public static class PaxTypeFare {  
        private String paxType;  
        private Double baseFare;  
        private Double totalFeesAndTaxes;  
        private Double totalAmount;  
        //Getters and Setters here  
    }  
}  

Spring Controller

@RequestMapping(value = "/getSelectedTripPrices", method = RequestMethod.POST, consumes = "application/json")   
@ResponseBody   
public ModelAndView getSelectedTripPrices(HttpSession session,   
 @RequestBody ItineraryPrice itineraryPrice,   
 BindingResult bindingResult) {   

    if(null != itineraryPrice.getAdultFares()){  
        System.out.println("[Type-code]: "              + itineraryPrice.getAdultFares().getPaxType());  
        System.out.println("[Type-baseFare]: "          + itineraryPrice.getAdultFares().getBaseFare());  
        System.out.println("[Type-totalFeesAndTaxes]: " + itineraryPrice.getAdultFares().getTotalFeesAndTaxes());  
        System.out.println("[Type-totalAmount]: "       + itineraryPrice.getAdultFares().getTotalAmount());  
    }  
    else  
        System.out.println("ADULT-FARES-NULL");  

    if(null != itineraryPrice.getChildFares()){  
        System.out.println("[Type-code]: "              + itineraryPrice.getChildFares().getPaxType());  
        System.out.println("[Type-baseFare]: "          + itineraryPrice.getChildFares().getBaseFare());  
        System.out.println("[Type-totalFeesAndTaxes]: " + itineraryPrice.getChildFares().getTotalFeesAndTaxes());  
        System.out.println("[Type-totalAmount]: "       + itineraryPrice.getChildFares().getTotalAmount());  
    }  
    else  
        System.out.println("CHILD-FARES-NULL");  

    if(null != itineraryPrice.getInfantFares()){  
        System.out.println("[Type-code]: "              + itineraryPrice.getInfantFares().getPaxType());  
        System.out.println("[Type-baseFare]: "          + itineraryPrice.getInfantFares().getBaseFare());  
        System.out.println("[Type-totalFeesAndTaxes]: " + itineraryPrice.getInfantFares().getTotalFeesAndTaxes());  
        System.out.println("[Type-totalAmount]: "       + itineraryPrice.getInfantFares().getTotalAmount());  
    }  
    else  
        System.out.println("INFANT-FARES-NULL");  
}  

you need to transform these data to JSON objects: to send info to the RequestParam you can use this:

$.ajax({
            url: './controller/find.htm',
            data: {
                adultCount: 1,
                childCount: 2,
                childCount: 3
            },
            success: function (data) {
                console.log('response=', data);
            }
        });

the ModelAttribute is use to macth form variables, you need to use RequestBody and you need to create a Json object in jquery and add that in the data section in the code.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM