Java Code Examples for com.fasterxml.jackson.databind.ObjectMapper#setVisibility()

The following examples show how to use com.fasterxml.jackson.databind.ObjectMapper#setVisibility() . You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example 1
Source File: RedisConfig.java    From notes with Apache License 2.0 6 votes vote down vote up
@SuppressWarnings({"unchecked", "rawtypes"})
@Bean
public RedisTemplate<Object, Object> redisTemplate(RedisConnectionFactory redisConnectionFactory)
        throws UnknownHostException {

    RedisTemplate<Object, Object> template = new RedisTemplate<Object, Object>();
    template.setConnectionFactory(redisConnectionFactory);

    ObjectMapper om = new ObjectMapper();

    om.setVisibility(PropertyAccessor.ALL, JsonAutoDetect.Visibility.ANY);
    RedisSerializer redisSerializer = fastJson2RedisSerializer();

    template.setValueSerializer(redisSerializer);
    template.setKeySerializer(new StringRedisSerializer());
    template.afterPropertiesSet();

    return template;

}
 
Example 2
Source File: JpomApplication.java    From Jpom with MIT License 6 votes vote down vote up
/**
     * jackson 配置
     *
     * @return mapper
     */
    private static ObjectMapper createJackson() {
        Jackson2ObjectMapperBuilder jackson2ObjectMapperBuilder = Jackson2ObjectMapperBuilder.json();
        jackson2ObjectMapperBuilder.simpleDateFormat(DatePattern.NORM_DATETIME_PATTERN);
        ObjectMapper build = jackson2ObjectMapperBuilder.build();
        // 忽略空
        build.setSerializationInclusion(JsonInclude.Include.NON_NULL);
        // 驼峰转下划线
        //        build.setPropertyNamingStrategy(new PropertyNamingStrategy.SnakeCaseStrategy());
        // long to String
        SimpleModule simpleModule = new SimpleModule();
        simpleModule.addSerializer(Long.class, ToStringSerializer.instance);
        simpleModule.addSerializer(Long.TYPE, ToStringSerializer.instance);
        build.registerModule(simpleModule);
        //
        build.setVisibility(PropertyAccessor.ALL, JsonAutoDetect.Visibility.ANY);
//        build.activateDefaultTyping(objectMapper.getPolymorphicTypeValidator(), ObjectMapper.DefaultTyping.NON_FINAL, JsonTypeInfo.As.WRAPPER_ARRAY);

        return build;
    }
 
Example 3
Source File: NotificationBoltFluxComponent.java    From streamline with Apache License 2.0 6 votes vote down vote up
@Override
protected void generateComponent() {
    notificationSink = (NotificationSink) conf.get(StormTopologyLayoutConstants.STREAMLINE_COMPONENT_CONF_KEY);
    String boltId = "notificationBolt" + UUID_FOR_COMPONENTS;
    String boltClassName = "com.hortonworks.streamline.streams.runtime.storm.bolt.notification.NotificationBolt";
    String[] constructorArgNames = {TopologyLayoutConstants.JSON_KEY_NOTIFICATION_SINK_JSON};
    try {
        if (notificationSink == null) {
            throw new RuntimeException("Error generating flux, notificationSink = " + notificationSink);
        }
        ObjectMapper objectMapper = new ObjectMapper();
        objectMapper.setVisibility(PropertyAccessor.ALL, JsonAutoDetect.Visibility.NONE);
        objectMapper.setVisibility(PropertyAccessor.FIELD, JsonAutoDetect.Visibility.ANY);
        String notificationSinkJsonStr = objectMapper.writeValueAsString(notificationSink);
        conf.put(TopologyLayoutConstants.JSON_KEY_NOTIFICATION_SINK_JSON, notificationSinkJsonStr);
    } catch (JsonProcessingException ex) {
        throw new RuntimeException(ex);
    }
    List<Object> boltConstructorArgs = getConstructorArgsYaml(constructorArgNames);
    String[] configMethodNames = {"withNotificationStoreClass"};
    String[] configKeys = {TopologyLayoutConstants.JSON_KEY_NOTIFICATION_STORE_CLASS};
    List configMethods = getConfigMethodsYaml(configMethodNames, configKeys);
    component = createComponent(boltId, boltClassName, null, boltConstructorArgs, configMethods);
    addParallelismToComponent();
}
 
