【问题标题】:Error when calling springRest webservice by an angularJs ClientangularJs客户端调用spring Rest web服务时出错
【发布时间】:2015-04-29 22:34:19
【问题描述】:

我正在尝试调用 Spring REST 实现的 Web 服务,但服务器以 400 http code bad request 响应。我将公开一些有关该请求的信息,希望有人可以帮助我。

这是我的控制器方法:

@RequestMapping(value = "/addpost", method = RequestMethod.POST,headers = "Accept=application/json")
public @ResponseBody
String addpost(@RequestBody PostDto post, @RequestBody UserDto user) {

    postservice.addPost(post, user);
    return "post inserted";

}

我调用 Web 服务的方式: $scope.addPost = function() {

$scope.addPost = function() {


    $http({
        method : 'POST',
        url : 'http://localhost:8080/wall/addpost',
        data : ({
            post : $scope.postt,
            user : $scope.userr
        }),

    }).success(function(data, status, headers, config) {
        // $scope.persons.push(data);
        alert("Success");
        $scope.user = "";
        $scope.post = "";
    }).error(function(data, status, headers, config) {
        alert("erreur");
        // called asynchronously if an error occurs
        // or server returns response with an error status.
    });

};

    };

两个对象“userr”和“postt”:

$scope.userr = {
        userId : 15,
        firstName : "foulen",
        lastName: "ben foulen"};

    $scope.postt = {
            Id : "18",
            datePost : null,
            note: "Mon message"};

服务器的响应:

Etat HTTP 400 - Required PostDto parameter 'post' is not present

这些是请求和响应的标头:

  Remote Address:127.0.0.1:8080
Request URL:http://localhost:8080/wall/addpost
Request Method:POST
Status Code:400 Mauvaise Requête
Request Headersview source
Accept:application/json, text/plain, */*
Accept-Encoding:gzip, deflate
Accept-Language:fr-FR,fr;q=0.8,en-US;q=0.6,en;q=0.4
Connection:keep-alive
Content-Length:125
Content-Type:application/json;charset=UTF-8
Host:localhost:8080
Origin:http://localhost:8080
Referer:http://localhost:8080/wall/view/test.html
User-Agent:Mozilla/5.0 (Windows NT 6.1; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/40.0.2214.115 Safari/537.36
Request Payloadview source
{post: {idpost: 18, datePost: null, note: "Mon message"},…}
post: {idpost: 18, datePost: null, note: "Mon message"}
datePost: null
idpost: 18
note: "Mon message"
user: {userId: 15, firstName: "foulen", lastName: "ben foulen"}
firstName: "foulen"
lastName: "ben foulen"
userId: 15
Response Headersview source
Connection:close
Content-Length:983
Content-Type:text/html;charset=utf-8
Date:Fri, 27 Feb 2015 11:16:49 GMT
Server:Apache-Coyote/1.1

【问题讨论】:

    标签: angularjs spring web-services


    【解决方案1】:

    您不能使用两个@RequestBody,因为它只能绑定到单个对象(主体只能使用一次)。更简单的方法是引入包装对象并更改您的签名。

    【讨论】:

    【解决方案2】:

    无论你传递的参数是传递给控制器​​的吗?我的意思是你在角度控制器中提到的网址 'http://localhost:8080/wall/addpost'。

    【讨论】:

    • NO 我只是通过 url "'localhost:8080/wall/addpost" 调用服务我没有在 url 中传递任何参数
    • ok...试试@RequestParam("post") String post , @RequestParam("user") String user 。
    • 但我需要它们作为 Userdto 和 Postdto 对象
    • 查看更新后的问题我试过了,但同样的问题仍然存在 所需的 PostDto 参数“post”不存在
    【解决方案3】:

    您的 Angular 代码看起来不错。我认为问题出在服务器代码方面。请求参数@RequestParam PostDto post 应该有一个值来指示其名称,例如:@RequestParam PostDto("post") post。根据spring doc,这个值默认是""。

    为了完成,也许你应该使用更正常的方式发送Angular的请求。我的意思是使用 json 对象作为参数,而不是 html 表单。因为你已经有了用户和帖子的模型。

    希望帮助。

    编辑: 你可以修改你的角度代码,通过json对象发送参数。

    $http({
            method : 'POST',
            url : 'http://localhost:8080/wall/addpost',
            data : ({
                post : $scope.postt,
                user : $scope.userr
            })
        }).success(function(data, status, headers, config) {
            // $scope.persons.push(data);
            alert("Success");
            $scope.user = "";
            $scope.post = "";
        }).error(function(data, status, headers, config) {
            alert("erreur");
            // called asynchronously if an error occurs
            // or server returns response with an error status.
        });
    
    };
    

    并删除您的 java 代码中的标头:

    @RequestMapping(value = "/addpost", method = RequestMethod.POST)
    public @ResponseBody
    String addpost(@RequestParam(value="post") PostDto post, @RequestParam(value="user") UserDto user) {
    
        postservice.addPost(post, user);
        return "post inserted";
    
    }
    

    编辑: 型号:

    $scope.postInfo= {
        userId : 15,
        firstName : "foulen",
        lastName: "ben foulen",
        postId : "18",
        datePost : null,
        postnote: "Mon message"
     };
    

    AJAX:

    $http({
        method : 'POST',
        url : 'http://localhost:8080/wall/addpost',
        data : ({
            post : $scope.postInfo
        }).// your other codes
    })
    

    JAVA:

    @RequestMapping(value = "/addpost", method = RequestMethod.POST)
    public @ResponseBody String addpost(@RequestBody PostInfoDto post) {
        // codes...
    }
    

    【讨论】:

    • 即使我重命名参数后,同样的问题仍然存在
    • @housseminfo 我已经更新了我的答案,使用 json 对象作为请求参数。
    • 我得到了服务器的相同响应“Etat HTTP 400 - 所需的 PostDto 参数‘post’不存在”
    • @housseminfo,错了,你能在 Chrome 或 Firefox 的控制台中查看你的请求标头吗?检查参数是否发送到服务器。
    • @千月我已经更新了我的问题
    猜你喜欢
    • 1970-01-01
    • 2014-09-04
    • 2013-04-23
    • 2019-01-29
    • 1970-01-01
    • 1970-01-01
    • 2012-03-02
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多