fastjson SpringMVC中泛型对象参数被转换为JSONObject

owfi6suc  于 2021-11-27  发布在  Java
关注(0)|答案(6)|浏览(385)
  • fastjson: 1.2.21
  • spring: 4.2.3.RELEASE

BaseController

public abstract class BaseController<V extends BaseVoBean<Long, V>>{
    @RequestMapping(value = "/qry", method = RequestMethod.POST)
    @ResponseBody
    public MsgHeader<List<V>> get(@RequestBody MsgHeader<V> req) {}
}

OfferCatalogController

@Controller
@RequestMapping("/offerCatalog")
public class OfferCatalogController extends BaseController<OfferCatalogVo> {}

OfferCatalogVo

public class OfferCatalogVo extends BaseVoBean<Long,OfferCatalogVo> {}

MsgHeader

public class MsgHeader<V> {
    String  messageId;
    V       value;
}

POST 请求到 /offerCatalog/qry,请求参数为:

{"messageid":"118029","value":{}}

在 Controller 请求参数,发现 @requestbody MsgHeader req 中的 value (MsgHeader 中的 value)被解析为JSONObject,而 Jackson 会将 value 解析为 OfferCatalogVo;

相关配置:

<mvc:annotation-driven>
    <mvc:message-converters register-defaults="true">
        <bean class="org.springframework.http.converter.StringHttpMessageConverter">
            <constructor-arg value="UTF-8" />
            <property name="supportedMediaTypes">
                <list>
                    <value>text/plain;charset=UTF-8</value>
                    <value>text/html;charset=UTF-8</value>
                    <value>application/json;charset=UTF-8</value>
                </list>
            </property>
        </bean>
        <bean class="com.alibaba.fastjson.support.spring.FastJsonHttpMessageConverter4" id="fastJsonHttpMessageConverter">
            <property name="supportedMediaTypes">
                <list>
                    <value>application/json;charset=UTF-8</value>
                </list>
            </property>
            <property name="fastJsonConfig">
                <bean class="com.alibaba.fastjson.support.config.FastJsonConfig">
                    <property name="serializerFeatures">
                        <list>
                            <value>IgnoreErrorGetter</value>
                        </list>
                    </property>
                    <property name="serializeFilters">
                        <bean class="com.charkeyqk.base.utils.BaseFastjsonPropertyFilter">
                            <constructor-arg>
                                <list>
                                    <value>opType</value>
                                </list>
                            </constructor-arg>
                        </bean>
                    </property>
                </bean>
            </property>
        </bean>
    </mvc:message-converters>
</mvc:annotation-driven>
eiee3dmh

eiee3dmh1#

@VictorZeng 帮忙看看

izj3ouym

izj3ouym2#

@CharkeyQK 能提供一下testcase吗

eagi6jfj

eagi6jfj3#

我整了个 web 工程,fastjson-test。你看一下工程的 TODO。谢谢。

vdgimpew

vdgimpew4#

@CharkeyQK 按照你提供的测试用例确实出现了同样问题, fastjson目前并不支持根据未指定类型的泛型对象(MsgHeader req)对其进行数据绑定,可以尝试在OfferCatalogController中重构get方法。

@Controller
@RequestMapping("/offerCatalog")
public class OfferCatalogController extends BaseController<OfferCatalogVo> {

    @Override
    public MsgHeader<List<OfferCatalogVo>> get(@RequestBody MsgHeader<OfferCatalogVo> req) {

        return super.get(req);
    }
}

@wenshao Jackson的实现方式比较特殊,Jackson将这种未指定类型的泛型对象重新检查一遍,并根据其子类产生新数据类型。

lokaqttq

lokaqttq5#

有计划实现“根据未指定类型的泛型对象对其进行数据绑定”吗?

l5tcr1uw

l5tcr1uw6#

这个问题最新版本有实现吗?

相关问题