Search code examples
angularjsweb-servicesspring-mvchttp-posthttp-status-code-415

415- Unsupported Media Type Error | angularjs + Spring3.2


I am getting 415 error in browser. I am not able to find the mistake. Could you please help.

  • loginController.js

$scope.user = {email: "admin", password: "password"};

$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');
});
  • LoginController.java

@Controller

@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");
}

}

Network Call

request and response headers


Solution

  • Jackson mapper fixed the problem.

    add maven dependency of Jackson-mapper in your pom.xml