Example 4
Source File: RedisConfig.java    From demo-project with MIT License 6 votes vote down vote up
@SuppressWarnings("all")
@Bean
public RedisTemplate<String, String> redisTemplate(RedisConnectionFactory factory) {
    StringRedisTemplate template = new StringRedisTemplate(factory);
    Jackson2JsonRedisSerializer jackson2JsonRedisSerializer = new Jackson2JsonRedisSerializer(Object.class);
    ObjectMapper om = new ObjectMapper();
    om.setVisibility(PropertyAccessor.ALL, JsonAutoDetect.Visibility.ANY);
    om.enableDefaultTyping(ObjectMapper.DefaultTyping.NON_FINAL);
    jackson2JsonRedisSerializer.setObjectMapper(om);
    RedisSerializer stringSerializer = new StringRedisSerializer();
    template.setKeySerializer(stringSerializer);
    template.setValueSerializer(jackson2JsonRedisSerializer);
    template.setHashKeySerializer(stringSerializer);
    template.setHashValueSerializer(jackson2JsonRedisSerializer);
    template.afterPropertiesSet();
    return template;
}
 
Example 5
Source File: RedisConfig.java    From biliob_backend with MIT License 6 votes vote down vote up
@Bean
public RedisTemplate<Object, Object> redisTemplate(RedisConnectionFactory connectionFactory) {
    RedisTemplate<Object, Object> template = new RedisTemplate<>();
    template.setConnectionFactory(connectionFactory);

    // 使用Jackson2JsonRedisSerializer来序列化和反序列化redis的value值(默认使用JDK的序列化方式)
    Jackson2JsonRedisSerializer<Object> serializer =
            new Jackson2JsonRedisSerializer<>(Object.class);

    ObjectMapper mapper = new ObjectMapper();
    mapper.setVisibility(PropertyAccessor.ALL, JsonAutoDetect.Visibility.ANY);
    mapper.enableDefaultTyping(ObjectMapper.DefaultTyping.NON_FINAL);
    serializer.setObjectMapper(mapper);
    template.setValueSerializer(serializer);
    // 使用StringRedisSerializer来序列化和反序列化redis的key值
    template.setKeySerializer(new StringRedisSerializer());
    template.afterPropertiesSet();
    return template;
}
 
Example 6
Source File: RedisConfig.java    From xmfcn-spring-cloud with Apache License 2.0 6 votes vote down vote up
/**
 * springboot2.x 使用LettuceConnectionFactory 代替 RedisConnectionFactory
 * 在application.yml配置基本信息后,springboot2.x  RedisAutoConfiguration能够自动装配 LettuceConnectionFactory 和 RedisConnectionFactory 及其 RedisTemplate
 *
 * @param redisConnectionFactory
 * @return
 */
@Bean
public RedisTemplate redisTemplate(LettuceConnectionFactory redisConnectionFactory) {
    RedisTemplate<String, Object> template = new RedisTemplate<>();
    template.setConnectionFactory(redisConnectionFactory);

    //使用Jackson2JsonRedisSerializer来序列化和反序列化redis的value值
    Jackson2JsonRedisSerializer serializer = new Jackson2JsonRedisSerializer(Object.class);

    ObjectMapper mapper = new ObjectMapper();
    mapper.setVisibility(PropertyAccessor.ALL, JsonAutoDetect.Visibility.ANY);
    mapper.enableDefaultTyping(ObjectMapper.DefaultTyping.NON_FINAL);
    serializer.setObjectMapper(mapper);

    template.setValueSerializer(serializer);
    //使用StringRedisSerializer来序列化和反序列化redis的key值
    template.setKeySerializer(new StringRedisSerializer());
    template.setHashKeySerializer(new StringRedisSerializer());
    template.setHashValueSerializer(serializer);
    template.afterPropertiesSet();
    return template;
}
 
