Java REST HTTP POST在curl中工作但在AngularJS

时间:2016-09-17 20:24:32

标签: java angularjs rest curl

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

为了验证我的逻辑是否来自服务器端,我使用了来自Google Apps的REST API客户端以及Unix Command curl。两种方法都返回HTTP状态200。

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

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

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

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;
    }
}

1 个答案:

答案 0 :(得分:1)

REST服务方法的声明包括:

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

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

为了实现此方法,您需要添加值为" application / json"的Content-type标头。 类似的东西:

SELECT FirstName, LastName, MONTHNAME(BirthDate) as BirthMonth
FROM   employees;