feign 序列化_Spring Cloud Feign 配置 FastJson
时间: 2024-02-17 14:01:00 浏览: 152
要在Spring Cloud Feign中配置FastJson序列化,您需要完成以下步骤:
1. 添加FastJson依赖
在项目的pom.xml文件中,添加FastJson依赖:
```xml
<dependency>
<groupId>com.alibaba</groupId>
<artifactId>fastjson</artifactId>
<version>1.2.47</version>
</dependency>
```
2. 创建FastJson转换器
创建一个FastJson转换器类,继承自Spring的HttpMessageConverter接口,并实现其方法。代码如下:
```java
import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.parser.Feature;
import com.alibaba.fastjson.serializer.SerializerFeature;
import org.springframework.http.HttpHeaders;
import org.springframework.http.HttpInputMessage;
import org.springframework.http.HttpOutputMessage;
import org.springframework.http.MediaType;
import org.springframework.http.converter.HttpMessageConverter;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import java.lang.reflect.Type;
import java.nio.charset.Charset;
import java.util.ArrayList;
import java.util.List;
public class FastJsonHttpMessageConverter implements HttpMessageConverter<Object> {
public static final Charset DEFAULT_CHARSET = Charset.forName("UTF-8");
private SerializerFeature[] serializerFeatures = new SerializerFeature[0];
private Feature[] parserFeatures = new Feature[0];
@Override
public boolean canRead(Type type, Class<?> contextClass, MediaType mediaType) {
return true;
}
@Override
public boolean canWrite(Type type, Class<?> aClass, MediaType mediaType) {
return true;
}
@Override
public List<MediaType> getSupportedMediaTypes() {
List<MediaType> mediaTypes = new ArrayList<>();
mediaTypes.add(MediaType.APPLICATION_JSON_UTF8);
mediaTypes.add(MediaType.APPLICATION_JSON);
mediaTypes.add(MediaType.TEXT_PLAIN);
return mediaTypes;
}
@Override
public Object read(Type type, Class<?> contextClass, HttpInputMessage inputMessage) throws IOException {
InputStream inputStream = inputMessage.getBody();
return JSON.parseObject(inputStream, DEFAULT_CHARSET, type, parserFeatures);
}
@Override
public void write(Object o, Type type, MediaType mediaType, HttpOutputMessage outputMessage) throws IOException {
HttpHeaders headers = outputMessage.getHeaders();
headers.setContentType(MediaType.APPLICATION_JSON_UTF8);
String jsonString = JSON.toJSONString(o, serializerFeatures);
OutputStream outputStream = outputMessage.getBody();
outputStream.write(jsonString.getBytes(DEFAULT_CHARSET));
outputStream.flush();
}
public SerializerFeature[] getSerializerFeatures() {
return serializerFeatures;
}
public void setSerializerFeatures(SerializerFeature[] serializerFeatures) {
this.serializerFeatures = serializerFeatures;
}
public Feature[] getParserFeatures() {
return parserFeatures;
}
public void setParserFeatures(Feature[] parserFeatures) {
this.parserFeatures = parserFeatures;
}
}
```
3. 配置FastJson转换器
在Spring的配置类中,创建一个FastJson转换器的bean,并将其注册到Spring的HttpMessageConverters中。代码如下:
```java
import com.alibaba.fastjson.serializer.SerializerFeature;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.http.converter.HttpMessageConverter;
import org.springframework.http.converter.json.MappingJackson2HttpMessageConverter;
import org.springframework.web.client.RestTemplate;
import java.util.ArrayList;
import java.util.List;
@Configuration
public class FeignConfiguration {
@Bean
public HttpMessageConverter fastJsonHttpMessageConverter() {
return new FastJsonHttpMessageConverter();
}
@Bean
public RestTemplate restTemplate() {
RestTemplate restTemplate = new RestTemplate();
List<HttpMessageConverter<?>> messageConverters = new ArrayList<>();
MappingJackson2HttpMessageConverter converter = new MappingJackson2HttpMessageConverter();
converter.setObjectMapper(new ObjectMapper());
messageConverters.add(converter);
messageConverters.add(fastJsonHttpMessageConverter());
restTemplate.setMessageConverters(messageConverters);
return restTemplate;
}
}
```
4. 配置Feign Client
在Feign Client的配置类中,使用@FeignClient注解的configuration属性,将FastJson转换器的bean引入到Feign Client中。代码如下:
```java
import org.springframework.cloud.openfeign.EnableFeignClients;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
@Configuration
@EnableFeignClients(defaultConfiguration = {FeignConfiguration.class})
public class FeignClientConfiguration {
}
```
完成以上步骤之后,您就可以在Spring Cloud Feign中使用FastJson作为序列化工具了。
阅读全文