com.fasterxml.jackson.databind.ObjectMapper.setDefaultTyping()方法的使用及代码示例

x33g5p2x  于2022-01-17 转载在 其他  
字(8.6k)|赞(0)|评价(0)|浏览(107)

本文整理了Java中com.fasterxml.jackson.databind.ObjectMapper.setDefaultTyping()方法的一些代码示例,展示了ObjectMapper.setDefaultTyping()的具体用法。这些代码示例主要来源于Github/Stackoverflow/Maven等平台,是从一些精选项目中提取出来的代码,具有较强的参考意义,能在一定程度帮忙到你。ObjectMapper.setDefaultTyping()方法的具体详情如下:
包路径:com.fasterxml.jackson.databind.ObjectMapper
类名称:ObjectMapper
方法名:setDefaultTyping

ObjectMapper.setDefaultTyping介绍

[英]Method for enabling automatic inclusion of type information, using specified handler object for determining which types this affects, as well as details of how information is embedded.

NOTE: use of Default Typing can be a potential security risk if incoming content comes from untrusted sources, so care should be taken to use a TypeResolverBuilder that can limit allowed classes to deserialize.
[中]方法,用于启用类型信息的自动包含,使用指定的处理程序对象确定这会影响哪些类型,以及如何嵌入信息的详细信息。
注意:如果传入的内容来自不受信任的源,则使用默认类型可能会带来潜在的安全风险,因此应注意使用TypeResolverBuilder,以限制允许的类进行反序列化。

代码示例

代码示例来源:origin: redisson/redisson

/**
 * Method for disabling automatic inclusion of type information; if so, only
 * explicitly annotated types (ones with
 * {@link com.fasterxml.jackson.annotation.JsonTypeInfo}) will have
 * additional embedded type information.
 */
public ObjectMapper disableDefaultTyping() {
  return setDefaultTyping(null);
}

代码示例来源:origin: redisson/redisson

mapTyper.init(JsonTypeInfo.Id.CLASS, null);
mapTyper.inclusion(JsonTypeInfo.As.PROPERTY);
mapObjectMapper.setDefaultTyping(mapTyper);

代码示例来源:origin: redisson/redisson

mapTyper.init(JsonTypeInfo.Id.CLASS, null);
mapTyper.inclusion(JsonTypeInfo.As.PROPERTY);
mapObjectMapper.setDefaultTyping(mapTyper);

代码示例来源:origin: redisson/redisson

return setDefaultTyping(typer);

代码示例来源:origin: spring-projects/spring-security

public static void enableDefaultTyping(ObjectMapper mapper) {
  if (mapper != null) {
    TypeResolverBuilder<?> typeBuilder = mapper.getDeserializationConfig().getDefaultTyper(null);
    if (typeBuilder == null) {
      mapper.setDefaultTyping(createWhitelistedDefaultTyping());
    }
  }
}

代码示例来源:origin: org.springframework.security/spring-security-core

public static void enableDefaultTyping(ObjectMapper mapper) {
  if (mapper != null) {
    TypeResolverBuilder<?> typeBuilder = mapper.getDeserializationConfig().getDefaultTyper(null);
    if (typeBuilder == null) {
      mapper.setDefaultTyping(createWhitelistedDefaultTyping());
    }
  }
}

代码示例来源:origin: spring-projects/spring-framework

objectMapper.setDefaultTyping(this.defaultTyping);

代码示例来源:origin: redisson/redisson

/**
 * Method for enabling automatic inclusion of type information -- needed
 * for proper deserialization of polymorphic types (unless types
 * have been annotated with {@link com.fasterxml.jackson.annotation.JsonTypeInfo}) --
 * using "As.PROPERTY" inclusion mechanism and specified property name
 * to use for inclusion (default being "@class" since default type information
 * always uses class name as type identifier)
 *<p>
 * NOTE: use of Default Typing can be a potential security risk if incoming
 * content comes from untrusted sources, and it is recommended that this
 * is either not done, or, if enabled, use {@link #setDefaultTyping}
 * passing a custom {@link TypeResolverBuilder} implementation that white-lists
 * legal types to use.
 */
