場景及需求: 項目接入了SpringBoot開發,如今需求是服務端接口返回的字段若是爲空,那麼自動轉爲空字符串。html
例如:
[
{
"id": 1,
"name": null
},
{
"id": 2,
"name": "xiaohong"
}
]web
如上,格式化後的返回內容應該爲:
[
{
"id": 1,
"name": ""
},
{
"id": 2,
"name": "xiaohong"
}
]spring
這裏直接給出解決方案代碼,這裏支持FastJson和Jackson配置序列化的方式:json
@Configuration public class WebCatMvcConfiguration extends WebMvcConfigurationSupport { @Override protected void configureMessageConverters(List<HttpMessageConverter<?>> converters) { MappingJackson2HttpMessageConverter converter = new MappingJackson2HttpMessageConverter(); ObjectMapper objectMapper = new ObjectMapper(); SimpleModule module = new SimpleModule(); module.addSerializer(new ToStringSerializer(Long.TYPE)); module.addSerializer(new ToStringSerializer(Long.class)); module.addSerializer(new ToStringSerializer(BigInteger.class)); objectMapper.getSerializerProvider().setNullValueSerializer(new JsonSerializer<Object>() { @Override public void serialize(Object o, JsonGenerator jsonGenerator, SerializerProvider serializerProvider) throws IOException { jsonGenerator.writeString(""); } }); objectMapper.registerModule(module); converter.setObjectMapper(objectMapper); //這裏是fastJSON的配置方式,更多的內容能夠查看SerializerFeature // FastJsonHttpMessageConverter converter = new FastJsonHttpMessageConverter(); // converter.setFeatures(SerializerFeature.WriteNullStringAsEmpty, SerializerFeature.WriteNullNumberAsZero, // SerializerFeature.WriteNullBooleanAsFalse, SerializerFeature.WriteNullListAsEmpty); converters.add(converter); }
}
最後咱們也能夠了解一下:WebMvcConfigurationSupport類
下面是它的官方文檔描述:
微信
public class WebMvcConfigurationSupportextends Objectimplements ApplicationContextAware, ServletContextAware
This is the main class providing the configuration behind the MVC Java config. It is typically imported by adding @EnableWebMvc
to an application@Configuration
class. An alternative more advanced option is to extend directly from this class and override methods as necessary remembering to add@Configuration
to the subclass and @Bean
to overridden @Bean
methods. For more details see the Javadoc of @EnableWebMvc
.mvc
This class registers the following HandlerMapping
s:app
RequestMappingHandlerMapping
ordered at 0 for mapping requests to annotated controller methods.ide
HandlerMapping
ordered at 1 to map URL paths directly to view names.post
BeanNameUrlHandlerMapping
ordered at 2 to map URL paths to controller bean names.this
HandlerMapping
ordered at Integer.MAX_VALUE-1
to serve static resource requests.
HandlerMapping
ordered at Integer.MAX_VALUE
to forward requests to the default servlet.
Registers these HandlerAdapter
s:
RequestMappingHandlerAdapter
for processing requests with annotated controller methods.
HttpRequestHandlerAdapter
for processing requests with HttpRequestHandler
s.
SimpleControllerHandlerAdapter
for processing requests with interface-based Controller
s.
Registers a HandlerExceptionResolverComposite
with this chain of exception resolvers:
ExceptionHandlerExceptionResolver
for handling exceptions through @ExceptionHandler
methods.
ResponseStatusExceptionResolver
for exceptions annotated with @ResponseStatus
.
DefaultHandlerExceptionResolver
for resolving known Spring exception types
Registers an AntPathMatcher
and a UrlPathHelper
to be used by:
the HandlerMapping
for ViewControllers
and the HandlerMapping
for serving resources
Note that those beans can be configured with a PathMatchConfigurer
.
Both the RequestMappingHandlerAdapter
and the ExceptionHandlerExceptionResolver
are configured with default instances of the following by default:
a OptionalValidatorFactoryBean
if a JSR-303 implementation is available on the classpath
a range of HttpMessageConverter
s depending on the third-party libraries available on the classpath.
分類: SpringBoot