spring mvc @Configuration addConverterFactory 无效问题

spring 版本: 4.3.7 addFormatters(FormatterRegistry registry) 不生效

<!-- 此处与 @EnableWebmvc 冲突, 配置此处后 EnableWebmvc 中的配置无效 -->
    <mvc:annotation-driven content-negotiation-manager="contentNegotiationManager">
        <mvc:message-converters>
            <bean class="org.springframework.http.converter.StringHttpMessageConverter">
                <constructor-arg value="UTF-8"/>
            </bean>
            <bean class="org.springframework.http.converter.json.MappingJackson2HttpMessageConverter">
                <property name="supportedMediaTypes">
                    <list>
                        <value>application/json;charset=UTF-8</value>
                        <value>text/plain;charset=utf-8</value>
                        <value>text/html;charset=UTF-8</value>
                        <value>text/json;charset=UTF-8</value>
                        <value>text/html;charset=ISO-8859-1</value>
                        <value>application/x-www-form-urlencoded;charset=UTF-8</value>
                    </list>
                </property>
                <property name="prettyPrint" value="false"></property>
                <property name="objectMapper">
                    <bean class="com.we.core.web.view.JsonMapper">
                        <property name="dateFormat">
                            <bean class="java.text.SimpleDateFormat">
                                <constructor-arg type="java.lang.String" value="yyyy-MM-dd HH:mm:ss"/>
                            </bean>
                        </property>
                    </bean>
                </property>
            </bean>
        </mvc:message-converters>
    </mvc:annotation-driven>

@Configuration
@EnableWebMvc
public class WebAppConfig extends WebMvcConfigurerAdapter {
    @Override
    public void addFormatters(FormatterRegistry registry) {
        super.addFormatters(registry);

        registry.addConverterFactory(new StrValueToEnumConverterFactory());
    }

}

暂时不清楚是 spring 设计如此 还是 bug 。

  • 解决方案 (建议根据基于现有配置修改)
  1. 使用 xml <mvc:annotation-driven>:
    image
<!-- conversion-service="mvcConversionService" conversion-service 这个有两个 bean , 使用 mvcConversionService
(经过 debug 调试, 另一个是错误的)-->
<mvc:annotation-driven content-negotiation-manager="contentNegotiationManager"
                       conversion-service="mvcConversionService">
        <!-- 省略 -->
</mvc:annotation-driven>
  1. 删掉 <mvc:annotation-driven>......</mvc:annotation-driven>, 使用纯 java 配置

posted on 2022-11-09 15:25  正义的伙伴!  阅读(488)  评论(0编辑  收藏  举报

导航

//增加一段JS脚本,为目录生成使用