POST请求未映射到Spring MVC Controller

时间:2019-02-21 10:10:26

标签: angularjs spring spring-mvc angular-promise

我正在从Angularjs $ http service发送POST请求。当我的承诺如下所示并且正确映射到Spring Controller时,它工作正常

service.js

reassignKsaPendingLeads : function(data,username)
                                    {
    var promise = $http({
        url : "reassignPendingLeads.htm",
        method : "POST",
        data : mydata
        })
        .success(function(data, status, header, config, statusText)
        {
        }       
}

Spring Controller

@RequestMapping({ "reassignPendingLeads.htm" })
    public @ResponseBody String updateAccessStatus(@RequestBody List<KsaLead> ksaLeads) 
    {
        log.info("Inside updateAccessStatus");
        return "true";
    }        

我从service.js传递的json对象已正确映射到@RequestBody List ksaLeads。
由于json对象包含与KsaLead的bean属性相同的键。

但是现在我要传递除json正文(数据)之外的另一个参数用户名
然后我得到Spring Controller不支持的Request方法'POST'。

新service.js

reassignKsaPendingLeads : function(data,username)
                                    {
                                        var mydata = JSON.stringify(data) + '&username=' + username;    
                                        var promise = $http({
                                            url : "reassignPendingLeads.htm",
                                            method : "POST",
                                            data : mydata
                                        })
                                        .success(function(data, status, header, config, statusText)
                                        {
                                        }

新版Spring Controller

@RequestMapping({ "reassignPendingLeads.htm" })
    public @ResponseBody String updateAccessStatus(@RequestBody List<KsaLead> ksaLeads,@RequestParam String username) 
    {
        log.info("Inside updateAccessStatus");
        return "true";
    }

任何人都可以指导我如何从angularJs传递数据,以便将其正确映射到Spring Controller

1 个答案:

答案 0 :(得分:0)

我假设您想将用户名作为请求参数传递。为此,$http可以有一个params输入字段:

function(data,username) {
    var promise = $http({
        url: "reassignPendingLeads.html",
        method: "POST",
        data: data,
        params: { username: username }
    })
// continue with your promise here
}

如果要在请求正文中传递它,我们必须执行以下步骤:

我们必须将username添加到mydata对象。

// we assume that data is not a primitive type
function(data,username) {
    var mydata;
    // check if data is defined
    if (data) {
        // make a deep copy first, since we don't want to modify the input
        mydata = angular.copy(data);
    } else {
        // if the data is undefined, create e new object
        mydata = {};
    }
    // add the username to mydata
    mydata.username = username;
    var promise = $http({
        url: "reassignPendingLeads.html",
        method: "POST",
        data: mydata
    })
// continue with your promise here
}

在后端,我们必须确保我们可以接受此类数据。为此,建议创建一个数据类,其中包含用户名以及可能从前端显示在data对象上的其他任何字段。 大概是这样的:

public class MyDataClass {
    private String username;
    // other fields

    // no args constructor
    public MyDataClass() {}

    // all args constructor
    public MyDataClass(String username) {
        this.username = username;
    }

   // getter and setters 
}

现在,我们修改控制器以接受它:

@RequestMapping(value = "reassignPendingLeads.htm", method = RequestMethod.POST)
    public @ResponseBody String updateAccessStatus(@RequestBody MyDataClass data) 
    {
        log.info(data.getUsername());
        return "true";
    }