Example 7
Source File: JacksonMappingExceptionUnitTest.java    From tutorials with MIT License 5 votes vote down vote up
@Test
public final void givenObjectHasNoAccessors_whenSerializingWithPrivateFieldsVisibility_thenNoException() throws JsonParseException, IOException {
    final ObjectMapper objectMapper = new ObjectMapper();
    objectMapper.setVisibility(PropertyAccessor.FIELD, Visibility.ANY);
    final String dtoAsString = objectMapper.writeValueAsString(new MyDtoNoAccessors());

    assertThat(dtoAsString, containsString("intValue"));
    assertThat(dtoAsString, containsString("stringValue"));
    assertThat(dtoAsString, containsString("booleanValue"));
}
 
Example 8
Source File: RedisConfig.java    From ueboot with BSD 3-Clause "New" or "Revised" License 5 votes vote down vote up
@Bean
@ConditionalOnMissingBean(type = {"org.springframework.data.redis.core.RedisTemplate"})
public RedisTemplate<?, ?> redisTemplate(
        LettuceConnectionFactory redisConnectionFactory) {
    StringRedisTemplate template = new StringRedisTemplate(redisConnectionFactory);
    Jackson2JsonRedisSerializer jackson2JsonRedisSerializer = new Jackson2JsonRedisSerializer(Object.class);
    ObjectMapper om = new ObjectMapper();
    om.setVisibility(PropertyAccessor.ALL, JsonAutoDetect.Visibility.ANY);
    om.activateDefaultTyping(BasicPolymorphicTypeValidator.builder().build(),ObjectMapper.DefaultTyping.NON_FINAL);
    jackson2JsonRedisSerializer.setObjectMapper(om);
    template.setValueSerializer(jackson2JsonRedisSerializer);
    template.setConnectionFactory(redisConnectionFactory);
    template.afterPropertiesSet();
    return template;
}
 
Example 9
Source File: RedisCacheAutoConfiguration.java    From loc-framework with MIT License 5 votes vote down vote up
private org.springframework.data.redis.cache.RedisCacheConfiguration determineConfiguration() {
  Redis redisProperties = this.cacheProperties.getRedis();
  org.springframework.data.redis.cache.RedisCacheConfiguration config = org.springframework.data.redis.cache.RedisCacheConfiguration
      .defaultCacheConfig();

  Jackson2JsonRedisSerializer<Object> jackson2JsonRedisSerializer = new Jackson2JsonRedisSerializer<>(
      Object.class);

  ObjectMapper om = new ObjectMapper();
  om.setVisibility(PropertyAccessor.GETTER, JsonAutoDetect.Visibility.ANY);
  om.enableDefaultTyping(ObjectMapper.DefaultTyping.NON_FINAL);
  om.setSerializationInclusion(Include.NON_NULL);
  om.configure(DeserializationFeature.FAIL_ON_UNKNOWN_PROPERTIES, false);

  jackson2JsonRedisSerializer.setObjectMapper(om);
  config = config
      .serializeKeysWith(SerializationPair.fromSerializer(new StringRedisSerializer()));
  config = config
      .serializeValuesWith(SerializationPair.fromSerializer(jackson2JsonRedisSerializer));

  if (redisProperties.getTimeToLive() != null) {
    config = config.entryTtl(redisProperties.getTimeToLive());
  }
  if (redisProperties.getKeyPrefix() != null) {
    config = config.prefixKeysWith(redisProperties.getKeyPrefix());
  }
  if (!redisProperties.isCacheNullValues()) {
    config = config.disableCachingNullValues();
  }
  if (!redisProperties.isUseKeyPrefix()) {
    config = config.disableKeyPrefix();
  }
  return config;
}
 
