繁体   English   中英

Java REST HTTP POST 在 curl 中有效,但在 AngularJS 中无效

[英]Java REST HTTP POST works in curl but not in AngularJS

几天来,我一直试图让 HTTP POST从 AngularJS 工作。 我现在真的被困住了,我真的很感激任何见解! 对于服务器端,我使用 JAX-RS 和 Jersey。 我传递的对象是application/json类型,并且我创建了一个 Java 类Student (有关更多详细信息,请参阅下面的代码)。 我的目标是将一个Student对象从 AngularJS 发布到服务器。

为了从服务器端验证我的逻辑是否合理,我使用了来自 Google Apps 的 REST API 客户端和 Unix 命令 curl。 这两种方法都返回 HTTP 状态 200。

然而,当我从 AngularJS 尝试同样的事情时,HTTP POST出现错误, Response对象没有数据且状态为 -1。 发布请求似乎已经到达服务器,因为它输出 HTTP 状态 204。我不明白为什么使用 AngularJS 我得到 204 的状态代码但使用 curl/REST API 客户端我得到 200 的状态并且它正常工作.

以下是我的代码的相关部分:

控制器定义和包含 HTTP POST调用的函数addAName()

app.controller('PostingController', function($scope, $http) {
$scope.postMessage='This message is a default message and will change after I click the button and addAName() is called.';
$scope.name='CODER';
$scope.postData = 'Not Available';

$scope.addAName = function(){
    var data = {"studentId": 2, "firstName": "Jason", "lastName": "Lee", "specialization": "Student"};

    $http.post("http:/localhost:8080/MathAssignment/studentservice/users", data).then( function successCallback(response) {
        $scope.postMessage = "POSTING WORKS!";
        $scope.postStatus = response.status;
        $scope.postData = response.data;
     }, function errorCallback( response ) {
        $scope.postMessage = "POSTING STILL DOES NOT WORK";
        $scope.postStatus = response.status;
        $scope.postData = response.data;
    });
};
});   

接受 HTTP POST调用的服务器端代码:

   @POST
   @Path("/users")
   @Produces(MediaType.APPLICATION_JSON)
   @Consumes(MediaType.APPLICATION_JSON)
   public Response updateUser(Student inputJsonObj) throws IOException {
      int id = (int) inputJsonObj.getStudentId();
      String name = (String) inputJsonObj.getFirstName() ;
      String profession = (String) inputJsonObj.getSpecialization();
      System.err.println("JSON is" + inputJsonObj.toString());
      System.err.println("Name is" + name);
      return Response.status(200).entity(inputJsonObj).build();
   }

Student类定义:

public class Student {

    private int studentId;
    private String firstName;
    private String lastName;
    private String specialization;

    public int getStudentId() {
        return studentId;
    }
    public void setStudentId(int studentId) {
        this.studentId = studentId;
    }
    public String getFirstName() {
        return firstName;
    }
    public void setFirstName(String firstName) {
        this.firstName = firstName;
    }
    public String getLastName() {
        return lastName;
    }
    public void setLastName(String lastName) {
        this.lastName = lastName;
    }
    public String getSpecialization() {
        return specialization;
    }
    public void setSpecialization(String specialization) {
        this.specialization = specialization;
    }
}

REST 服务方法的声明包括:

@Consumes(MediaType.APPLICATION_JSON)

这表明该方法使用“application/json”的内容类型。

为了达到此方法,您需要添加一个值为“application/json”的 Content-type 标头。 就像是:

   $http.post("/foo/bar", requestData, {
        headers: { 'Content-type': 'application/json'}
    }).success(function(responseData) {
        //do stuff with response
    });

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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