Spring MVC + AngularJS 415 Unsupported Media Type

The question is very simple and I know that many other questions have been answered, but none worked for me. Using Spring MVC and AngularJS I get UnSupported Media Type for AngularJS 415!

I tried to set the angular header to application / json
I tried with @Consumes

server side annotation
I tried with consumes ="application/json"


I am tired of consumes ="application/application/json;charset=UTF-8'


I tried with consumes ={"application/json","application/xml"}


I also tried to set the property.

I tried to explicitly set the listener content type on the client to exactly match the one on the server, but NOTHING WORKS! here are related questions that haven't helped anyone! 1
2 3

Here is my controller

/**
 * Created by adelin.ghanayem@gmail.com
 */
@Controller
@RequestMapping(value = "/administration/places")
public class PlacesController {

    private PlacesService service;

    @Autowired
    public PlacesController(PlacesService service) {

        this.service = service;

    }


    @RequestMapping(method = RequestMethod.POST,consumes = {"application/json;charset=UTF-8"})
    public String newPlace(@RequestBody Place places) {

        String id = service.addNewPlace(places);

        return "/administration/places/" + id;

    }


    @RequestMapping(value = "/{id}")
    public Place getById(@PathVariable String id) {

        return new Place();

    }


}

      

And my AngularJS controller

function NewPlacesController(scope, http) {


    scope.place = {};

    scope.add = function () {

        http.post(URLS.addNewPlace, scope.place,{'Content-Type': 'application/json'}).success(function (value) {

            console.log("got it !");

        }).error(function (value) {
            console.log("CUR!");
        });

    }
}

NewPlacesController['$inject'] = ['$scope', '$http'];

      

+3


source to share


3 answers


Try:

@RequestMapping(method = RequestMethod.POST,consumes = {"application/json;charset=UTF-8"}, produces={"application/json;charset=UTF-8"})
public String newPlace(@RequestBody Place places) {

    String id = service.addNewPlace(places);

    return "/administration/places/" + id;

}

      



And make sure you include the Jackson Databind library.

+2


source


Look at content type in angular code. You may need to explicitly install it. Also take a look at the Spring MVC implementation. You can use the "REST Console" app in Chrome to test your api to make sure it works the way you think it works.



+1


source


add to your pom:

    <dependency>
        <groupId>org.codehaus.jackson</groupId>
        <artifactId>jackson-mapper-asl</artifactId>
        <version>1.9.13</version>
    </dependency>

      

+1


source







All Articles