2017-03-31 124 views
0

尝试发送从angularjs到Spring MVC的数据,但仍将继续得到415错误

app.controller('RegisterFormSubmitCtrl', ['$scope', '$http', '$location', function($scope, $http) { 

$scope.submit = function() { 

    var registerData = { 
     "email" : $scope.email, 
     "firstName" : $scope.firstName, 
     "lastName" : $scope.lastName, 
     "DoB": $scope.DoB = new Date(), 
     "password" : $scope.password 
    }; 

    console.log(registerData); 

    $http({ 
     method: 'POST', 
     url: "http://localhost:8080/home", 
     data: registerData, 
     headers: { 
      'Content-type': 'application/json, charset=UTF-8' 
     } 
    }).then(function successCallback(response) { 
    }, function errorCallback(response) { 

    }); 
}; }]); 

Spring MVC的控制器

@Consumes("text/html") 
    @RequestMapping(value = "/home", method = RequestMethod.POST) 
    public ResponseEntity<Void> afterRegister(@RequestBody RegisterUserRequest request){ 
     System.out.print("Register user: " + request.getFirstName()); 
     if(userManager.emailRegistered(request.getEmail())){ 
      return new ResponseEntity<>(HttpStatus.CONFLICT); 
     } 
     // checking with google datastore 
     else if(userManager.addUser(request.getEmail(), request.getPassword(), request.getFirstName(), request.getLastName(), 
       request.getDoB(), "User")) { 
      return new ResponseEntity<>(HttpStatus.CREATED); 
     } 
     return new ResponseEntity<>(HttpStatus.CONFLICT); 
    } 

RegisterUserRequest类

@Accessors(chain = true) 
@Data 
@NoArgsConstructor 
@AllArgsConstructor 
public class RegisterUserRequest { 
    @NotNull 
    private String email; 
    private String firstName; 
    private String lastName; 
    private Date DoB; 
    @NotNull 
    private String password; 
} 

错误:

Failed to load resource: the server responded with a status of 415 (Unsupported Media Type) 

我试过删除@RequestBody表示法来摆脱错误,但然后控制器只收到从request null。 还试图在映射添加produces='application/json',仍然有错误415

在依赖我添加后阅读下面JSON:

<dependency> 
    <groupId>org.codehaus.jackson</groupId> 
    <artifactId>jackson-mapper-asl</artifactId> 
    <version>1.9.13</version> 
</dependency> 
<dependency> 
    <groupId>com.fasterxml.jackson.core</groupId> 
    <artifactId>jackson-databind</artifactId> 
    <version>2.8.6</version> 
</dependency> 
+0

你可以发布你的'RegisterUserRequest'类吗? – hrdkisback

+0

我已经添加了'RegisterUserReques'类代码 – Celeste

+0

添加这种依赖关系' \t \t \t org.codehaus.jackson \t \t \t 杰克逊核心 - 翔升 \t \t \t 1.9.13 \t \t'检查。 – hrdkisback

回答

0

你的控制器afterRegister()方法现正接受text/html内容即@Consumes("text/html"),所以它更改为@Consumes("application/json")

+0

我试过了,但仍然收到错误415 – Celeste

相关问题