java getcontenttype_Java JavaType.getContentType方法代码示例

import com.fasterxml.jackson.databind.JavaType; //导入方法依赖的package包/类

protected void initTypeInclusion(ObjectMapper mapObjectMapper) {

TypeResolverBuilder> mapTyper = new DefaultTypeResolverBuilder(DefaultTyping.NON_FINAL) {

public boolean useForType(JavaType t) {

switch (_appliesFor) {

case NON_CONCRETE_AND_ARRAYS:

while (t.isArrayType()) {

t = t.getContentType();

}

// fall through

case OBJECT_AND_NON_CONCRETE:

return (t.getRawClass() == Object.class) || !t.isConcrete();

case NON_FINAL:

while (t.isArrayType()) {

t = t.getContentType();

}

// to fix problem with wrong long to int conversion

if (t.getRawClass() == Long.class) {

return true;

}

if (t.getRawClass() == XMLGregorianCalendar.class) {

return false;

}

return !t.isFinal(); // includes Object.class

default:

// case JAVA_LANG_OBJECT:

return (t.getRawClass() == Object.class);

}

}

};

mapTyper.init(JsonTypeInfo.Id.CLASS, null);

mapTyper.inclusion(JsonTypeInfo.As.PROPERTY);

mapObjectMapper.setDefaultTyping(mapTyper);

// warm up codec

try {

byte[] s = mapObjectMapper.writeValueAsBytes(1);

mapObjectMapper.readValue(s, Object.class);

} catch (IOException e) {

throw new IllegalStateException(e);

}

}


版权声明:本文为weixin_36196497原创文章,遵循CC 4.0 BY-SA版权协议,转载请附上原文出处链接和本声明。