spring MVC配置conversionService无效

我访问的时候遇到错误,内容如下

警告: Failed to read HTTP message: org.springframework.http.converter.HttpMessageNotReadableException: Could not read document: Can not construct instance of com.serva.threed.util.ProjectStatus from String value ("demo"): value not one of declared Enum instance names: [IMPL, DEMO, DEAL, CLOSE]
 at [Source: java.io.PushbackInputStream@12686eff; line: 1, column: 72] (through reference chain: com.serva.threed.project.po.Project["status"]); nested exception is com.fasterxml.jackson.databind.exc.InvalidFormatException: Can not construct instance of com.serva.threed.util.ProjectStatus from String value ("demo"): value not one of declared Enum instance names: [IMPL, DEMO, DEAL, CLOSE]
 at [Source: java.io.PushbackInputStream@12686eff; line: 1, column: 72] (through reference chain: com.serva.threed.project.po.Project["status"])

错误原因是controller在响应请求时,进行数据绑定,JSON在转换Bean时,bean的一个属性是枚举类型没法直接转换。

    @RequestMapping(value = "/project", method = RequestMethod.POST, produces = {"application/json"})
    public Project insert(@RequestBody Project project) throws Exception {
        projectService.insert(project);
        return project;
    }

于是就在dispatcher-servlet.xml中添加配置,配置如下:

<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
    xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:p="http://www.springframework.org/schema/p"
    xmlns:context="http://www.springframework.org/schema/context"
    xmlns:mvc="http://www.springframework.org/schema/mvc"
    xsi:schemaLocation="http://www.springframework.org/schema/beans
                        http://www.springframework.org/schema/beans/spring-beans-4.2.xsd
                        http://www.springframework.org/schema/context
                        http://www.springframework.org/schema/context/spring-context-4.2.xsd
                        http://www.springframework.org/schema/mvc
                        http://www.springframework.org/schema/mvc/spring-mvc-4.2.xsd">

    <!-- 自动扫描 -->
    <context:annotation-config />
    <context:component-scan base-package="com.test.threed" />
    <mvc:annotation-driven conversion-service="conversionService" />

    <mvc:cors>
        <mvc:mapping path="/**" allowed-origins="*" allowed-methods="GET,POST,OPTIONS,PUT,DELETE"
                     max-age="3600" allow-credentials="true"></mvc:mapping>
    </mvc:cors>


    <!-- 枚举类型自动绑定 -->
    <!-- <bean id="myConversionService" class="org.springframework.format.support.FormattingConversionServiceFactoryBean">
        <property name="converters">
            <set>
                <bean class="com.test.threed.converter.StringToEnumConverterFactory" />
                <bean class="com.test.threed.converter.StringToProjectStatusConverter" />
            </set>
        </property>
    </bean> -->
    <bean id="conversionService" class="org.springframework.context.support.ConversionServiceFactoryBean">
        <property name="converters">
            <set>
                <bean class="com.test.threed.converter.StringToEnumConverterFactory" />
                <bean class="com.test.threed.converter.StringToProjectStatusConverter" />
            </set>
        </property>
    </bean>

</beans>

FormattingConversionServiceFactoryBean和ConversionServiceFactoryBean都不行,StringToEnumConverterFactory和StringToProjectStatusConverter中断点,压根没有进入,怀疑配置有误,无奈一直找不到相关文档

阅读 7k
2 个回答

你传的是json?@RequestBody使用的应该是jackson反序列化,和ConversionService没有关系

撰写回答
你尚未登录,登录后可以
  • 和开发者交流问题的细节
  • 关注并接收问题和回答的更新提醒
  • 参与内容的编辑和改进,让解决方法与时俱进
推荐问题