Example 10
Source File: RedisConfig.java    From mall with Apache License 2.0 5 votes vote down vote up
@Bean
public RedisSerializer<Object> redisSerializer() {
    //创建JSON序列化器
    Jackson2JsonRedisSerializer<Object> serializer = new Jackson2JsonRedisSerializer<>(Object.class);
    ObjectMapper objectMapper = new ObjectMapper();
    objectMapper.setVisibility(PropertyAccessor.ALL, JsonAutoDetect.Visibility.ANY);
    objectMapper.enableDefaultTyping(ObjectMapper.DefaultTyping.NON_FINAL);
    serializer.setObjectMapper(objectMapper);
    return serializer;
}
 
Example 11
Source File: ClientExample.java    From alchemy with MIT License 5 votes vote down vote up
private static AlchemyClient buildClient(String configurationFile) throws Exception {
    final Validator validator = BaseValidator.newValidator();
    final ObjectMapper mapper = Jackson.newObjectMapper();
    mapper.setVisibility(PropertyAccessor.FIELD, JsonAutoDetect.Visibility.ANY);
    final ConfigurationFactory<AlchemyClientConfiguration> configurationFactory =
        new DefaultConfigurationFactoryFactory<AlchemyClientConfiguration>().create(
            AlchemyClientConfiguration.class,
            validator,
            mapper,
            "");

    return new AlchemyClient(
        configurationFactory.build(new File(configurationFile))
    );
}
 
Example 12
Source File: RedisConfig.java    From mogu_blog_v2 with Apache License 2.0 5 votes vote down vote up
@Bean
public RedisTemplate<String, String> redisTemplate(RedisConnectionFactory factory) {
    StringRedisTemplate template = new StringRedisTemplate(factory);
    Jackson2JsonRedisSerializer jackson2JsonRedisSerializer = new Jackson2JsonRedisSerializer(Object.class);
    ObjectMapper om = new ObjectMapper();
    om.setVisibility(PropertyAccessor.ALL, JsonAutoDetect.Visibility.ANY);
    om.enableDefaultTyping(ObjectMapper.DefaultTyping.NON_FINAL);
    jackson2JsonRedisSerializer.setObjectMapper(om);
    template.setValueSerializer(jackson2JsonRedisSerializer);
    template.afterPropertiesSet();
    return template;
}
 
Example 13
Source File: LinksSnippetTest.java    From spring-auto-restdocs with Apache License 2.0 5 votes vote down vote up
@Before
public void setup() {
    mapper = new ObjectMapper();
    mapper.setVisibility(mapper.getSerializationConfig().getDefaultVisibilityChecker()
            .withFieldVisibility(JsonAutoDetect.Visibility.ANY));
    javadocReader = mock(JavadocReader.class);
    constraintReader = mock(ConstraintReader.class);
}
 
Example 14
Source File: ObjectMapperProvider.java    From openscoring with GNU Affero General Public License v3.0 5 votes vote down vote up
public ObjectMapperProvider(){
	ObjectMapper objectMapper = new ObjectMapper();
	objectMapper.registerModule(new PMMLModule());
	objectMapper.setVisibility(PropertyAccessor.FIELD, JsonAutoDetect.Visibility.ANY);
	objectMapper.setVisibility(PropertyAccessor.GETTER, JsonAutoDetect.Visibility.NONE);
	objectMapper.setVisibility(PropertyAccessor.IS_GETTER, JsonAutoDetect.Visibility.NONE);
	objectMapper.setVisibility(PropertyAccessor.SETTER, JsonAutoDetect.Visibility.NONE);
	objectMapper.enable(SerializationFeature.INDENT_OUTPUT);
	objectMapper.disable(SerializationFeature.WRITE_DATES_AS_TIMESTAMPS);

	setObjectMapper(objectMapper);
}
 