public ObjectMapper enableDefaultTypingAsProperty(DefaultTyping applicability, String propertyName)
{
  TypeResolverBuilder<?> typer = new DefaultTypeResolverBuilder(applicability);
  // we'll always use full class name, when using defaulting
  typer = typer.init(JsonTypeInfo.Id.CLASS, null);
  typer = typer.inclusion(JsonTypeInfo.As.PROPERTY);
  typer = typer.typeProperty(propertyName);
  return setDefaultTyping(typer);
}

代码示例来源:origin: org.springframework/spring-web

objectMapper.setDefaultTyping(this.defaultTyping);

代码示例来源:origin: orbit/orbit

public JsonMessageSerializer()
{
  actorReferenceModule = new ActorReferenceModule(DefaultDescriptorFactory.get());
  mapper.registerModule(actorReferenceModule);
  mapper.setVisibility(mapper.getSerializationConfig().getDefaultVisibilityChecker()
      .withFieldVisibility(JsonAutoDetect.Visibility.ANY)
      .withGetterVisibility(JsonAutoDetect.Visibility.NONE)
      .withIsGetterVisibility(JsonAutoDetect.Visibility.NONE)
      .withSetterVisibility(JsonAutoDetect.Visibility.NONE)
      .withCreatorVisibility(JsonAutoDetect.Visibility.NONE));
  mapper.setSerializationInclusion(JsonInclude.Include.NON_NULL);
  TypeResolverBuilder<?> typer = new ClassIdTypeResolverBuilder(ObjectMapper.DefaultTyping.JAVA_LANG_OBJECT);
  typer = typer.init(JsonTypeInfo.Id.NAME, null);
  typer = typer.inclusion(JsonTypeInfo.As.PROPERTY);
  mapper.setDefaultTyping(typer);
}

代码示例来源:origin: stackoverflow.com

protected ObjectMapper getObjectMapperForSerialization() {
  ObjectMapper mapper = new ObjectMapper();

  StdTypeResolverBuilder typeResolverBuilder = new ObjectMapper.DefaultTypeResolverBuilder(ObjectMapper.DefaultTyping.OBJECT_AND_NON_CONCRETE);
  typeResolverBuilder = typeResolverBuilder.inclusion(JsonTypeInfo.As.PROPERTY);
  typeResolverBuilder.init(JsonTypeInfo.Id.CLASS, new ClassNameIdResolver(SimpleType.construct(Base.class), TypeFactory.defaultInstance()));
  mapper.setDefaultTyping(typeResolverBuilder);

  return mapper;
}

代码示例来源:origin: stackoverflow.com

protected ObjectMapper getObjectMapperForDeserialization() {
   ObjectMapper mapper = new ObjectMapper();
   StdTypeResolverBuilder typeResolverBuilder = new ObjectMapper.DefaultTypeResolverBuilder(ObjectMapper.DefaultTyping.OBJECT_AND_NON_CONCRETE);
   typeResolverBuilder = typeResolverBuilder.inclusion(JsonTypeInfo.As.PROPERTY);
   typeResolverBuilder.init(JsonTypeInfo.Id.CLASS, new ClassNameIdResolver(SimpleType.construct(Base.class), TypeFactory.defaultInstance()) {
     private HashMap<Class, Class> classes = new HashMap<Class, Class>() {
       {
         put(ConcreteA.class, ConcreteAAdapter.class);
         put(ConcreteB.class, ConcreteBAdapter.class);
         put(ConcreteC.class, ConcreteCAdapter.class);
       }
     };
     @Override
     public String idFromValue(Object value) {
       return (classes.containsKey(value.getClass())) ? value.getClass().getName() : null;
     }
     @Override
     public JavaType typeFromId(String id) {
       try {
         return classes.get(Class.forName(id)) == null ? super.typeFromId(id) : _typeFactory.constructSpecializedType(_baseType, classes.get(Class.forName(id)));
       } catch (ClassNotFoundException e) {
         // todo catch the e
       }
       return super.typeFromId(id);
     }
   });
   mapper.setDefaultTyping(typeResolverBuilder);
   return mapper;
 }

