This repository has been archived by the owner on Oct 23, 2024. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 6.5k
FastJsonpHttpMessageConverter4_CN (已废弃)
业余布道师 edited this page Nov 30, 2018
·
1 revision
FastJson 提供了Spring MVC HttpMessageConverter的实现,将POJO输出为JSONP,支持跨域数据访问。
- FastJsonpHttpMessageConverter4 for Spring MVC 4.2+
客户端异步请求后端服务接口,前后端不在同一个域名下,需要跨域数据访问。
curl http://server:port/user/find/1?callback=javaScriptFunction
// javaScriptFunction:JavaScript代码中的回调函数名
// callback:Java代码中约定的请求参数
<mvc:annotation-driven>
<mvc:message-converters>
<bean
class="com.alibaba.fastjson.support.spring.FastJsonpHttpMessageConverter4">
<property name="supportedMediaTypes">
<list>
<value>application/json;charset=UTF-8</value>
</list>
</property>
</bean>
</mvc:message-converters>
</mvc:annotation-driven>
<mvc:default-servlet-handler />
<bean id="fastJsonpResponseBodyAdvice" class="com.alibaba.fastjson.support.spring.FastJsonpResponseBodyAdvice">
<constructor-arg>
<list>
<value>callback</value>
<value>jsonp</value>
</list>
</constructor-arg>
</bean>
@EnableWebMvc
@Configuration
public class Config extends WebMvcConfigurerAdapter {
@Bean
public FastJsonpResponseBodyAdvice fastJsonpResponseBodyAdvice() {
return new FastJsonpResponseBodyAdvice("callback", "jsonp");
}
@Override
public void extendMessageConverters(List<HttpMessageConverter<?>> converters) {
converters.add(0, new FastJsonpHttpMessageConverter4());
super.extendMessageConverters(converters);
}
}
spring boot项目中可以不继承WebMvcConfigurerAdapter
@Configuration
public class Config{
@Bean
public FastJsonpResponseBodyAdvice fastJsonpResponseBodyAdvice() {
return new FastJsonpResponseBodyAdvice("callback", "jsonp");
}
@Bean
public FastJsonpHttpMessageConverter4 fastJsonpHttpMessageConverter4() {
return new FastJsonpHttpMessageConverter4();
}
}
如有需要修改本注脚,请联系阿里巴巴,
© Alibaba Fastjson Develop Team
注明: 版权所有阿里巴巴,请注明版权所有者
If you need to amend this footnote, please contact Alibaba.
© Alibaba Fastjson Develop Team
Note: Copyright Alibaba, please indicate the copyright owner