0

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 ?

2
  • When you make Ajax call, data value is a string, try pass a json, for example: {key:value} Commented Feb 8, 2015 at 9:57
  • I have tried 'JSON.stringify' also, Did I miss any configuration in project? Commented Feb 8, 2015 at 16:12

2 Answers 2

1

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>
Sign up to request clarification or add additional context in comments.

Comments

0

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

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.