简体   繁体   中英

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

I am using Spring 3.2.10, jquery 1.6 and com.googlecode.json-simple 1.1.1 libraries. Follow I post my code,

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

    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. And also I already put the “<mvc:annotation-driven />” tag in my spring configuration file. 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. You mentioned that you are using a json-simple library. Spring MVC expects jackson libraries on the classpath, so this can very well be your issue.

Try adding jackson libraries to your project. 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)

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

Try using JSON.stringify. The ajax call will look like

function 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);
        }
    });

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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