zoukankan      html  css  js  c++  java
  • jackson


    @ControllerAdvice
    public class MyExceptionHandler {

    //1、浏览器客户端返回的都是json
    @ResponseBody
    @ExceptionHandler(Exception.class)
    public Map<String, Object> handleException(Exception e) {
    Map<String, Object> map = new HashMap<>();
    map.put("code", 100);
    map.put("message", e.getMessage());
    map.put("result", null);
    return map;
    }

    }



    import java.io.IOException;
    import java.util.ArrayList;
    import java.util.List;

    import com.fasterxml.jackson.core.JsonProcessingException;
    import com.fasterxml.jackson.core.type.TypeReference;
    import com.fasterxml.jackson.databind.ObjectMapper;

    public class JsonUtil {
    /**
    * 将对象转换为json格式字符串
    *
    * @param Object
    * @return json string
    */
    public static String toJSON(Object obj) {
    ObjectMapper om = new ObjectMapper();
    try {
    return om.writeValueAsString(obj);
    } catch (JsonProcessingException e) {
    e.printStackTrace();
    }
    return "";
    }

    /**
    *
    * 将json形式字符串转换为java实体类
    *
    */
    public static <T> T parse(String jsonStr, Class<T> clazz) {
    ObjectMapper om = new ObjectMapper();
    T readValue = null;
    try {
    readValue = om.readValue(jsonStr, clazz);
    } catch (IOException e) {
    e.printStackTrace();
    }
    return readValue;
    }
    // List<MyClass> myObjects = mapper.readValue(jsonInput, new TypeReference<List<MyClass>>(){});

    /**
    * List<T> 反序列化
    * @param jsonStr
    * @param clazz
    * @return
    */
    public static <T> List<T> parseList(String jsonStr, Class<T> clazz) {
    ObjectMapper om = new ObjectMapper();
    List<T> readValue = null;
    List<T> list = new ArrayList<>();
    try {
    readValue = om.readValue(jsonStr, new TypeReference<List<T>>(){} );
    } catch (IOException e) {
    e.printStackTrace();
    }
    return readValue;
    }



    public static String parseObject(String jsonStr) {
    ObjectMapper om = new ObjectMapper();
    String readValue = null;
    try {
    readValue = om.writeValueAsString(jsonStr);
    } catch (IOException e) {
    e.printStackTrace();
    }
    return readValue;
    }
    }



    import java.io.Serializable;
    import java.util.List;
    import java.util.Map;
    import java.util.Set;
    import java.util.concurrent.TimeUnit;

    import org.springframework.beans.factory.annotation.Autowired;
    import org.springframework.data.redis.core.HashOperations;
    import org.springframework.data.redis.core.ListOperations;
    import org.springframework.data.redis.core.RedisTemplate;
    import org.springframework.data.redis.core.SetOperations;
    import org.springframework.data.redis.core.ValueOperations;
    import org.springframework.data.redis.core.ZSetOperations;
    import org.springframework.data.redis.serializer.RedisSerializer;
    import org.springframework.data.redis.serializer.StringRedisSerializer;
    import org.springframework.stereotype.Component;

    @Component
    @SuppressWarnings({ "unchecked", "rawtypes" })
    public class RedisUtil {

    private RedisTemplate redisTemplate;

    /**
    * 将redisTemplate默认改为stringRedis序列化方式。
    *
    * @param redisTemplate
    */
    @Autowired(required = false)
    public void setRedisTemplate(RedisTemplate redisTemplate) {
    RedisSerializer stringSerializer = new StringRedisSerializer();
    redisTemplate.setKeySerializer(stringSerializer);
    redisTemplate.setValueSerializer(stringSerializer);
    redisTemplate.setHashKeySerializer(stringSerializer);
    redisTemplate.setHashValueSerializer(stringSerializer);
    this.redisTemplate = redisTemplate;
    }

    public <T> void test(String key, List<T> list) {
    ValueOperations<String, Object> operations = redisTemplate.opsForValue();
    String json = JsonUtil.toJSON(list);
    operations.set(key, json);
    }

    public <T> List<Map<String,Object>> getTest(String key,Class<T> z){
    ValueOperations<String, Object> operations = redisTemplate.opsForValue();
    Object object = operations.get(key);
    return (List<Map<String, Object>>) JsonUtil.parseList(object.toString(), z);
    }

    /**
    * 写入缓存
    *
    * @param key
    * @param value
    * @return
    */
    public boolean set(final String key, Object value) {
    boolean result = false;
    try {

    ValueOperations<Serializable, Object> operations = redisTemplate.opsForValue();

    operations.set(key, value);
    result = true;
    } catch (Exception e) {
    e.printStackTrace();
    }
    return result;
    }

    /**
    * 写入缓存设置时效时间
    *
    * @param key
    * @param value
    * @return //
    */
    public boolean set(final String key, Object value, Long expireTime, TimeUnit timeUnit) {
    boolean result = false;
    try {
    ValueOperations<Serializable, Object> operations = redisTemplate.opsForValue();
    operations.set(key, value);
    redisTemplate.expire(key, expireTime, timeUnit);
    result = true;
    } catch (Exception e) {
    e.printStackTrace();
    }
    return result;
    }

    /**
    * 批量删除对应的value
    *
    * @param keys
    */
    public void remove(final String... keys) {
    for (String key : keys) {
    remove(key);
    }
    }

    /**
    * 批量删除key
    *
    * @param pattern
    */
    public void removePattern(final String pattern) {
    Set<Serializable> keys = redisTemplate.keys(pattern);
    if (keys.size() > 0) {
    redisTemplate.delete(keys);
    }
    }

    /**
    * 删除对应的value
    *
    * @param key
    */
    public void remove(final String key) {
    if (exists(key)) {
    redisTemplate.delete(key);
    }
    }

    /**
    * 判断缓存中是否有对应的value
    *
    * @param key
    * @return
    */
    public boolean exists(final String key) {
    return redisTemplate.hasKey(key);
    }

    /**
    * 读取缓存
    *
    * @param key
    * @return
    */
    public Object get(final String key) {
    Object result = null;
    ValueOperations<Serializable, Object> operations = redisTemplate.opsForValue();
    result = operations.get(key);
    return result;
    }

    /**
    * 哈希 添加
    *
    * @param key
    * @param hashKey
    * @param value
    */
    public void hmSet(String key, Object hashKey, Object value) {
    HashOperations<String, Object, Object> hash = redisTemplate.opsForHash();
    hash.put(key, hashKey, value);
    }

    /**
    * 哈希获取数据
    *
    * @param key
    * @param hashKey
    * @return
    */
    public Object hmGet(String key, Object hashKey) {
    HashOperations<String, Object, Object> hash = redisTemplate.opsForHash();
    return hash.get(key, hashKey);
    }

    /**
    * 列表添加
    *
    * @param k
    * @param v
    */
    public void lPush(String k, Object v) {
    ListOperations<String, Object> list = redisTemplate.opsForList();
    list.rightPush(k, v);
    }

    /**
    * 列表获取
    *
    * @param k
    * @param l
    * @param l1
    * @return
    */
    public List<Object> lRange(String k, long l, long l1) {
    ListOperations<String, Object> list = redisTemplate.opsForList();
    return list.range(k, l, l1);
    }

    /**
    * 集合添加
    *
    * @param key
    * @param value
    */
    public void add(String key, Object value) {
    SetOperations<String, Object> set = redisTemplate.opsForSet();
    set.add(key, value);
    }

    /**
    * 集合获取
    *
    * @param key
    * @return
    */
    public Set<Object> setMembers(String key) {
    SetOperations<String, Object> set = redisTemplate.opsForSet();
    return set.members(key);
    }

    /**
    * 有序集合添加
    *
    * @param key
    * @param value
    * @param scoure
    */
    public void zAdd(String key, Object value, double scoure) {
    ZSetOperations<String, Object> zset = redisTemplate.opsForZSet();
    zset.add(key, value, scoure);
    }

    /**
    * 有序集合获取
    *
    * @param key
    * @param scoure
    * @param scoure1
    * @return
    */
    public Set<Object> rangeByScore(String key, double scoure, double scoure1) {
    ZSetOperations<String, Object> zset = redisTemplate.opsForZSet();
    return zset.rangeByScore(key, scoure, scoure1);
    }
    }


  • 相关阅读:
    线程同步 –Mutex和Semaphore
    线程同步 –AutoResetEvent和ManualResetEvent
    线程同步 – lock和Monitor
    .NET垃圾回收 – 非托管资源
    .NET垃圾回收 – 原理浅析
    反射简介—C#特性和反射
    反射简介—类型反射和晚期绑定
    Django REST framework 第一章 Serialization
    Django REST framework 简介
    Python Django 实用小案例2
  • 原文地址:https://www.cnblogs.com/rempop/p/12795981.html
Copyright © 2011-2022 走看看