一个新项目组,为了解决Long类型转String类型给前端,防止经度丢失。添加了一段代码,发现没用。
我在其他项目上,如此,是有用的,但是放到新项目发现没用。
其实问题还在其次,最主要的是,让有心人,学会怎样从源码的角度,去分析解决问题,具体应该怎么入手。
分析:
处理返回值是在这里
ServletInvocableHandlerMethod
public void invokeAndHandle(ServletWebRequest webRequest, ModelAndViewContainer mavContainer,
Object... providedArgs) throws Exception {
Object returnValue = invokeForRequest(webRequest, mavContainer, providedArgs);
setResponseStatus(webRequest);
if (returnValue == null) {
if (isRequestNotModified(webRequest) || getResponseStatus() != null || mavContainer.isRequestHandled()) {
disableContentCachingIfNecessary(webRequest);
mavContainer.setRequestHandled(true);
return;
}
}
else if (StringUtils.hasText(getResponseStatusReason())) {
mavContainer.setRequestHandled(true);
return;
}
mavContainer.setRequestHandled(false);
Assert.state(this.returnValueHandlers != null, "No return value handlers");
try {
this.returnValueHandlers.handleReturnValue(
returnValue, getReturnValueType(returnValue), mavContainer, webRequest);
}
catch (Exception ex) {
if (logger.isTraceEnabled()) {
logger.trace(formatErrorForReturnValue(returnValue), ex);
}
throw ex;
}
}
AbstractMessageConverterMethodProcessor:接着调用的是这个类的这个方法。
可以看到维护了messageConverters,转换器就是这个,所以要看这个转换器是怎么注入进去的。
protected <T> void writeWithMessageConverters(@Nullable T value, MethodParameter returnType,
ServletServerHttpRequest inputMessage, ServletServerHttpResponse outputMessage)
throws IOException, HttpMediaTypeNotAcceptableException, HttpMessageNotWritableException {
if (selectedMediaType != null) {
selectedMediaType = selectedMediaType.removeQualityValue();
for (HttpMessageConverter<?> converter : this.messageConverters) {
是构造方法注入进去的。
public AbstractMessageConverterMethodArgumentResolver(List<HttpMessageConverter<?>> converters,
@Nullable List<Object> requestResponseBodyAdvice) {
Assert.notEmpty(converters, "'messageConverters' must not be empty");
this.messageConverters = converters;
this.allSupportedMediaTypes = getAllSupportedMediaTypes(converters);
this.advice = new RequestResponseBodyAdviceChain(requestResponseBodyAdvice);
}
打断点,项目启动查看调用栈,发现是这里注入的。
RequestMappingHandlerAdapter类。是从RequestMappingHandlerAdapter维护的messageConverts注入进去。所以看RequestMappingHandlerAdapter类的messageConverts是怎么来的。
private List<HandlerMethodArgumentResolver> getDefaultArgumentResolvers() {
List<HandlerMethodArgumentResolver> resolvers = new ArrayList<>(30);
// Annotation-based argument resolution
resolvers.add(new RequestParamMethodArgumentResolver(getBeanFactory(), false));
resolvers.add(new RequestParamMapMethodArgumentResolver());
resolvers.add(new PathVariableMethodArgumentResolver());
resolvers.add(new PathVariableMapMethodArgumentResolver());
resolvers.add(new MatrixVariableMethodArgumentResolver());
resolvers.add(new MatrixVariableMapMethodArgumentResolver());
resolvers.add(new ServletModelAttributeMethodProcessor(false));
resolvers.add(new RequestResponseBodyMethodProcessor(getMessageConverters(), this.requestResponseBodyAdvice));
// 这里
resolvers.add(new RequestPartMethodArgumentResolver(getMessageConverters(), this.requestResponseBodyAdvice));
resolvers.add(new RequestHeaderMethodArgumentResolver(getBeanFactory()));
}
public List<HttpMessageConverter<?>> getMessageConverters() {
return this.messageConverters;
}
通过idea工具,查看这个变量的引用,发现只有两个地方对这个变量存在set值。
一个是RequestMappingHandlerAdapter的构造方法
public RequestMappingHandlerAdapter() {
this.messageConverters = new ArrayList<>(4);
this.messageConverters.add(new ByteArrayHttpMessageConverter());
this.messageConverters.add(new StringHttpMessageConverter());
if (!shouldIgnoreXml) {
try {
this.messageConverters.add(new SourceHttpMessageConverter<>());
}
catch (Error err) {
// Ignore when no TransformerFactory implementation is available
}
}
this.messageConverters.add(new AllEncompassingFormHttpMessageConverter());
}
一个是WebMvcConfigurationSupport配置类定义的@Bean注解构造RequestMappingHandlerAdapter 对象注入ioc容器。
于是我们知道了,原来RequestMappingHandlerAdapter 这么重要的对象是在这里通过@bean注解引入到ioc的。第一步构建这个对象,构建对象自然就会调用构造方法,也就是上面提到的构造方法里会对messageConverts进行添加值。构造完了之后,再调用WebMvcConfigurationSupport的getMessage方法获取MessageConvert对象,然后添加进去。
@Bean
public RequestMappingHandlerAdapter requestMappingHandlerAdapter(
@Qualifier("mvcContentNegotiationManager") ContentNegotiationManager contentNegotiationManager,
@Qualifier("mvcConversionService") FormattingConversionService conversionService,
@Qualifier("mvcValidator") Validator validator) {
RequestMappingHandlerAdapter adapter = createRequestMappingHandlerAdapter();
adapter.setContentNegotiationManager(contentNegotiationManager);
adapter.setMessageConverters(getMessageConverters());
adapter.setWebBindingInitializer(getConfigurableWebBindingInitializer(conversionService, validator));
adapter.setCustomArgumentResolvers(getArgumentResolvers());
adapter.setCustomReturnValueHandlers(getReturnValueHandlers());
if (jackson2Present) {
adapter.setRequestBodyAdvice(Collections.singletonList(new JsonViewRequestBodyAdvice()));
adapter.setResponseBodyAdvice(Collections.singletonList(new JsonViewResponseBodyAdvice()));
}
AsyncSupportConfigurer configurer = getAsyncSupportConfigurer();
if (configurer.getTaskExecutor() != null) {
adapter.setTaskExecutor(configurer.getTaskExecutor());
}
if (configurer.getTimeout() != null) {
adapter.setAsyncRequestTimeout(configurer.getTimeout());
}
adapter.setCallableInterceptors(configurer.getCallableInterceptors());
adapter.setDeferredResultInterceptors(configurer.getDeferredResultInterceptors());
return adapter;
}
configureMessageConverters(this.messageConverters);方法子类会维护一个WebMvcConfigurerComposite复合WebMvcConfigurer对象,然后循环调用里面的所有WebMvcConfigurer的configureMessageConverters方法,这是一种设计模式。
protected final List<HttpMessageConverter<?>> getMessageConverters() {
if (this.messageConverters == null) {
this.messageConverters = new ArrayList<>();
configureMessageConverters(this.messageConverters);
if (this.messageConverters.isEmpty()) {
addDefaultHttpMessageConverters(this.messageConverters);
}
extendMessageConverters(this.messageConverters);
}
return this.messageConverters;
}
WebMvcConfigurerComposite类。
@Override
public void configureMessageConverters(List<HttpMessageConverter<?>> converters) {
for (WebMvcConfigurer delegate : this.delegates) {
delegate.configureMessageConverters(converters);
}
}
WebMvcConfigurationSupport的addDefaultHttpMessageConverters方法会添加很多默认的转换器。
我这边调试,看到,这里一共添加了8个转换器。
protected final void addDefaultHttpMessageConverters(List<HttpMessageConverter<?>> messageConverters) {
messageConverters.add(new ByteArrayHttpMessageConverter());
messageConverters.add(new StringHttpMessageConverter());
messageConverters.add(new ResourceHttpMessageConverter());
messageConverters.add(new ResourceRegionHttpMessageConverter());
if (!shouldIgnoreXml) {
try {
messageConverters.add(new SourceHttpMessageConverter<>());
}
catch (Throwable ex) {
// Ignore when no TransformerFactory implementation is available...
}
}
messageConverters.add(new AllEncompassingFormHttpMessageConverter());
if (romePresent) {
messageConverters.add(new AtomFeedHttpMessageConverter());
messageConverters.add(new RssChannelHttpMessageConverter());
}
if (!shouldIgnoreXml) {
if (jackson2XmlPresent) {
Jackson2ObjectMapperBuilder builder = Jackson2ObjectMapperBuilder.xml();
if (this.applicationContext != null) {
builder.applicationContext(this.applicationContext);
}
messageConverters.add(new MappingJackson2XmlHttpMessageConverter(builder.build()));
}
else if (jaxb2Present) {
messageConverters.add(new Jaxb2RootElementHttpMessageConverter());
}
}
if (kotlinSerializationJsonPresent) {
messageConverters.add(new KotlinSerializationJsonHttpMessageConverter());
}
if (jackson2Present) {
Jackson2ObjectMapperBuilder builder = Jackson2ObjectMapperBuilder.json();
if (this.applicationContext != null) {
builder.applicationContext(this.applicationContext);
}
messageConverters.add(new MappingJackson2HttpMessageConverter(builder.build()));
}
else if (gsonPresent) {
messageConverters.add(new GsonHttpMessageConverter());
}
else if (jsonbPresent) {
messageConverters.add(new JsonbHttpMessageConverter());
}
if (jackson2SmilePresent) {
Jackson2ObjectMapperBuilder builder = Jackson2ObjectMapperBuilder.smile();
if (this.applicationContext != null) {
builder.applicationContext(this.applicationContext);
}
messageConverters.add(new MappingJackson2SmileHttpMessageConverter(builder.build()));
}
if (jackson2CborPresent) {
Jackson2ObjectMapperBuilder builder = Jackson2ObjectMapperBuilder.cbor();
if (this.applicationContext != null) {
builder.applicationContext(this.applicationContext);
}
messageConverters.add(new MappingJackson2CborHttpMessageConverter(builder.build()));
}
}
比如添加MappingJackson2HttpMessageConverter转换器,发现,builder建造者是无参构造的,build方法也没有传参,所以这里是没有提供给使用者注入参数的机会的。
Jackson2ObjectMapperBuilder builder = Jackson2ObjectMapperBuilder.json();
if (this.applicationContext != null) {
builder.applicationContext(this.applicationContext);
}
messageConverters.add(new MappingJackson2HttpMessageConverter(builder.build()));
getMessageConverters方法里调用这个方法,从方法名可以看出,这是扩展messageConverters,默认是空的方法,所以,我们可以继承这个类,然后实现这个方法,达到往messageConverts注入转换器的目的。
extendMessageConverters(this.messageConverters);
走到这里,貌似已经分析完了,但是实际处理请求的时候,发现转换器是10个,不是8个,那哪里还注入了转换器呢。
HttpMessageConvertersAutoConfiguration类里会注入HttpMessageConverters对象,这里的参数converters,里面就有两个转换器,其中就有一个我们自定义objectMapper的转换器。
ObjectProvider是spring新的类,用来注入的,避免不存在对象导致的注入空指针。
这里就有个疑问,这两个转换器,是哪里构造的,并且注入到ioc容器的。
@Bean
@ConditionalOnMissingBean
public HttpMessageConverters messageConverters(ObjectProvider<HttpMessageConverter<?>> converters) {
return new HttpMessageConverters(converters.orderedStream().collect(Collectors.toList()));
}
HttpMessageConverters构造方法,有一个参数addDefaultConverters是否添加默认构造器,前面分析了,默认构造器有8个,再加上从ioc中注入的2个,总共就是10个,对上了。
又来了新的问题,HttpMessageConverters这个对象是加入了ioc容器,但是HttpservletAdapter对象的转换器,是怎么注入进去的呢
public HttpMessageConverters(boolean addDefaultConverters, Collection<HttpMessageConverter<?>> converters) {
List<HttpMessageConverter<?>> combined = getCombinedConverters(converters,
addDefaultConverters ? getDefaultConverters() : Collections.emptyList());
combined = postProcessConverters(combined);
this.converters = Collections.unmodifiableList(combined);
}
通过打断点,给WebMvcConfigurationSupport的messageConverters变量打断点,发现其是构造 RequestMappingHandlerAdapter 对象的时候adapter.setMessageConverters(getMessageConverters());一下注入了10个转换器。
所以getMessageConverters()里面应该获取到10个转换器才对。通过debug运行方法getMessageConverters(),果然返回10个转换器。
跟进去
protected final List<HttpMessageConverter<?>> getMessageConverters() {
if (this.messageConverters == null) {
this.messageConverters = new ArrayList<>();
configureMessageConverters(this.messageConverters);
if (this.messageConverters.isEmpty()) {
addDefaultHttpMessageConverters(this.messageConverters);
}
extendMessageConverters(this.messageConverters);
}
return this.messageConverters;
}
@Bean
public RequestMappingHandlerAdapter requestMappingHandlerAdapter(
@Qualifier("mvcContentNegotiationManager") ContentNegotiationManager contentNegotiationManager,
@Qualifier("mvcConversionService") FormattingConversionService conversionService,
@Qualifier("mvcValidator") Validator validator) {
RequestMappingHandlerAdapter adapter = createRequestMappingHandlerAdapter();
adapter.setContentNegotiationManager(contentNegotiationManager);
adapter.setMessageConverters(getMessageConverters());
跟进去来到这里,WebMvcConfigurerComposite复合WebMvcConfigurer里面维护很多个WebMvcConfigurer,依次调用configureMessageConverters方法。
@Override
public void configureMessageConverters(List<HttpMessageConverter<?>> converters) {
for (WebMvcConfigurer delegate : this.delegates) {
delegate.configureMessageConverters(converters);
}
}
其中一个WebMvcAutoConfigurationAdapter,调用其configureMessageConverters方法,发现了messageConvertersProvider变量,这个变量是ObjectProvider类,这个类就是spring用来依赖注入的,看构造方法,所以,这里spring会自动把HttpMessageConverters注入进来,前面说了HttpMessageConverters类有包含我们定义好的ObjectMapper的转换器。
不过这个类是延迟加载的,没有调用其获取对象方法的时候,是不会构造注入的
private final ObjectProvider<HttpMessageConverters> messageConvertersProvider;
@Override
public void configureMessageConverters(List<HttpMessageConverter<?>> converters) {
this.messageConvertersProvider
.ifAvailable((customConverters) -> converters.addAll(customConverters.getConverters()));
}
public WebMvcAutoConfigurationAdapter(
org.springframework.boot.autoconfigure.web.ResourceProperties resourceProperties,
WebProperties webProperties, WebMvcProperties mvcProperties, ListableBeanFactory beanFactory,
ObjectProvider<HttpMessageConverters> messageConvertersProvider,
ObjectProvider<ResourceHandlerRegistrationCustomizer> resourceHandlerRegistrationCustomizerProvider,
ObjectProvider<DispatcherServletPath> dispatcherServletPath,
ObjectProvider<ServletRegistrationBean<?>> servletRegistrations) {
this.resourceProperties = resourceProperties.hasBeenCustomized() ? resourceProperties
: webProperties.getResources();
this.mvcProperties = mvcProperties;
this.beanFactory = beanFactory;
this.messageConvertersProvider = messageConvertersProvider;
this.resourceHandlerRegistrationCustomizer = resourceHandlerRegistrationCustomizerProvider.getIfAvailable();
this.dispatcherServletPath = dispatcherServletPath;
this.servletRegistrations = servletRegistrations;
this.mvcProperties.checkConfiguration();
}
本来分析到这里就结束了,但是,问题来了
之前为什么会分析这个代码呢,因为我为了解决Long类型转String类型给前端,防止经度丢失。添加了一段代码,发现没用。
我在其他项目上,如此,是有用的,但是放到新项目发现没用。
代码如下:
@Configuration
public class JacksonConfig {
/***
* 控制层返回json时处理LocalDateTime
*
* @Title: getObjectMapper
* @return
*/
@Bean(name = "mapperObject")
public ObjectMapper getObjectMapper() {
ObjectMapper objectMapper = new ObjectMapper();
objectMapper.configure(DeserializationFeature.FAIL_ON_UNKNOWN_PROPERTIES, false);
JavaTimeModule javaTimeModule = new JavaTimeModule();
// 处理json返回数据时将LocalDateTime LocalDate LocalTime转换成字符串
javaTimeModule.addSerializer(LocalDateTime.class,
new LocalDateTimeSerializer(DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss")));
javaTimeModule.addSerializer(LocalDate.class,
new LocalDateSerializer(DateTimeFormatter.ofPattern("yyyy-MM-dd")));
javaTimeModule.addSerializer(LocalTime.class, new LocalTimeSerializer(DateTimeFormatter.ofPattern("HH:mm:ss")));
// 将long类型转成String,js针对long类型的精度小于java
javaTimeModule.addSerializer(Long.class, ToStringSerializer.instance);
// @RequestBody 接受的字符串自动转成LocalDateTime LocalDate LocalTime
javaTimeModule.addDeserializer(LocalDateTime.class,
new LocalDateTimeDeserializer(DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss")));
javaTimeModule.addDeserializer(LocalDate.class,
new LocalDateDeserializer(DateTimeFormatter.ofPattern("yyyy-MM-dd")));
javaTimeModule.addDeserializer(LocalTime.class,
new LocalTimeDeserializer(DateTimeFormatter.ofPattern("HH:mm:ss")));
// SimpleModule xssModule = new SimpleModule("HTML XSS");
//xssModule.addSerializer(new XssJsonSerializer(String.class));
// xssModule.addDeserializer(String.class, new XssJsonDeserializer(String.class));
// objectMapper.registerModule(xssModule);
objectMapper.registerModules(javaTimeModule);
return objectMapper;
}
}
问题分析:前面说了,WebMvcAutoConfiguration会通过ObjectProvider延迟注入HttpMessageConverters对象。然后通过configureMessageConverters方法注入进去的,但是通过debug发现,代码根本不会进入这里。
public WebMvcAutoConfigurationAdapter(
org.springframework.boot.autoconfigure.web.ResourceProperties resourceProperties,
WebProperties webProperties, WebMvcProperties mvcProperties, ListableBeanFactory beanFactory,
ObjectProvider<HttpMessageConverters> messageConvertersProvider,
ObjectProvider<ResourceHandlerRegistrationCustomizer> resourceHandlerRegistrationCustomizerProvider,
ObjectProvider<DispatcherServletPath> dispatcherServletPath,
ObjectProvider<ServletRegistrationBean<?>> servletRegistrations) {
this.resourceProperties = resourceProperties.hasBeenCustomized() ? resourceProperties
: webProperties.getResources();
this.mvcProperties = mvcProperties;
this.beanFactory = beanFactory;
this.messageConvertersProvider = messageConvertersProvider;
this.resourceHandlerRegistrationCustomizer = resourceHandlerRegistrationCustomizerProvider.getIfAvailable();
this.dispatcherServletPath = dispatcherServletPath;
this.servletRegistrations = servletRegistrations;
this.mvcProperties.checkConfiguration();
}
@Override
public void configureMessageConverters(List<HttpMessageConverter<?>> converters) {
this.messageConvertersProvider
.ifAvailable((customConverters) -> converters.addAll(customConverters.getConverters()));
}
HttpMessageConvertersAutoConfiguration配置类里会装载HttpMessageConverters ,通过debug,发现,这里装载的转换器,是有我们自己定义ObjectMapper的转换器的。所以到这里是没问题的。
@Bean
@ConditionalOnMissingBean
public HttpMessageConverters messageConverters(ObjectProvider<HttpMessageConverter<?>> converters) {
return new HttpMessageConverters(converters.orderedStream().collect(Collectors.toList()));
}
所以有理由怀疑,之前的循环依次调用WebMvcConfigurer里没有我们的WebMvcAutoConfiguration。
@Override
public void configureMessageConverters(List<HttpMessageConverter<?>> converters) {
for (WebMvcConfigurer delegate : this.delegates) {
delegate.configureMessageConverters(converters);
}
}
发现果然没有WebMvcAutoConfiguration,下面是两个不同项目的对比,同时通过打断点,查看this对象,发现一个项目的this是WebMvcAutoConfiguration$EnableWebMvcConfiguration
一个是DelegatingWebMvcConfiguration。
image.png
image.png
然后我们来看为什么没有WebMvcAutoConfiguration
看注解当没有WebMvcConfigurationSupport类的时候,才会构建这个WebMvcAutoConfiguration。那就打断点在WebMvcConfigurationSupport方法里。
发现会构建,并且this对象是DelegatingWebMvcConfiguration
@Configuration(proxyBeanMethods = false)
@ConditionalOnWebApplication(type = Type.SERVLET)
@ConditionalOnClass({ Servlet.class, DispatcherServlet.class, WebMvcConfigurer.class })
@ConditionalOnMissingBean(WebMvcConfigurationSupport.class)
@AutoConfigureOrder(Ordered.HIGHEST_PRECEDENCE + 10)
@AutoConfigureAfter({ DispatcherServletAutoConfiguration.class, TaskExecutionAutoConfiguration.class,
ValidationAutoConfiguration.class })
public class WebMvcAutoConfiguration {
然后看DelegatingWebMvcConfiguration的子类,看注解说这个配置类等价于@EnableWebMvc注解,
/**
* Configuration equivalent to {@code @EnableWebMvc}.
*/
@Configuration(proxyBeanMethods = false)
@EnableConfigurationProperties(WebProperties.class)
public static class EnableWebMvcConfiguration extends DelegatingWebMvcConfiguration implements ResourceLoaderAware {
private final Resources resourceProperties;
然后我们的项目启动类上加了EnableWebMvc注解,其会通过import注解注入DelegatingWebMvcConfiguration类。
至此,谜底解开。
@EnableAsync(proxyTargetClass = true)
@EnableWebMvc
@SpringBootApplication(exclude = { DataSourceAutoConfiguration.class, SecurityAutoConfiguration.class })
@MapperScan({"com.bigfire.business.mapper",
"com.bigfire.system.mapper",
"com.bigfire.**.mapper", "com.bigfire.**.mapperextend"})
public class BigSkyP01ZaihaiApplication
}
@Retention(RetentionPolicy.RUNTIME)
@Target(ElementType.TYPE)
@Documented
@Import(DelegatingWebMvcConfiguration.class)
public @interface EnableWebMvc {
}
结论:一个项目启动类使用了EnableWebMvc注解,导致注入了DelegatingWebMvcConfiguration类,之后就不会注入WebMvcAutoConfiguration类,之后就不会通过ObjectProvider延迟注入HttpMessageConverters,而我们自己定义的ObjectMapper是在HttpMessageConverters里,在WebMvcConfigurerComposite循环配置HttpMessageConverter的时候就不会调用WebMvcAutoConfiguration的配置方法:configureContentNegotiation,就不会注入自定义的ObjectMapper。
网友评论