Question

I am trying to perform an ajax request using the jsonp data type due to cross domain issues in a clustered environment.

I can make jsonp requests to methods mapped with no @RequestBody parameters, but when I do try to implement a RequestMapping with a @RequestBody parameter I get a 415 Unsupported Media Type error.

Usually when I get this problem it is due to some property not mapping correctly between the json object posted and the Java object it is mapped to in Spring. But the only discrepency I can find is that using jsonp it adds a parm named callback and one with the name of an underscore "_"

So I added the tag @JsonIgnoreProperties(ignoreUnknown = true) to my Java object and figured that should solve that, however it is still throwing this error.

Is there anything else I need to do?

EDIT: I am now seeing this stack trace in the debug log output from Spring: org.springframework.web.HttpMediaTypeNotSupportedException: Content type 'application/octet-stream' not supported

$.ajax({
  url : 'http://blah/blah.html',
  data : { abc : '123' }, (I also tried to JSON.stringify the object but no difference)
  dataType : 'jsonp',
  success : function(response) {
    alert('ok '+JSON.stringify(response));
  },
  fail : function(response) { 
    alert('error'+JSON.stringify(response));
  }
});

The Spring controller is:

@RequestMapping({ "blah/blah" })
@ResponseBody
public ReturnObject getBlahBlah (@RequestBody MyObject obj) throws Exception {

    }

The parameter object is:

@JsonIgnoreProperties(ignoreUnknown = true)
public class MyObject {

  private String abc;
  // getter and setter for abc auto generated by MyEclipse
}

I have a breakpoint on the Controller method which is never hit.

Was it helpful?

Solution

JSONP means that jQuery will create a <script> element with src pointing to your controller URL.

As you can see, this approach doesn't allow you to pass any data in request body, all data should be passed in URL as query parameters. data : { abc : '123' } means that abc=123 is added to the URL.

At controller side you need to use either @RequestParam (to bind inidividual parameters) or @ModelAttribute (to bind multiple parameters into an object):

public ReturnObject getBlahBlah (@RequestParam("abc") int abc) throws Exception { ... }
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top