【问题标题】:AngularJs Spring MVC $http.post Error 415 (Unsupported Media Type)AngularJs Spring MVC $http.post 错误 415(不支持的媒体类型)
【发布时间】:2017-03-31 21:03:36
【问题描述】:

尝试将数据从 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>

【问题讨论】:

  • 你能发布你的RegisterUserRequest课程吗?
  • 我添加了RegisterUserReques类代码
  • 添加这个依赖&lt;dependency&gt; &lt;groupId&gt;org.codehaus.jackson&lt;/groupId&gt; &lt;artifactId&gt;jackson-core-asl&lt;/artifactId&gt; &lt;version&gt;1.9.13&lt;/version&gt; &lt;/dependency&gt;并检查。

标签: angularjs json spring-mvc http-post http-status-code-415


【解决方案1】:

您的控制器afterRegister() 方法现在接受text/html 内容,即@Consumes("text/html"),因此将其更改为@Consumes("application/json")

【讨论】:

    【解决方案2】:

    尝试检查您的 spring mvc 配置是否注册了 json 转换器 by my another answer

    【讨论】:

      猜你喜欢
      • 2014-11-04
      • 1970-01-01
      • 2013-08-08
      • 2015-02-25
      • 2014-10-02
      • 2014-06-21
      • 2023-03-04
      • 2017-05-20
      • 2014-02-16
      相关资源
      最近更新 更多