• JsonAutoDetect注解找不到错误


    RedisConfig类代码:

    package com.neo.SpringBoot.config;

    import java.lang.reflect.Method;

    import org.springframework.cache.CacheManager;
    import org.springframework.cache.annotation.CachingConfigurerSupport;
    import org.springframework.cache.annotation.EnableCaching;
    import org.springframework.cache.interceptor.KeyGenerator;
    import org.springframework.context.annotation.Bean;
    import org.springframework.context.annotation.Configuration;
    import org.springframework.data.redis.cache.RedisCacheManager;
    import org.springframework.data.redis.connection.RedisConnectionFactory;
    import org.springframework.data.redis.core.RedisTemplate;
    import org.springframework.data.redis.core.StringRedisTemplate;
    import org.springframework.data.redis.serializer.Jackson2JsonRedisSerializer;

    import com.fasterxml.jackson.annotation.PropertyAccessor;
    import com.fasterxml.jackson.databind.ObjectMapper;

    @Configuration
    @EnableCaching
    public class RedisConfig extends CachingConfigurerSupport {

    public KeyGenerator keyGenerator() {
    return new KeyGenerator() {

    @Override
    public Object generate(Object target, Method method, Object... params) {
    StringBuilder sb = new StringBuilder();
    sb.append(target.getClass().getName());
    sb.append(method.getName());
    for (Object object : params) {
    sb.append(object.toString());
    }
    return sb.toString();
    }
    };
    }

    @SuppressWarnings("rawtypes")
    @Bean
    public CacheManager cacheManager(RedisTemplate redisTemplate) {
    RedisCacheManager rcm = new RedisCacheManager(redisTemplate);
    // 设置缓存的过期时间
    // rcm.setDefaultExpiration(60);//秒
    return rcm;
    }

    @Bean
    public RedisTemplate<String, String> redisTemplate(RedisConnectionFactory factory) {
    StringRedisTemplate template = new StringRedisTemplate(factory);
    Jackson2JsonRedisSerializer<Object> 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;
    }
    }

    错误提示:


    pom.xml应引入:

    <dependency>
    <groupId>com.fasterxml.jackson.core</groupId>
    <artifactId>jackson-annotations</artifactId>
    </dependency>

    问题解决:

    导入:import com.fasterxml.jackson.annotation.JsonAutoDetect;

  • 相关阅读:
    uvalive 3644 X-Plosives
    uva 11997 K Smallest Sums
    Codeforces Round #441 (Div. 2, by Moscow Team Olympiad) D. Sorting the Coins
    Codeforces Round #441 (Div. 2, by Moscow Team Olympiad) C. Classroom Watch
    Codeforces Round #441 (Div. 2, by Moscow Team Olympiad) B. Divisiblity of Differences
    Codeforces Round #441 (Div. 2, by Moscow Team Olympiad) A. Trip For Meal
    1004. 成绩排名 (20)
    1003. 我要通过!(20)
    1002. 写出这个数 (20)
    1001. 害死人不偿命的(3n+1)猜想 (15)
  • 原文地址:https://www.cnblogs.com/jxldjsn/p/10391567.html
Copyright © 2020-2023  润新知