springboot封装JsonUtil,CookieUtil工具类代码实例
这篇文章主要介绍了springboot封装JsonUtil,CookieUtil工具类过程解析,文中通过示例代码介绍的非常详细,对大家的学习或者工作具有一定的参考学习价值,需要的朋友可以参考下
JsonUtil
publicclassJsonUtil{ privatestaticObjectMapperobjectMapper=newObjectMapper(); publicstaticStringobjectToString(Objectobject)throwsJsonProcessingException{ returnobjectMapper.writeValueAsString(object); } publicstaticTstringToObject(Stringjson,Class object)throwsIOException{ returnobjectMapper.readValue(json,object); } }
CookieUtil
publicclassCookieUtil{ publicstaticStringgetCookie(HttpServletRequestrequest,StringcookieName){ Cookie[]cookies=request.getCookies(); if(cookies!=null){ for(Cookiecookie:cookies){ if(cookie.getName().equals(cookieName)){ returncookie.getValue(); } } } returnnull; } publicstaticvoidsetCookie(HttpServletResponseresponse,StringcookieName,Stringvalue,intcookieMaxAge){ Cookiecookie=newCookie(cookieName,value); cookie.setPath("/"); cookie.setMaxAge(cookieMaxAge); response.addCookie(cookie); } publicstaticvoiddeleteCookie(HttpServletResponseresponse,StringcookieName){ setCookie(response,cookieName,null,0); } }
以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持毛票票。