简体   繁体   English

Spring MVC + AngularJS 415不支持的媒体类型

[英]Spring MVC + AngularJS 415 Unsupported Media Type

The question is very simple and I know it was answered in many other question but none worked for me. 这个问题非常简单,我知道它在很多其他问题中得到了回答,但没有一个对我有用。 Using Spring MVC and AngularJS I am getting AngularJS 415 Unsupported Media Type ! 使用Spring MVC和AngularJS我得到AngularJS 415不支持的媒体类型!

I tried setting angular header to application/json 我尝试将angular header设置为application / json
I tried with @Consumes annotation on Server Side 我尝试在服务器端使用@Consumes注释
I tried with consumes ="application/json" 我尝试使用consumes ="application/json"
I tired with consumes ="application/application/json;charset=UTF-8' 我厌倦了consumes ="application/application/json;charset=UTF-8'
I tried with consumes ={"application/json","application/xml"} 我尝试使用consumes ={"application/json","application/xml"}
I also tried to setup produces property. 我也尝试设置生产属性。

I tried explicitly setting the hear content type on the client to match exactly the one on the server but, NOTHING WORKED ! 我尝试在客户端上明确设置听力内容类型,以完全匹配服务器上的那个,但是,没有工作! here are related questions that none actually helped ! 这里是没有实际帮助的相关问题! 1 1
2 3 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 还有我的AngularJS控制器

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'];

Try it: 试试吧:

@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 included Jackson Databind library. 并确保你包括Jackson Databind库。

Look at the content-type in your angular code. 查看角度代码中的内容类型。 You may need to explicitly set it. 您可能需要明确设置它。 Also look at your Spring MVC implementation. 另请参阅Spring MVC实现。 You can use the "REST Console" app in chrome to test your api to make sure it works the way you believe it should. 您可以使用chrome中的“REST控制台”应用程序来测试您的api,以确保它以您认为的方式运行。

add in your pom : 加入你的pom:

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM