IpUtil,CookeiUtil,ObjectMapperUtil工具类

Posted 可——叹——落叶飘零

tags:

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

文章目录

IpUtil

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.util.StringUtils;
import org.springframework.web.context.request.RequestContextHolder;
import org.springframework.web.context.request.ServletRequestAttributes;
import javax.servlet.http.HttpServletRequest;

public class IPUtils 
    private static Logger logger = LoggerFactory.getLogger(IPUtils.class);
    public static String getIpAddr() 
        HttpServletRequest request = ((ServletRequestAttributes) RequestContextHolder.getRequestAttributes()).getRequest();
        String ip = null;
        try 
            ip = request.getHeader("x-forwarded-for");
            if (StringUtils.isEmpty(ip) || "unknown".equalsIgnoreCase(ip)) 
                ip = request.getHeader("Proxy-Client-IP");
            
            if (StringUtils.isEmpty(ip) || ip.length() == 0 || "unknown".equalsIgnoreCase(ip)) 
                ip = request.getHeader("WL-Proxy-Client-IP");
            
            if (StringUtils.isEmpty(ip) || "unknown".equalsIgnoreCase(ip)) 
                ip = request.getHeader("HTTP_CLIENT_IP");
            
            if (StringUtils.isEmpty(ip) || "unknown".equalsIgnoreCase(ip)) 
                ip = request.getHeader("HTTP_X_FORWARDED_FOR");
            
            if (StringUtils.isEmpty(ip) || "unknown".equalsIgnoreCase(ip)) 
                ip = request.getRemoteAddr();
            
         catch (Exception e) 
            logger.error("IPUtils ERROR ", e);
        
        return ip;
    

CookieUtil

import org.springframework.boot.web.servlet.server.Session;
import javax.servlet.http.Cookie;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

public class CookieUtil 

    public static void addCookie(HttpServletResponse response,String cookieName, String cookieValue, String doMain, int second)
        javax.servlet.http.Cookie cookie=new Cookie(cookieName,cookieValue );
        cookie.setDomain(doMain);
        cookie.setMaxAge(second);
        cookie.setPath("/");
        response.addCookie(cookie);
    
    public static  Cookie getCookie(HttpServletRequest request,String cookieName)
        Cookie[] cookies=request.getCookies();
        if(cookies!=null&&cookies.length>0)
            for (Cookie cookie:cookies)
                if(cookie.getName().equals(cookieName))
                    return cookie;
                
            
        
        return null;
    
    public static String getCookieValue(HttpServletRequest request,String cookieName)
        Cookie cookie=getCookie(request,cookieName );
        return cookie==null?null:cookie.getValue();
    

ObjectMapperUtil

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

public class ObjectMapperUtil 
    private static final ObjectMapper MAPPER=new ObjectMapper();
    public static String toJSON(Object obj)
        try 
            return MAPPER.writeValueAsString(obj);
         catch (JsonProcessingException e) 
            //将检查异常转化为运行时异常,之后被全局异常处理机制处理
            e.printStackTrace();
            throw new RuntimeException(e);
        
    
    //json转对象
    public static <T>T toObject(String json,Class<T> target)
        try 
            return MAPPER.readValue(json,target );
         catch (JsonProcessingException e) 
            //将检查异常转化为运行时异常,之后被全局异常处理机制处理
            e.printStackTrace();
            throw new RuntimeException(e);
        
    

以上是关于IpUtil,CookeiUtil,ObjectMapperUtil工具类的主要内容,如果未能解决你的问题,请参考以下文章

springboot拦截器中获取配置文件值

java 造轮子之手写logback简单版

9.3 客户端接收响应信息(异步转同步的实现)

Swift - 将类型动态传递给 JSONDecoder

SpringBoot.09.SpringBoot中如何处理Filter抛出的异常

如何通过自动装配注入具有不同配置的不同ObjectMappers?