Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

当父类的父类定义了范型, 子类反序列化失败 #2361

Closed
wkkata opened this issue Apr 9, 2019 · 2 comments
Closed

当父类的父类定义了范型, 子类反序列化失败 #2361

wkkata opened this issue Apr 9, 2019 · 2 comments

Comments

@wkkata
Copy link
Contributor

wkkata commented Apr 9, 2019

public class SuperBaseReply {
private List items;

public List<T> getItems() {
    return items;
}

public void setItems(List<T> items) {
    this.items = items;
}

}

public class BaseReply extends SuperBaseReply {

}

public class TestReply extends BaseReply {

}

当反序列化TestReply的时候, getItems会报失败,JSONOBJECT无法转的问题

@timandy
Copy link
Contributor

timandy commented Apr 26, 2019

泛型参数没有继承到子类,导致编译擦除引起的。建议将泛型参数继承到子类反序列化时使用T ypeR eference

@wkkata wkkata closed this as completed Apr 29, 2019
@TomYule
Copy link

TomYule commented Oct 22, 2020

springboot 可以如下配置 解决

@Configuration
@EnableWebMvc
public class Application extends WebMvcConfigurerAdapter {

 /**
     * 使用阿里 FastJson 作为JSON MessageConverter
     * @param converters
     */
    @Override
    public void configureMessageConverters(List<HttpMessageConverter<?>> converters) {
        FastJsonHttpMessageConverter converter = new FastJsonHttpMessageConverter();

//        log.info("converters:" + converters.toString());
        List<MediaType> supportMediaTypeList = new ArrayList<>();
//        supportMediaTypeList.add(MediaType.TEXT_HTML);
        supportMediaTypeList.add(MediaType.APPLICATION_JSON_UTF8);
//        supportMediaTypeList.add(MediaType.IMAGE_GIF);
//        supportMediaTypeList.add(MediaType.IMAGE_JPEG);
//        supportMediaTypeList.add(MediaType.IMAGE_PNG);

        FastJsonConfig config = new FastJsonConfig();
        config.setDateFormat("yyyy-MM-dd HH:mm:ss");
        config.setSerializerFeatures(
                //保留空的字段
                SerializerFeature.WriteMapNullValue,
                //String null -> ""
                SerializerFeature.WriteNullStringAsEmpty,
                //Number null -> 0
                SerializerFeature.WriteNullNumberAsZero,
                //List null-> []
                SerializerFeature.WriteNullListAsEmpty,
                //Boolean null -> false
                SerializerFeature.WriteNullBooleanAsFalse);
        converter.setFastJsonConfig(config);
        converter.setSupportedMediaTypes(supportMediaTypeList);
        converter.setDefaultCharset(Charset.forName("UTF-8"));
        converters.add(converter);
    }
}

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

3 participants