【问题标题】:415- Unsupported Media Type Error | angularjs + Spring3.2415- 不支持的媒体类型错误 | angularjs + Spring3.2
【发布时间】:2014-10-02 06:25:03
【问题描述】:

我在浏览器中收到 415 错误。我无法找到错误。你能帮忙吗?

  • loginController.js

$scope.user = {电子邮件:“管理员”,密码:“密码”};

$http.post('/expense-manager-api/login/authenticate', $scope.user, {
    headers: {"Content-Type": "application/json"}
}).success(function(login) {
    $scope.setError(login.status);
    $location.path("main");
}).error(function() {
    $scope.setError('Invalid user/password combination');
});
  • Lo​​ginController.java

@控制器

@RequestMapping("/login")

public class LoginController {

@RequestMapping(value = "/authenticate", method = RequestMethod.POST, consumes = MediaType.APPLICATION_JSON_VALUE, produces = MediaType.APPLICATION_JSON_VALUE)
public
@ResponseBody
LoginResponse login(@RequestBody LoginRequest loginRequest) {
    if (loginRequest.getEmail().equals("admin") && loginRequest.getPassword().equals("password")) {
        UUID uuid = UUID.randomUUID();

        return new LoginResponse(uuid.toString(), "OK");
    }

    return new LoginResponse(null, "Invalid user/password combination");
}

}

【问题讨论】:

  • 你能在 Firebug 或 Chrome 开发控制台上发布 HTTP 请求的样子吗?
  • @geo 我添加了照片。
  • Spring 似乎正在回归text/html。您确定日志中没有任何错误吗?
  • 是的。日志中没有错误。
  • 如果我调试服务器端代码。它甚至没有进入该方法的第一行。我的响应主体是“LoginRespone”,它是对象。怎么可能是text/html?

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


【解决方案1】:

Jackson mapper 解决了这个问题。

在你的 pom.xml 中添加 Jackson-mapper 的 maven 依赖

【讨论】:

    猜你喜欢
    • 2014-06-21
    • 1970-01-01
    • 2018-10-13
    • 2018-07-25
    • 2016-09-09
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2014-11-04
    相关资源
    最近更新 更多