简体   繁体   English

使用Ajax将json对象传递给Spring mvc控制器

[英]Pass json object to Spring mvc controller using Ajax

I am trying to pass simple JSON object to Spring MVC controller and getting error as “NetworkError: 415 Unsupported Media Type - https://my_url.html 我试图将简单的JSON对象传递给Spring MVC控制器,并收到“ NetworkError:415 Unsupported Media Type- https://my_url.html ”的错误消息

I am using Spring 3.2.10, jquery 1.6 and com.googlecode.json-simple 1.1.1 libraries. 我正在使用Spring 3.2.10,jquery 1.6和com.googlecode.json-简单1.1.1库。 Follow I post my code, 跟随我发布我的代码,

JSP JSP

function myTestFunction(year){
        $.ajax({
            type: "POST",                
            url: "my_url.html",
            data: "{\"testName\": \"MyName\"}",
            contentType: "application/json",
            mimeType: "application/json",
            success: function(response){
                console.log("SUCCESS: " + response);
            },
            error: function (e) {
                console.log("Error " + e);
            }
        });

Controller class 控制器类

 @RequestMapping(value = "/my_url.html", method = RequestMethod.POST)
    public void myTestMethod(@RequestBody MyInfo myInfo, HttpServletRequest request, HttpServletResponse response) throws Exception{
// my code
 }

MyInfo class MyInfo类

    public class MyInfo {    
        private String testName;

        public MyInfo () {
        }   

        public MyInfo (String testName) {
            this.testName = testName;
        }

        public String getTestName() {
            return testName;
        }

        public void setTestName(String testName) {
            this.testName = testName;
        }
}

I have tried with several options by adding dataType: 'json' and sending object using JSON.stringify But didn't work for me. 我通过添加dataType: 'json'并使用JSON.stringify发送对象尝试了几种方法,但对我不起作用。 And also I already put the “<mvc:annotation-driven />” tag in my spring configuration file. 而且我已经将“<mvc:annotation-driven />”标签放入了我的spring配置文件中。 What am I miss or doing incorrectly ? 我想念什么或做错什么?

Your request and mapping are OK. 您的请求和映射都可以。

The error you mentioned can occur when a converter attempts to convert the HTTP request to JAVA object. 当转换器尝试将HTTP请求转换为JAVA对象时,可能会出现您提到的错误。 You mentioned that you are using a json-simple library. 您提到您正在使用json-简单库。 Spring MVC expects jackson libraries on the classpath, so this can very well be your issue. Spring MVC期望在类路径上使用jackson库,因此这很可能是您的问题。

Try adding jackson libraries to your project. 尝试将jackson库添加到您的项目。 If using maven, for spring 3.2 the following should be a proper dependency, if you're using a different build system, simply download from maven repository, but also check for the transitive dependencies (you'll notice them listed inside the maven file in the jar archive) 如果使用Maven,则对于Spring 3.2,以下应该是适当的依赖项;如果使用的是不同的构建系统,则只需从maven存储库下载,然后检查传递性依赖项(您会发现它们在maven文件中列出了) jar档案)

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

Try using JSON.stringify. 尝试使用JSON.stringify。 The ajax call will look like Ajax调用看起来像

function myTestFunction(year){ 函数myTestFunction(year){

    $.ajax({
        type: "POST",                
        url: "my_url.html",
        data: JSON.stringify({"testName": "MyName"}),
        contentType: "application/json",
        mimeType: "application/json",
        success: function(response){
            console.log("SUCCESS: " + response);
        },
        error: function (e) {
            console.log("Error " + e);
        }
    });

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

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