Home > Back-end >  Feign integration fastjson call interface returns an error
Feign integration fastjson call interface returns an error

Time:10-02

 package com.luyang.cloud.com mon. Config; 

The import com. Alibaba. Fastjson. Serializer. SerializerFeature;
The import com. Alibaba. Fastjson. Support. Config. FastJsonConfig;
The import com. Alibaba. Fastjson. Support. Spring. FastJsonHttpMessageConverter;
The import org. Springframework. Beans. Factory. The ObjectFactory;
The import org. Springframework. Boot. Autoconfigure. HTTP. HttpMessageConverters;
The import org. Springframework. Cloud. Openfeign. Support. ResponseEntityDecoder;
The import org. Springframework. Cloud. Openfeign. Support. SpringDecoder;
The import org. Springframework. Cloud. Openfeign. Support. SpringEncoder;
The import org. Springframework. Context. The annotation. Bean;
The import org. Springframework. HTTP. MediaType;
The import org. Springframework. HTTP. Converter. HttpMessageConverter;

Import the Java. Util. ArrayList;
import java.util.List;


Public class FeignConfig {

@ Bean
Public ResponseEntityDecoder feignDecoder () {
HttpMessageConverter fastJsonConverter=createFastJsonConverter ();
ObjectFactory The objectFactory=() - & gt; New HttpMessageConverters (fastJsonConverter);
Return new ResponseEntityDecoder (new SpringDecoder (objectFactory));
}

@ Bean
Public SpringEncoder feignEncoder () {
HttpMessageConverter fastJsonConverter=createFastJsonConverter ();
ObjectFactory The objectFactory=() - & gt; New HttpMessageConverters (fastJsonConverter);
Return new SpringEncoder (objectFactory);
}

Private HttpMessageConverter createFastJsonConverter () {

//create fastJson message converter
FastJsonHttpMessageConverter fastConverter=new FastJsonHttpMessageConverter ();

//update the latest version to=============================================================
List SupportedMediaTypes=new ArrayList<> (a);
SupportedMediaTypes. Add (MediaType. APPLICATION_JSON);
SupportedMediaTypes. Add (MediaType. APPLICATION_JSON_UTF8);
SupportedMediaTypes. Add (MediaType. APPLICATION_ATOM_XML);
SupportedMediaTypes. Add (MediaType. APPLICATION_FORM_URLENCODED);
SupportedMediaTypes. Add (MediaType. APPLICATION_OCTET_STREAM);
SupportedMediaTypes. Add (MediaType. APPLICATION_PDF);
SupportedMediaTypes. Add (MediaType. APPLICATION_RSS_XML);
SupportedMediaTypes. Add (MediaType. APPLICATION_XHTML_XML);
SupportedMediaTypes. Add (MediaType. APPLICATION_XML);
SupportedMediaTypes. Add (MediaType. IMAGE_GIF);
SupportedMediaTypes. Add (MediaType. IMAGE_JPEG);
SupportedMediaTypes. Add (MediaType. IMAGE_PNG);
SupportedMediaTypes. Add (MediaType. TEXT_EVENT_STREAM);
SupportedMediaTypes. Add (MediaType. TEXT_HTML);
SupportedMediaTypes. Add (MediaType. TEXT_MARKDOWN);
SupportedMediaTypes. Add (MediaType. TEXT_PLAIN);
SupportedMediaTypes. Add (MediaType. TEXT_XML);
FastConverter. SetSupportedMediaTypes (supportedMediaTypes);

//create the configuration class
FastJsonConfig FastJsonConfig=new FastJsonConfig ();
//modify configuration returns the content filtering
//WriteNullListAsEmpty: List fields if is null, the output of [], rather than the null
//WriteNullStringAsEmpty: character type field if it is null, the output is "", rather than the null
//DisableCircularReferenceDetect: to eliminate the problem of cyclic references to the same object, the default is false (if not configuration could be entering dead loop)
//WriteNullBooleanAsFalse: if a Boolean field is null, the output is false, rather than the null
//WriteMapNullValue: whether the output value is null fields, the default is false
FastJsonConfig. SetSerializerFeatures (
SerializerFeature DisableCircularReferenceDetect,
SerializerFeature. WriteMapNullValue
);
FastConverter. SetFastJsonConfig (fastJsonConfig);

Return fastConverter;
}

Public @ Bean HttpMessageConverters fastJsonHttpMessageConverters () {
//define a convert converts message object
FastJsonHttpMessageConverter fastConverter=new FastJsonHttpMessageConverter ();
//add fastjson configuration information
FastJsonConfig FastJsonConfig=new FastJsonConfig ();
FastJsonConfig. SetSerializerFeatures (SerializerFeature. PrettyFormat);
//configuration information is added in the convert
FastConverter. SetFastJsonConfig (fastJsonConfig);
//add the convert to the converters in
HttpMessageConverter<?> The converter=fastConverter;
Return new HttpMessageConverters (converter);
}
}


Integration after using A service invoke service B B normal return data but A Null service receives data and error
 Java. Lang. IllegalArgumentException: Invalid characters (CR/LF) in the header WWW - Authenticate 
The at org. Springframework. Security. Web. Firewall. FirewalledResponse. ValidateCrlf (FirewalledResponse. Java: 73) ~ [spring ws-security - web - 5.1.7. The jar: 5.1.7. RELEASE]
The at org. Springframework. Security. Web. Firewall. FirewalledResponse. AddHeader (FirewalledResponse. Java: 55) ~ [spring ws-security - web - 5.1.7. The jar: 5.1.7. RELEASE]
The at javax.mail. Servlet. HTTP. HttpServletResponseWrapper. AddHeader (HttpServletResponseWrapper. Java: 177) ~ [javax.mail. Servlet - API - 4.0.1. Jar: 4.0.1]
The at org. Springframework. Security. Web. Util. OnCommittedResponseWrapper. AddHeader (OnCommittedResponseWrapper. Java: 63) ~ [spring ws-security - web - 5.1.7. The jar: 5.1.7. RELEASE]
nullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnull
  • Related