代码示例来源:origin: spring-projects/spring-integration

ObjectMapper mapper = new Jackson2JsonObjectMapper().getObjectMapper();
mapper.setDefaultTyping(new WhitelistTypeResolverBuilder(trustedPackages));

代码示例来源:origin: com.ning.billing/killbill-osgi-bundles-analytics

/**
 * Method for disabling automatic inclusion of type information; if so, only
 * explicitly annotated types (ones with
 * {@link com.fasterxml.jackson.annotation.JsonTypeInfo}) will have
 * additional embedded type information.
 */
public ObjectMapper disableDefaultTyping() {
  return setDefaultTyping(null);
}

代码示例来源:origin: com.jwebmp.jackson.core/jackson-databind

/**
 * Method for disabling automatic inclusion of type information; if so, only
 * explicitly annotated types (ones with
 * {@link com.fasterxml.jackson.annotation.JsonTypeInfo}) will have
 * additional embedded type information.
 */
public ObjectMapper disableDefaultTyping() {
  return setDefaultTyping(null);
}

代码示例来源:origin: com.fasterxml.jackson.core/com.springsource.com.fasterxml.jackson.core.jackson-databind

/**
 * Method for disabling automatic inclusion of type information; if so, only
 * explicitly annotated types (ones with
 * {@link com.fasterxml.jackson.annotation.JsonTypeInfo}) will have
 * additional embedded type information.
 */
public ObjectMapper disableDefaultTyping() {
  return setDefaultTyping(null);
}

代码示例来源:origin: Nextdoor/bender

/**
 * Method for disabling automatic inclusion of type information; if so, only
 * explicitly annotated types (ones with
 * {@link com.fasterxml.jackson.annotation.JsonTypeInfo}) will have
 * additional embedded type information.
 */
public ObjectMapper disableDefaultTyping() {
  return setDefaultTyping(null);
}

代码示例来源:origin: com.eclipsesource.jaxrs/jersey-all

/**
 * Method for disabling automatic inclusion of type information; if so, only
 * explicitly annotated types (ones with
 * {@link com.fasterxml.jackson.annotation.JsonTypeInfo}) will have
 * additional embedded type information.
 */
public ObjectMapper disableDefaultTyping() {
  return setDefaultTyping(null);
}

代码示例来源:origin: hstaudacher/osgi-jax-rs-connector

/**
 * Method for disabling automatic inclusion of type information; if so, only
 * explicitly annotated types (ones with
 * {@link com.fasterxml.jackson.annotation.JsonTypeInfo}) will have
 * additional embedded type information.
 */
public ObjectMapper disableDefaultTyping() {
  return setDefaultTyping(null);
}

代码示例来源:origin: codeabovelab/haven-platform

@Autowired
public AppConfigService(KvMapperFactory kvMapperFactory, ObjectMapper objectMapper) {
  // we must use custom configuration of mapper, but need use most options and modules from global mapper
  this.objectMapper = objectMapper.copy();
  StdTypeResolverBuilder trb = new ObjectMapper.DefaultTypeResolverBuilder(ObjectMapper.DefaultTyping.JAVA_LANG_OBJECT);
  trb.init(JsonTypeInfo.Id.CLASS, new CustomTypeIdResolver()).inclusion(JsonTypeInfo.As.PROPERTY);
  this.objectMapper.setDefaultTyping(trb);
  this.objectMapper.registerModule(new KvSupportModule(kvMapperFactory));
}

相关文章

微信公众号

最新文章

更多

ObjectMapper类方法