Example 15
Source File: UserConfigUtils.java    From burp-rest-api with BSD 2-Clause "Simplified" License 5 votes vote down vote up
/**
 * Given a userconfig, copy it in a new temporary file and injects all the registered extensions
 * @param path a userconfig path to be injected
 * @return a new userconfig path
 * @throws IOException when one of the two userconfig is not accessible/writable/creatable
 */
public String injectExtensions(String path) throws IOException {
    Path userOptionsTempFile = Files.createTempFile("user-options_", ".json");
    FileCopyUtils.copy(new File(path), userOptionsTempFile.toFile());

    //addBurpExtensions here to the temporary file and return the handle to the new temporary file
    //- read all file in in jackson object
    ObjectMapper objectMapper = new ObjectMapper();
    objectMapper.setVisibility(PropertyAccessor.FIELD, JsonAutoDetect.Visibility.ANY);
    JsonNode tree = objectMapper.readTree(userOptionsTempFile.toFile());
    //- inject the burp extensions here inside the user configuration
    JsonNode user_options = safeGet(objectMapper, tree, "user_options");
    JsonNode extender = safeGet(objectMapper, user_options, "extender");
    JsonNode extension = extender.get("extensions");
    if (!extension.isArray()) {
        ArrayNode array = objectMapper.createArrayNode();
        ((ObjectNode)extender).replace("extensions", array);
        extension = array;
    }
    for (Extension e : extensions) {
        ((ArrayNode) extension).addPOJO(e);
    }
    //- write the jackson configuration inside the temporary user configuration
    objectMapper.writer(new DefaultPrettyPrinter()).writeValue(userOptionsTempFile.toFile(), tree);

    userOptionsTempFile.toFile().deleteOnExit();
    return userOptionsTempFile.toAbsolutePath().toString();
}
 
Example 16
Source File: RedisConfig.java    From MyCommunity with Apache License 2.0 5 votes vote down vote up
@Bean
    // RedisConnectionFactory 连接工厂,Spring会自动注入这个Bean
    public RedisTemplate<String, Object> redisTemplate(RedisConnectionFactory factory) {
        RedisTemplate<String, Object> template = new RedisTemplate<>();
        // 配置连接工厂
        template.setConnectionFactory(factory);

        RedisSerializer<String> redisSerializer = new StringRedisSerializer();
        //使用Jackson2JsonRedisSerializer来序列化和反序列化redis的value值(默认使用JDK的序列化方式)
        Jackson2JsonRedisSerializer jackson2JsonRedisSerializer = new Jackson2JsonRedisSerializer(Object.class);

        ObjectMapper om = new ObjectMapper();
        // 指定要序列化的域,field,get和set,以及修饰符范围,ANY是都有包括private和public
        om.setVisibility(PropertyAccessor.ALL, JsonAutoDetect.Visibility.ANY);
        // 指定序列化输入的类型,类必须是非final修饰的,final修饰的类,比如String,Integer等会跑出异常
        om.enableDefaultTyping(ObjectMapper.DefaultTyping.NON_FINAL);

        jackson2JsonRedisSerializer.setObjectMapper(om);

        // 设置key的序列化方式
        template.setKeySerializer(redisSerializer); // 序列化为String
        // 设置value的序列化方式
        template.setValueSerializer(jackson2JsonRedisSerializer);
//        // 设置hash的key的序列化方式   当value为hash时,hash有key和value,所以需要设置
//        template.setHashKeySerializer(RedisSerializer.string());
//        // 设置hash的value的序列化方式
//        template.setHashValueSerializer(RedisSerializer.json());
        //value hashmap序列化
        template.setHashValueSerializer(jackson2JsonRedisSerializer);

        template.afterPropertiesSet();  // 使上面设置生效
        return template;
    }
 
Example 17
Source File: JacksonProvider.java    From minnal with Apache License 2.0 5 votes vote down vote up
/**
 * @param mapper
 * @param annotationsToUse
 */
