如何向Spring发送几个String?

时间:2017-03-10 04:36:21

标签: ajax spring

如何将多个弦发送到弹簧?如果我将几个字符串转换为数组,我可以发送,但如果我尝试发送几个字符串它不起作用。发送数组到Spring并不是一个好主意,因为在Spring中我必须使用更多的代码(split,new String等)。

function sendCustomerInfo() {

    var nameCustomerForSend = $("#NickNameCustomerForSend").val();
    var phoneCustomerForSend = $("#PhoneCustomerForSend").val();
    var emailCustomerForSend = $("#EmailCustomerForSend").val();
    var addressCustomerForSend = $("#descriptionCustomerForSend").val();

    console.log("Name: " + nameCustomerForSend + " Address: " + addressCustomerForSend + " Phone: " + phoneCustomerForSend
        + " Email: " + emailCustomerForSend);


    $.ajax({   
        headers: {
            "Accept": "application/json",
            "Content-Type": "application/json"
        },
        type: "POST", //это типа method
        data: {NickName:nameCustomerForSend, Phone:phoneCustomerForSend, Email:emailCustomerForSend,
            description:addressCustomerForSend},
        url: '/showAll/customerInfo',
        success: function (msg) {  
            window.location.href = "/showAll"
        }
    });
}

我收到:Failed to load resource: the server responded with a status of 401 () 在春天,我试着收到:

 @RequestMapping(value = "/showAll/customerInfo", method = { RequestMethod.POST}
        , produces = MediaType.APPLICATION_JSON_VALUE)
public ModelAndView showAllForCustomer(@RequestParam(value = "NickName")String name,
                                       @RequestParam(value = "Phone")String phone,
                                       @RequestParam(value = "description")String addressDelivery,
                                       @RequestParam(value = "Email")String email) {
    System.out.println("Name: " + name + " Phone: " + phone + " Address: " + addressDelivery + " Email: " + email);
    ModelAndView modelAndView = new ModelAndView();
    return modelAndView;

}

1 个答案:

答案 0 :(得分:0)

@RequestBody与post和application / json类型数据一起使用 创建具有showAllForCustomer中指定的所有参数的DTO 像

public ModelAndView showAllForCustomer(@RequestBody CustomerDto dto){
    ............
}