如何使用@RequestBody一个JSONP请求?如何使用、RequestBody、JSONP

2023-09-11 01:12:46 作者:风月本无界

我想使用由于在集群环境中的跨域问题JSONP数据类型来执行一个Ajax请求。

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

我可以JSONP请求映射到任何@RequestBody参数的方法,但是当我试图实现RequestMapping与@RequestBody参数,我收到了415不支持的媒体类型的错误。

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.

通常当我得到这个问题是由于某些属性不贴JSON对象与Java之间的正确映射对象被映射到春天。但唯一discrepency我能找到的是,使用JSONP它增加了一个名为PARM回调和一个带下划线_

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 "_"

所以我加了标签@JsonIgnoreProperties(ignoreUnknown = TRUE),以我的Java对象,并计算过,应该解决这个问题,但它仍然抛出这个错误。

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?

编辑:我现在看到从春天的调试日志输出该堆栈跟踪: org.springframework.web.HttpMediaTypeNotSupportedException:内容类型应用程序/八位字节流不支持

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.

推荐答案

JSONP意味着jQuery将创建一个<脚本> 元素的src 指向控制器的URL。

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

正如你所看到的,这种做法不允许你通过任何数据请求体,所有的数据应在URL中传递查询参数。 数据:{ABC:'123'} 表示 ABC = 123 添加到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.

在控制器端需要使用任何 @RequestParam (绑定inidividual参数)或者 @ModelAttribute (以绑定多个参数到一个对象):

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 { ... }