public JacksonProvider(ObjectMapper mapper, Annotations[] annotationsToUse) {
    super(mapper, annotationsToUse);
    mapper.setVisibility(PropertyAccessor.FIELD, Visibility.NONE);
    mapper.setVisibility(PropertyAccessor.GETTER, Visibility.PROTECTED_AND_PUBLIC);
    mapper.setVisibility(PropertyAccessor.SETTER, Visibility.PROTECTED_AND_PUBLIC);
    mapper.configure(MapperFeature.REQUIRE_SETTERS_FOR_GETTERS, true);
    mapper.setPropertyNamingStrategy(getPropertyNamingStrategy());
}
 
Example 18
Source File: RedisConfig.java    From codeway_service with GNU General Public License v3.0 5 votes vote down vote up
/**
 * 配置自定义Json序列化器 jackson2
 *
 * @param redisConnectionFactory :redis连接工厂
 * @return :RedisTemplate
 */
@Bean
public RedisTemplate<String, Object> redisTemplate(RedisConnectionFactory redisConnectionFactory) {
    RedisTemplate<String, Object> template = new RedisTemplate<>();
    template.setConnectionFactory(redisConnectionFactory);
    // 使用jackson2序列化
    Jackson2JsonRedisSerializer<Object> jackson2JsonRedisSerializer = new Jackson2JsonRedisSerializer<>(Object.class);

    ObjectMapper mapper = new ObjectMapper();
    mapper.setVisibility(PropertyAccessor.ALL, JsonAutoDetect.Visibility.ANY);
    mapper.enableDefaultTyping(ObjectMapper.DefaultTyping.NON_FINAL);
    mapper.registerModule(new Jdk8Module())
            .registerModule(new JavaTimeModule());
    jackson2JsonRedisSerializer.setObjectMapper(mapper);
    StringRedisSerializer stringRedisSerializer = new StringRedisSerializer();

    // key采用String的序列化方式
    template.setKeySerializer(stringRedisSerializer);
    // hash的key也采用String的序列化方式
    template.setHashKeySerializer(stringRedisSerializer);
    // value序列化方式采用jackson
    template.setValueSerializer(jackson2JsonRedisSerializer);

    // hash的value序列化方式采用jackson
    template.setHashValueSerializer(jackson2JsonRedisSerializer);
    template.afterPropertiesSet();


    // 设置默认使用Jackson序列化
    template.setDefaultSerializer(new Jackson2JsonRedisSerializer<Object>(Object.class));
    return template;
}
 
Example 19
Source File: RedisConfig.java    From Spring-Boot-Book with Apache License 2.0 5 votes vote down vote up
@Bean
public RedisTemplate<String, String> redisTemplate(RedisConnectionFactory factory) {
    StringRedisTemplate template = new StringRedisTemplate(factory);
    Jackson2JsonRedisSerializer jackson2JsonRedisSerializer = new Jackson2JsonRedisSerializer(Object.class);
    ObjectMapper om = new ObjectMapper();
    om.setVisibility(PropertyAccessor.ALL, JsonAutoDetect.Visibility.ANY);
    om.enableDefaultTyping(ObjectMapper.DefaultTyping.NON_FINAL);
    jackson2JsonRedisSerializer.setObjectMapper(om);
    template.setValueSerializer(jackson2JsonRedisSerializer);
    template.afterPropertiesSet();
    return template;
}
 
Example 20
Source File: JavadocReaderImpl.java    From spring-auto-restdocs with Apache License 2.0 5 votes vote down vote up
private static ObjectMapper objectMapper() {
    ObjectMapper mapper = new ObjectMapper();
    mapper.setVisibility(mapper.getSerializationConfig().getDefaultVisibilityChecker()
            .withFieldVisibility(JsonAutoDetect.Visibility.ANY)
            .withGetterVisibility(JsonAutoDetect.Visibility.NONE)
            .withSetterVisibility(JsonAutoDetect.Visibility.NONE)
            .withCreatorVisibility(JsonAutoDetect.Visibility.NONE));
    return mapper;
}