springmvc4.1+hibernate和jackson2.5的配置

首先是配置spring-servlet.xml(对springmvc配置)

<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
    xmlns:mvc="http://www.springframework.org/schema/mvc"
    xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
    xmlns:context="http://www.springframework.org/schema/context"
    xsi:schemaLocation="
        http://www.springframework.org/schema/beans
        http://www.springframework.org/schema/beans/spring-beans.xsd
    	http://www.springframework.org/schema/context
    	http://www.springframework.org/schema/context/spring-context.xsd
        http://www.springframework.org/schema/mvc
        http://www.springframework.org/schema/mvc/spring-mvc.xsd">
	
	<!-- 启动Spring的支持注解 -->
	<context:annotation-config/>
	<mvc:annotation-driven />
	
	<!-- 设置当前项目的根包 ,扫描有注解文件的包-->
	<!--
		ATTENTION!! 对于springMVC,要求配置子容器spring-servlet.xml用于配置@Controller
		所以在这里忽略扫描Controller,特别要注意,子容器的扫描会覆盖掉父容器springContext.xml
		的扫描配置所以在spring-servlet.xml中需要忽略扫描@Service,不然的话会覆盖掉父容器对
		@Service的事务增强配置,导致事务无效...........................................
	-->
	<context:component-scan base-package="com"> 
		<context:include-filter type="annotation" expression="org.springframework.stereotype.Controller" /> 
		<context:exclude-filter type="annotation" expression="org.springframework.stereotype.Service" /> 
	</context:component-scan>
	 
	<!-- 用于过滤静态文件如css js jpg png等 -->
	<mvc:default-servlet-handler/>
	
	<!-- Spring MVC视图分解器 -->
    <bean id="viewResolver" class="org.springframework.web.servlet.view.InternalResourceViewResolver">
    	<property name="prefix" value="/WEB-INF/jsp/"></property>
    	<property name="suffix" value=".jsp"></property>
    </bean>
	
	<!-- ajax json -->
    <bean
        class="org.springframework.web.servlet.mvc.annotation.AnnotationMethodHandlerAdapter">
        <property name="messageConverters">
        <list>
            <ref bean="mappingJacksonHttpMessageConverter" />
            <ref bean="stringHttpMessageConverter" />
        </list>
        </property>
    </bean>
    <!--  ajax json --> 
   <span style="color:#ff0000;"> <bean id="mappingJacksonHttpMessageConverter"
        class="org.springframework.http.converter.json.MappingJackson2HttpMessageConverter">
        <property name="supportedMediaTypes">
       		<list>
          		  <value>text/html;charset=UTF-8</value>
       	 	</list>
    	</property>
    </bean></span>
    <!-- ajax返回utf8 -->
	<bean id="stringHttpMessageConverter" class="org.springframework.http.converter.StringHttpMessageConverter" >
		<property name = "supportedMediaTypes">    
			<list>    
				<value>text/plain;charset=UTF-8</value>    
			</list>    
		</property>  
	</bean> 
    
<!-- <mvc:annotation-driven>
	    <mvc:message-converters register-defaults="true">
	        <bean class="org.springframework.http.converter.json.MappingJackson2HttpMessageConverter">
	            <property name="objectMapper">
	                <bean class="com.fasterxml.jackson.databind.ObjectMapper">
	                    <property name="serializationInclusion">
	                        <value type="com.fasterxml.jackson.annotation.JsonInclude.Include">NON_NULL</value>
	                    </property>
	                </bean>
	            </property>
	        </bean>
	    </mvc:message-converters>
	</mvc:annotation-driven> 
-->

	
	<!-- SpringMVC中的上传文件配置,在这里配置上传文件大小限制不够灵活,建议使用jQuery框架来配置,这样可以节省服务器的压力 -->
	<bean id="multipartResolver" class="org.springframework.web.multipart.commons.CommonsMultipartResolver">
		<property name="defaultEncoding" value="utf-8"/>
		<property name="maxUploadSize" value="10485760000"/>
		<property name="maxInMemorySize" value="40960"/>
	</bean>

    <bean class="org.springframework.web.servlet.mvc.annotation.DefaultAnnotationHandlerMapping"></bean>
	
	<!-- 异常处理类 
	<bean id="exceptionResolver" class="org.springframework.web.servlet.handler.SimpleMappingExceptionResolver">
		<property name="defaultErrorView">
			<value>error</value>
		</property>
		<property name="exceptionMappings">
			<props>
				<prop key="java.sql.SQLException">/error/error</prop>
				<prop key="java.lang.RuntimeException">/error/error</prop>
			</props>
		</property>
	</bean>
	-->
	
</beans>


现在想要在Controller层将一个hibernate的实体转换为json格式,但是如果这个实体存在外键关系,如果不进行@JsonManagedReference和@JsonBackReference的配置会进入死循环,如下

@Entity
@Table(name = "department", catalog = "sqlhw_company")
public class Department implements java.io.Serializable {

	// Fields

	private Integer id;
	private String DNo;
	private String DName;
	@JsonManagedReference
	private Set<Manager> managers = new HashSet<Manager>(0);
        ............
}


@Entity
@Table(name = "manager", catalog = "sqlhw_company")
public class Manager implements java.io.Serializable {

	// Fields

	private Integer id;
	@JsonBackReference
	private Department department;
        ..................
}

但是如果很多时候,我们返回一个JSON,对于需要哪些字段并不是固定的,例如我有时候我将Department转换成JSON的时候不需要managers这个字段,但是有时候又需要,所以要写一个过滤器


public class WriteJsonUtil {

	public WriteJsonUtil() {
	}
	
	//忽略ignoreNames的属性
	public static String writeJSON(Object obj, String [] ignoreNames) throws IOException {
		ObjectMapper mapper = new ObjectMapper();  
		mapper.addMixIn(Object.class, PropertyFilterMixIn.class);  
		
		FilterProvider filters = new SimpleFilterProvider().
				addFilter("IgnorePropertyByName", SimpleBeanPropertyFilter.serializeAllExcept(ignoreNames));  
		
		DateFormat df = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
		mapper.setDateFormat(df);
		
		ObjectWriter writer = mapper.writer(filters);  
		return writer.writeValueAsString(obj);
	}
	
	@JsonFilter("IgnorePropertyByName")  
	class PropertyFilterMixIn {}  
}

ignoreNames就是要过滤的字段


  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值