Error 415. Service REST with Spring - AngularJs

I have a problem with my service. I am sending a petition $http.get from angular to my Spring controller, but i have the next error : The server refused this request because the requested party is in a format not supported by the requested resource for the requested method.

Controller:

@Controller
public class PersonController {

    @Autowired
    private PersonService personService;


    @RequestMapping(value = "/person", consumes = {"application/json;charset=UTF-8"}, produces={"application/json;charset=UTF-8"},  method = RequestMethod.GET)
    public @ResponseBody List<Person> listPersons_() {
        return this.personService.listPersons();
    }
...

Angular Service:

    function get($scope, $http) {
        $http.get('person').
            success(function(data) {
                $scope.greeting = data;
            });
    }
...

Screenshot: Console

Header: Headers

Upvotes: 0

Views: 116

Answers (3)

Well ... after removing the atributes (consume and produces), I would get an error 406, and I felt I needed to add the Jackson-databin library. and it worked.

Controller:

@Controller
public class PersonController {

    @Autowired
    private PersonService personService;


    @RequestMapping(value = "/person", method = RequestMethod.GET)
    public @ResponseBody List<Person> listPersons_() {
        return this.personService.listPersons();
    }
...

Dependency:

<dependency>
    <groupId>com.fasterxml.jackson.core</groupId>
    <artifactId>jackson-databind</artifactId>
    <version>2.3.0</version>
</dependency>

Thanks a lot.

Upvotes: 0

cst1992
cst1992

Reputation: 3941

I don't think you need those produces/consumes headers. I had developed a very similar application which had the AngularJS service the same as yours, but it was relatively very simple on the Spring end:

// List All Employees
@RequestMapping(value = "/employee/all", method = RequestMethod.GET)
public AllEmployeesResponse listAllEmployees() {

    return empService.listAll();
}

All I had was a AllEmployeesResponse object containing a list of Employee objects.

The json took care of itself. If yours is not, check if you have implemented Serializable in your @Entity class.

Also, mine had a @RestController instead of a plain @Controller.

Upvotes: 1

Daniel Lavoie
Daniel Lavoie

Reputation: 1922

Spring MVC is not very verbose by default when it comes to HTTP errors. What you can do is raise the log level of the package org.springframework.web to DEBUG so you can get a better explanation of the 415 error.

Best of luck !

Daniel

Upvotes: 1

Related Questions