zoukankan      html  css  js  c++  java
  • 快速构建一个权限项目(五)

    今天我们首先讲的是Json转化工具-JsonMapper开发:

    在这里我们首先在pom文件引入jackson的两个依赖,分别是:

    <!-- jackson -->
        <dependency>
          <groupId>org.codehaus.jackson</groupId>
          <artifactId>jackson-core-asl</artifactId>
          <version>1.9.13</version>
        </dependency>
        <dependency>
          <groupId>org.codehaus.jackson</groupId>
          <artifactId>jackson-mapper-asl</artifactId>
          <version>1.9.13</version>
        </dependency>

    然后在util包编写JsonMapper类:

    package cn.oyc.util;
    
    import org.codehaus.jackson.map.DeserializationConfig;
    import org.codehaus.jackson.map.ObjectMapper;
    import org.codehaus.jackson.map.SerializationConfig;
    import org.codehaus.jackson.map.annotate.JsonSerialize;
    import org.codehaus.jackson.map.ser.impl.SimpleFilterProvider;
    import org.codehaus.jackson.type.TypeReference;
    
    public class JsonMapper {
        private static ObjectMapper objectMapper = new ObjectMapper();
    
        static{
            //config
            objectMapper.disable(DeserializationConfig.Feature.FAIL_ON_UNKNOWN_PROPERTIES);
            objectMapper.configure(SerializationConfig.Feature.FAIL_ON_EMPTY_BEANS,false);
            objectMapper.setFilters(new SimpleFilterProvider().setFailOnUnknownId(false));
            objectMapper.setSerializationInclusion(JsonSerialize.Inclusion.NON_EMPTY);
        }
        public static <T> String obj2String(T src){
            if (src==null){
                return null;
            }
            try {
                return src instanceof String ? (String) src : objectMapper.writeValueAsString(src);
            }catch (Exception e){
                e.printStackTrace();
                return null;
            }
        }
        public static <T> T string2Obj(String src, TypeReference<T> typeReference) {
            if (src == null || typeReference == null) {
                return null;
            }
            try {
                return (T) (typeReference.getType().equals(String.class) ? src : objectMapper.readValue(src, typeReference));
            } catch (Exception e) {
                e.printStackTrace();
                return null;
            }
        }
    }

    然后在学习我们的获取Spring上下文工具-ApplicationContextHelper开发

    在common创建一个获取上下文的类applicationContextGelper:

    package cn.oyc.common;
    
    import org.springframework.beans.BeansException;
    import org.springframework.context.ApplicationContext;
    import org.springframework.context.ApplicationContextAware;
    import org.springframework.stereotype.Component;
    
    @Component("applicationContextHelper")
    public class ApplicationContextHelper implements ApplicationContextAware {
        private static ApplicationContext applicationContext;
    
        @Override
        public void setApplicationContext(ApplicationContext context) throws BeansException {
            applicationContext = context;
        }
        public static <T> T popBean(Class<T> clazz){
            if (applicationContext == null){
                return null;
            }
            return applicationContext.getBean(clazz);
        }
    
        public static <T> T popBean(String name,Class<T> clazz){
            if (applicationContext == null){
                return null;
            }
            return applicationContext.getBean(name,clazz);
        }
    }

    然后别忘记在spring-servle.xml配置一下,内容为:

    <bean class="cn.oyc.common.ApplicationContextHelper" lazy-init="false" />

    今天我们就教到这里,请关注下集!

  • 相关阅读:
    好看的滚动条样式
    mysql常用语句
    实现点击左边菜单,然后右边弹出网页内容。
    封装一个tab思想方法实现点击的时候显示或隐藏效果
    JS对话框_JS模态对话框showModalDialog用法总结
    登录框
    git分支介绍和常用操作
    git 撤销删除恢复某次提交记录
    charles导出请求-转换格式应用至postman
    git工作流及提交操作
  • 原文地址:https://www.cnblogs.com/Myoyc/p/12233618.html
Copyright © 2011-2022 走看看