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);
}
}
上一篇:ASP.NET WEB API的服务托管(Self-HOST)


下一篇:MVC项目实践,在三层架构下实现SportsStore-09,ASP.NET MVC调用ASP.NET Web API的查询服务