这篇文章主要介绍了springboot封装JsonUtil,CookieUtil工具类过程解析,文中通过示例代码介绍的非常详细,对大家的学习或者工作具有一定的参考学习价值,需要的朋友可以参考下
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); } }
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); } }
以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持亿速云。
免责声明:本站发布的内容(图片、视频和文字)以原创、转载和分享为主,文章观点不代表本网站立场,如果涉及侵权请联系站长邮箱:is@yisu.com进行举报,并提供相关证据,一经查实,将立刻删除涉嫌侵权内容。