程序员节快乐~!
不说废话,直接上代码
import com.alibaba.fastjson.serializer.SerializerFeature;
import com.alibaba.fastjson.support.config.FastJsonConfig;
import com.alibaba.fastjson.support.spring.FastJsonHttpMessageConverter;
import feign.codec.Decoder;
import feign.codec.Encoder;
import feign.form.spring.SpringFormEncoder;
import org.springframework.boot.autoconfigure.http.HttpMessageConverters;
import org.springframework.cloud.openfeign.support.SpringDecoder;
import org.springframework.cloud.openfeign.support.SpringEncoder;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.context.annotation.Primary;
import org.springframework.context.annotation.Scope;
import org.springframework.http.MediaType;
import java.util.ArrayList;
import java.util.List;
/**
* @author 青蛙
* @date 2023/10/23 10:48 AM
*/
@Configuration
public class WechatConfiguration {
static class Jackson2HttpMessageConverter extends FastJsonHttpMessageConverter {
public Jackson2HttpMessageConverter() {
//解决微信服务不管是提交什么ContentType都是返回Text_plain的破问题
List<MediaType> types = new ArrayList<>();
types.add(MediaType.TEXT_PLAIN);
types.add(MediaType.TEXT_HTML);
types.add(MediaType.APPLICATION_JSON);
setSupportedMediaTypes(types);
//配置fastjson
FastJsonConfig config = new FastJsonConfig();
//防止递归问题
config.setSerializerFeatures(SerializerFeature.DisableCircularReferenceDetect);
setFastJsonConfig(config);
}
}
@Bean
public Decoder decoder() {
//重写使用FastJSON序列化
return new SpringDecoder(() -> new HttpMessageConverters(new Jackson2HttpMessageConverter()));
}
@Bean
@Primary
@Scope("prototype")
public Encoder encoder() {
//重写使用FastJSON序列化
return new SpringFormEncoder(new SpringEncoder(() -> new HttpMessageConverters(new Jackson2HttpMessageConverter())));
}
}