Spring 使用 MappingJacksonValue 响应和严格的 MIME 类型错误启动 JSONP
Spring Boot JSONP with MappingJacksonValue response and strict MIME type error
我已经阅读了很多关于 Spring 4 的 JSONP 支持的文章,但我仍然缺乏一个清晰的解释来使其与正确的媒体类型一起工作(在 chrome 下)
1) 我添加了 JsonpAdvice cfr Jackson JSONP Support
@ControllerAdvice
public class JsonpAdvice extends AbstractJsonpResponseBodyAdvice {
public JsonpAdvice() {
super("callback");
}
}
2) 我的控制器用 MappingJacksonValue*
包装响应
@RequestMapping(value = '/api/test', method = RequestMethod.GET)
@ResponseBody
public Object test(HttpServletRequest request) {
List<String> result = new ArrayList<String>();
result.add("hello");
result.add("world");
if(request.getParameter('callback')){
MappingJacksonValue value = new MappingJacksonValue(result)
value.setJsonpFunction(request.getParameter('callback'))
return value
}
return result
}
不确定 MappingJacksonValue 是否必要,或者 MappingJackson2HttpMessageConverter 是否会处理它?
3) 我在 application.yml:
中添加了明确的 media-types
spring:
profiles.active: development
jackson.property-naming-strategy: SNAKE_CASE
mvc:
media-types:
json: 'application/json'
xml: 'application/xml'
jsonp: 'application/javascript'
但是我仍然在 Chrome
中得到 下列错误:
Refused to execute script from 'https://example.com/api/test?callback=jQuery22406993800323428312_1481922214995&_=1481922214996'
because its MIME type ('application/json') is not executable, and strict MIME type checking is enabled.
缺少任何步骤吗?还是配置太多?
调试我的 JsonpAdvice.groovy 后,我发现 AbstractJsonpResponseBodyAdvice 需要一个字符串列表:private final String[] jsonpQueryParamNames;
我的初始代码使用的是一个简单的字符串。这是修复:
@ControllerAdvice
public class JsonpAdvice extends AbstractJsonpResponseBodyAdvice {
public JsonpAdvice() {
super(["callback"])
}
}
我已经阅读了很多关于 Spring 4 的 JSONP 支持的文章,但我仍然缺乏一个清晰的解释来使其与正确的媒体类型一起工作(在 chrome 下)
1) 我添加了 JsonpAdvice cfr Jackson JSONP Support
@ControllerAdvice
public class JsonpAdvice extends AbstractJsonpResponseBodyAdvice {
public JsonpAdvice() {
super("callback");
}
}
2) 我的控制器用 MappingJacksonValue*
包装响应@RequestMapping(value = '/api/test', method = RequestMethod.GET)
@ResponseBody
public Object test(HttpServletRequest request) {
List<String> result = new ArrayList<String>();
result.add("hello");
result.add("world");
if(request.getParameter('callback')){
MappingJacksonValue value = new MappingJacksonValue(result)
value.setJsonpFunction(request.getParameter('callback'))
return value
}
return result
}
不确定 MappingJacksonValue 是否必要,或者 MappingJackson2HttpMessageConverter 是否会处理它?
3) 我在 application.yml:
中添加了明确的 media-typesspring:
profiles.active: development
jackson.property-naming-strategy: SNAKE_CASE
mvc:
media-types:
json: 'application/json'
xml: 'application/xml'
jsonp: 'application/javascript'
但是我仍然在 Chrome
中得到 下列错误:Refused to execute script from 'https://example.com/api/test?callback=jQuery22406993800323428312_1481922214995&_=1481922214996' because its MIME type ('application/json') is not executable, and strict MIME type checking is enabled.
缺少任何步骤吗?还是配置太多?
调试我的 JsonpAdvice.groovy 后,我发现 AbstractJsonpResponseBodyAdvice 需要一个字符串列表:private final String[] jsonpQueryParamNames;
我的初始代码使用的是一个简单的字符串。这是修复:
@ControllerAdvice
public class JsonpAdvice extends AbstractJsonpResponseBodyAdvice {
public JsonpAdvice() {
super(["callback"])
}
}