report errors
Could not extract response: no suitable HttpMessageConverter found for
response type [classxxxx] and content type [text/plain]
reason
The return type content type is not application/JSON, but text/plain. It cannot be deserialized into object type, as shown in the figure
spring cloud openfeign essentially uses okhttpclient for request. If it is text/plain, it will be treated as text and cannot be deserialized automatically like JSON string, The following is my feinclient:
in this case, it is necessary to do compatibility processing for the return of this content type
add the jackson2httpmessageconverter conversion class, and increase the compatibility of text/plain and text/HTML return types
import org.springframework.http.MediaType;
import org.springframework.http.converter.json.MappingJackson2HttpMessageConverter;
import java.util.ArrayList;
import java.util.List;
/**
* @author <a href="mailto:[email protected]">Tino.Tang</a>
* @version ${project.version} - 2021/12/9
*/
public class MyJackson2HttpMessageConverter extends MappingJackson2HttpMessageConverter {
public LokiJackson2HttpMessageConverter() {
List<MediaType> mediaTypes = new ArrayList<>();
mediaTypes.add(MediaType.TEXT_PLAIN);
mediaTypes.add(MediaType.TEXT_HTML);
setSupportedMediaTypes(mediaTypes);
}
}
Add openfeign custom configuration and inject Decoder Bean.
import feign.Logger;
import feign.codec.Decoder;
import org.springframework.beans.factory.ObjectFactory;
import org.springframework.boot.autoconfigure.http.HttpMessageConverters;
import org.springframework.cloud.openfeign.support.SpringDecoder;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
/**
* @author <a href="mailto:[email protected]">Tino.Tang</a>
* @version ${project.version} - 2021/11/29
*/
@Configuration
public class OpenFeignLogConfig {
@Bean
public Logger.Level feignLoggerLeave() {
return Logger.Level.FULL;
}
@Bean
public Decoder feignDecoder() {
LokiJackson2HttpMessageConverter converter = new LokiJackson2HttpMessageConverter();
ObjectFactory<HttpMessageConverters> objectFactory = () -> new HttpMessageConverters(converter);
return new SpringDecoder(objectFactory);
}
}```
After processing, the call to the feign client will no longer report errors, regardless of whether the type is application/json or text/plain, it can be correctly deserialized to Object.
Read More:
- [Solved] 530 This server does not allow plain FTP. You have to use FTP over TLS
- Spring boot uses thread pool to realize asynchronous processing without return and asynchronous processing with return
- SpringCloud Use openFeign Multipartfile to Upload Files Error: Current request is not a multipart request
- Java: How to use itext to export PDF text absolute positioning (implementation method)
- [PROJECT] itdage java to get the weather and send text messages
- C# Bug Fixed InvalidCastException: Cannot cast from source type to destination type.
- [Solved] Failed to convert value of type ‘java.lang.String‘ to required type ‘java.util.Date‘;
- Feign calls cannot use “_” to report errors [How to Solve]
- The principle and return value of get() function in C language
- OTA Pack Compile Error: ExternalError: Failed to run signapk.jar: return code 1:Error: A JNI error has occurred
- [Solved] Consider defining a bean of type ‘org.springframework.data.redis.core.RedisTemplate‘ in your configu
- JAVA: How to Use Minio to upload pictures
- How to Use filechannel to copy files
- [Solved] Caused by: org.springframework.beans.factory.NoSuchBeanDefinitionException: No qualifying bean of type…
- When the database table field is set to self incrementing, use the entity class to insert or update the data to solve the error (Hibernate Framework)
- Springboot startup error: Field elasticsearchRestTemplate in cn.lili.modules.material.serviceImpl.QrMaterialServiceImpl required a bean of type
- [Solved] Error getting generated key or setting result to parameter object. UnsupportedOperationException
- List: How to de-duplication according to an attribute of an object
- JAVA: How to Use Multipartfile to upload Files