JsonConfig 사용

Programming/Java 2017.04.23 16:48 Posted by 파란크리스마스

JsonConfig 사용하기

출처 : 알팅스님의 개발 스토리 : Java Object(POJO) To JSON String

Mybatis XML

	<resultMap id="resultMapUserInfo" type="map">
		<result property="user_id" column="user_id" javaType="java.lang.String"/>
		<result property="email" column="email" javaType="java.lang.String"/>
		<result property="mobile" column="mobile" javaType="java.lang.String"/>
		<result property="name" column="name" javaType="java.lang.String"/>
		<result property="point" column="point" javaType="int"/>
		<result property="reg_date" column="reg_date" javaType="java.util.Date"/>
		<result property="service_start_date" column="service_start_date" javaType="java.util.Date"/>
		<result property="service_end_date" column="service_end_date" javaType="java.util.Date"/>
	</resultMap>

	<!-- listUser -->
	<select id="listUser" parameterType="map" resultMap="resultMapUserInfo">
		select a.*
		  FROM user_info a
		 WHERE a.delete_yn = 'N'
	</select>

Java

	JsonConfig jsonConfig = new JsonConfig();
	jsonConfig.registerJsonValueProcessor(java.util.Date.class, new JsonValueProcessor() {

		@Override
		public Object processArrayValue(Object value, JsonConfig jsonConfig) {
			
			java.util.Date[] dates = (java.util.Date[]) value;
			Long[] result = new Long[dates.length];
			for (int index = 0; index < dates.length; index++) {
				result[index] = dates[index].getTime();
			}
			return result;
		}

		@Override
		public Object processObjectValue(String key, Object value, JsonConfig jsonConfig) {
			java.util.Date date = (java.util.Date) value;
			return date.getTime();
		}
	});
	
	List<Map<String, Object>> listUser = service.listUser(null);
	jsonObjet.put("listUser", JSONArray.fromObject(listUser, jsonConfig));
저작자 표시
신고


 

티스토리 툴바