alibaba / alibaba/fastjson2

[QUESTION] with preset Content-Type 'null'

Open
#2,126 3 comments 0 reactions 0 assignees View on GitHub
question
Dominant language
Java
Stars
4.4k
Forks
613
Avg merge
1d 22h
Merged PRs (30d)
6

Description

### 请描述您的问题
*询问有关本项目的使用和其他方面的相关问题。*
在使用FastJson覆盖springboot中关于json的转换器时出现
```java
org.springframework.http.converter.HttpMessageNotWritableException: No converter for [class org.springframework.boot.actuate.endpoint.OperationResponseBodyMap] with preset Content-Type 'null'
at org.springframework.web.servlet.mvc.method.annotation.AbstractMessageConverterMethodProcessor.writeWithMessageConverters(AbstractMessageConverterMethodProcessor.java:319) ~[spring-webmvc-6.0.14.jar:6.0.14]
at org.springframework.web.servlet.mvc.method.annotation.RequestResponseBodyMethodProcessor.handleReturnValue(RequestResponseBodyMethodProcessor.java:194) ~[spring-webmvc-6.0.14.jar:6.0.14]
at org.springframework.web.method.support.HandlerMethodReturnValueHandlerComposite.handleReturnValue(HandlerMethodReturnValueHandlerComposite.java:78) ~[spring-web-6.0.14.jar:6.0.14]
at org.springframework.web.servlet.mvc.method.annotation.ServletInvocableHandlerMethod.invokeAndHandle(ServletInvocableHandlerMethod.java:136) ~[spring-webmvc-6.0.14.jar:6.0.14]
at org.springframework.web.servlet.mvc.method.annotation.RequestMappingHandlerAdapter.invokeHandlerMethod(RequestMappingHandlerAdapter.java:884) ~[spring-webmvc-6.0.14.jar:6.0.14]
at org.springframework.web.servlet.mvc.method.annotation.RequestMappingHandlerAdapter.handleInternal(RequestMappingHandlerAdapter.java:797) ~[spring-webmvc-6.0.14.jar:6.0.14]
at org.springframework.web.servlet.mvc.method.AbstractHandlerMethodAdapter.handle(AbstractHandlerMethodAdapter.java:87) ~[spring-webmvc-6.0.14.jar:6.0.14]
at org.springframework.web.servlet.DispatcherServlet.doDispatch(DispatcherServlet.java:1081) ~[spring-webmvc-6.0.14.jar:6.0.14]
at org.springframework.web.servlet.DispatcherServlet.doService(DispatcherServlet.java:974) ~[spring-webmvc-6.0.14.jar:6.0.14]
at org.springframework.web.servlet.FrameworkServlet.processRequest(FrameworkServlet.java:1014) ~[spring-webmvc-6.0.14.jar:6.0.14]
at org.springframework.web.servlet.FrameworkServlet.doGet(FrameworkServlet.java:903) ~[spring-webmvc-6.0.14.jar:6.0.14]
```

配置如下:
```java
@Configuration
@Order(Ordered.HIGHEST_PRECEDENCE)
public class CustomWebMvcConfig implements WebMvcConfigurer {

@Bean
public HttpMessageConverter responseBodyConverter() {
return new StringHttpMessageConverter(StandardCharsets.UTF_8);
}

/**
* 处理mvc json解析问题
* @param converters
*/
@Override
public void configureMessageConverters(List> converters) {
// 1、需要先定义一个convert转换消息的对象;
FastJsonHttpMessageConverter fastConverter = new FastJsonHttpMessageConverter();
// 2、添加fastjson的配置信息,比如 是否要格式化返回json数据
FastJsonConfig config = new FastJsonConfig();
config.setCharset(StandardCharsets.UTF_8);
config.setDateFormat("yyyy-MM-dd HH:mm:ss");
config.setWriterFeatures(JSONWriter.Feature.WriteNulls, JSONWriter.Feature.WriteMapNullValue, JSONWriter.Feature.PrettyFormat); // 配置序列化的行为
config.setReaderFeatures(JSONReader.Feature.FieldBased, JSONReader.Feature.SupportArrayToBean); // 配置反序列化的行为
fastConverter.setFastJsonConfig(config);

fastConverter.setDefaultCharset(StandardCharsets.UTF_8);
fastConverter.setSupportedMediaTypes(Collections.singletonList(MediaType.APPLICATION_JSON));
converters.add(0, fastConverter);
}
}
```

Contributor guide

Open the contributing guide

Research direction

Start by reviewing the posted CustomWebMvcConfig and the Spring MVC stack trace, focusing on how FastJsonHttpMessageConverter is registered and limited to application/json. Reproduce the failure with the Spring Boot Actuator endpoint described in the report. Done means identifying whether the converter configuration causes the null Content-Type error and documenting or validating a compatible resolution.

Written by the indexing model from the issue text.

Assessment

Tech stack
java, spring-boot
Domain
api, backend
Issue type
Bug
Difficulty
4/5
Estimated time
3-5 days
Activity status
Stale
Clarity
Needs clarification
Newbie friendliness
25/100

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.