springboot封装JsonUtil,CookieUtil工具类

Posted yloved

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了springboot封装JsonUtil,CookieUtil工具类相关的知识,希望对你有一定的参考价值。

springboot封装JsonUtil,CookieUtil工具类

yls
2019-9-23

  1. JsonUtil
public class JsonUtil 
    private static ObjectMapper objectMapper = new ObjectMapper();

    public static String objectToString(Object object) throws JsonProcessingException 
        return objectMapper.writeValueAsString(object);
    

    public static <T> T stringToObject(String json,Class<T> object) throws IOException 
        return objectMapper.readValue(json,object);
    
  1. CookieUtil
public class CookieUtil 
    public static String getCookie(HttpServletRequest request, String cookieName)

        Cookie[] cookies =  request.getCookies();
        if(cookies != null)
            for(Cookie cookie : cookies)
                if(cookie.getName().equals(cookieName))
                    return cookie.getValue();
                
            
        

        return null;
    

    public static void setCookie(HttpServletResponse response, String cookieName, String value,int cookieMaxAge)
        Cookie cookie = new Cookie(cookieName,value);
        cookie.setPath("/");
        cookie.setMaxAge(cookieMaxAge);
        response.addCookie(cookie);
    

    public static void deleteCookie(HttpServletResponse response, String cookieName)
        setCookie(response,cookieName,null,0);
    

以上是关于springboot封装JsonUtil,CookieUtil工具类的主要内容,如果未能解决你的问题,请参考以下文章

利用Jackson封装常用JsonUtil工具类

C#里JsonUtil.ToJson是啥意思

SpringBoot 拦截 response 记录日志

jsonUtil

JsonUtil对象与json互转

JSONUtil