快速构建一个权限项目
Posted My丶Ou
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了快速构建一个权限项目相关的知识,希望对你有一定的参考价值。
今天我们首先讲的是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" />
今天我们就教到这里,请关注下集!
以上是关于快速构建一个权限项目的主要内容,如果未能解决你的问题,请参考以下文章