Unsupported Media Type Error in AJAX-Spring

1.1k views Asked by At

I am trying to pass POST data from my jsp with jquery-ajax to my Spring-MVC controller function. The data is passed fine and I can print the JSON data when I use a String object to receive the RequestBody. But when I employ a DTO which has a List variable declared with its own objects the controller returns a '415 Unsupported Media Type Error' with the following statement,

The server refused this request because the request entity is in a format not supported by the requested resource for the requested method.

below given is the DTO class

    public class anyClassDTO{
          private String name;
          private List<anyClassDTO> subList = new ArrayList<anyClassDTO>();

              //getters and setters here
   }

Now, here is the controller function

@RequestMapping(headers ={"Accept=application/json"}, value = "urlFromJsp", method = RequestMethod.POST)
public @ResponseBody
String addData (HttpServletRequest request,
               @RequestBody List<anyClassDTO> dtoObject,    
               Model model)
{       
    return "{\"value\":\"true\"}";
}

Is it not possible for a list of objects to be received from the jsp page to a controller via AJAX?

Here is a set of sample data being passed from the jsp

[{"name":"module1","subList":[{"name":"chapter1","subList":[{"name":"subchapter1","subList":null}]}]},{"name":"module2","subList":[{"name":"chapter1","subList":[{"name":"subchapter1","subList":null}]}]}]

2

There are 2 answers

2
Sotirios Delimanolis On

Make sure your AJAX request sets the request's Content-Type to application/json.

Spring typically uses a MappingJacksonHttpMessageConverter to convert the request body when you specify @RequestBody. This HttpMessageConverter only supports application/*+json type content types, so you have to make sure your request contains it.

0
evyavan On

Well, we could make it work as it is by adding a little more detail. Instead of receiving the @ResponseBody as a List object I created another DTO which holds a List object of the original DTO. So the second DTO is basically a dummy which receives the data from AJAX as a single object.

Like I have said in the question I have a DTO as follows

 public class AnyClassDTO{
      private String name;
      private List<anyClassDTO> subList = new ArrayList<anyClassDTO>();

          //getters and setters here

}

I created another DTO which holds a List of the above DTO

public class DummyDTO{
      private List<AnyClassDTO> dummyObj;

      //getters and setters here
}

Then in the controller I changed the function to

@RequestMapping(headers ={"Accept=application/json"}, value = "urlFromJsp", method =  RequestMethod.POST)
public @ResponseBody
String addData (HttpServletRequest request,
           @RequestBody DummyDTO dummyDTOObj,    
           Model model)
{       
        return "{\"value\":\"true\"}";
}

Earlier if I was sending a list directly from AJAX, now I am sending a stringified litteral with a variable which holds the whole data.

And it works like a charm!