온 코딩

[Spring](게시판 예제) 데이터 변환 (JSON, XML) 본문

카테고리 없음

[Spring](게시판 예제) 데이터 변환 (JSON, XML)

SummerON 2021. 6. 28. 18:59

1. Jackson2 라이브러리 추가 (pom.xml)

<!-- Jackson2 -->
		<dependency>
			<groupId>com.fasterxml.jackson.core</groupId>
			<artifactId>jackson-databind</artifactId>
			<version>2.7.2</version>

 

2. HttpMessageConvertor 등록(presentation-layer.xml)

어노테이션 사용을 위해 mvc 등록한다.

<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
	xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
	xmlns:context="http://www.springframework.org/schema/context"
	xmlns:mvc="http://www.springframework.org/schema/mvc"
	xsi:schemaLocation="http://www.springframework.org/schema/mvc http://www.springframework.org/schema/mvc/spring-mvc-4.2.xsd
		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-4.2.xsd">

	<context:component-scan base-package="com.hhw.view"></context:component-scan>
    
    <!-- 어노테이션 추가 -->
    <mvc:annotation-driven></mvc:annotation-driven>
    
    <!-- 파일업로드 설정 -->
    <bean id="multipartResolver" class="org.springframework.web.multipart.commons.CommonsMultipartResolver">
    	<property name="maxUploadSize" value="100000"></property>
    </bean>
    
    <!-- 다국어설정 -->
    <!-- MessageSource 등록 -->
    <bean id="messageSource" class="org.springframework.context.support.ResourceBundleMessageSource">
    	<property name="basenames">
    		<list>
    			<value>message.messageSource</value>
    		</list>
    	</property>
    </bean>
    <!-- LocaleResolver등록 -->
    <bean id="localeResolver" class="org.springframework.web.servlet.i18n.SessionLocaleResolver"></bean>
	<!-- LocaleChangeInterceptor 등록 -->
	<mvc:interceptors>
		<bean class="org.springframework.web.servlet.i18n.LocaleChangeInterceptor">
			<property name="paramName" value="lang"></property>
		</bean>
	</mvc:interceptors>
	
</beans>






3. 컨트롤러 및 VO 클래스 수정

 Json

XML

1. BoardVO

@XmlAccessorType(XmlAccessType.FIELD) : xml로 변환 가능, 이 객체의 필드(변수) 자동으로 자식 엘리먼트로 표현 

@XmlAttribute : 이 어노테이션 있는 변수는 속성으로 표현

@XmlTransient : 이 어노테이션은 Json에서 @JsonIgnore와 같은 개념

package com.hhw.biz.board;

import java.util.Date;

import javax.xml.bind.annotation.XmlAccessType;
import javax.xml.bind.annotation.XmlAccessorType;
import javax.xml.bind.annotation.XmlAttribute;
import javax.xml.bind.annotation.XmlTransient;

import org.springframework.web.multipart.MultipartFile;

import com.fasterxml.jackson.annotation.JsonIgnore;


/** VO : Value Object **/

@XmlAccessorType(XmlAccessType.FIELD)
public class BoardVO {
	
	@XmlAttribute
	private int seq;
	private String title;
	private String writer;
	private String content;
	private Date regDate;
	private int cnt;
	
	@XmlTransient
	private String searchCondition;
	@XmlTransient
	private String searchKeyword;
	@XmlTransient
	private MultipartFile uploadFile;

	public BoardVO() {
	}

	@Override
	public String toString() {
		String values = "";
		values += "BoardVO [seq=" + this.seq; 
		values +=  ", title=" + this.title;
		values +=  ", writer=" + this.writer;
		values +=  ", content=" + this.content;
		values +=  ", regDate=" + this.regDate;
		values +=  ", cnt=" + this.cnt;
		values += ']';
		return values;
	}

	/** getter/setter **/
	
	
	public int getSeq() {
		return seq;
	}
	
	@JsonIgnore
	public String getSearchCondition() {
		return searchCondition;
	}

	public void setSearchCondition(String searchCondition) {
		this.searchCondition = searchCondition;
	}
	
	@JsonIgnore
	public String getSearchKeyword() {
		return searchKeyword;
	}

	public void setSearchKeyword(String searchKeyword) {
		this.searchKeyword = searchKeyword;
	}

	public void setSeq(int seq) {
		this.seq = seq;
	}

	public String getTitle() {
		return title;
	}

	public void setTitle(String title) {
		this.title = title;
	}

	public String getWriter() {
		return writer;
	}

	public void setWriter(String writer) {
		this.writer = writer;
	}

	public String getContent() {
		return content;
	}

	public void setContent(String content) {
		this.content = content;
	}

	public Date getRegDate() {
		return regDate;
	}

	public void setRegDate(Date regDate) {
		this.regDate = regDate;
	}

	public int getCnt() {
		return cnt;
	}

	public void setCnt(int cnt) {
		this.cnt = cnt;
	}
	
	@JsonIgnore
	public MultipartFile getUploadFile() {
		return uploadFile;
	}

	public void setUploadFile(MultipartFile uploadFile) {
		this.uploadFile = uploadFile;
	}
	
}

2. BoardListVO

xml의 경우 하나의 루트앨리먼트를 가져야 하기 때문에 게시글 목록을 boardVO 객체의 배열로 가져올 수 없음 

@XmlRootElement(name="boardList")

@XmlElement(name="board")

package com.hhw.biz.board;

import java.util.List;

import javax.xml.bind.annotation.XmlAccessType;
import javax.xml.bind.annotation.XmlAccessorType;
import javax.xml.bind.annotation.XmlElement;
import javax.xml.bind.annotation.XmlRootElement;

@XmlRootElement(name="boardList")
@XmlAccessorType(XmlAccessType.FIELD)
public class BoardListVO {
	
	@XmlElement(name="board")
	private List<BoardVO> boardList;
	
	public BoardListVO() {
	}
	
	public List<BoardVO> getBoardList(){
		return boardList;
	}

	public void setBoardList(List<BoardVO> boardList){
		this.boardList = boardList;
	}
}

3. 컨트롤러

	@Autowired
	private BoardService boardService;

	@RequestMapping("/dataTransform.do")
	@ResponseBody
	public BoardListVO dataTransform(BoardVO vo) {
		vo.setSearchCondition("TITLE");
		vo.setSearchKeyword("");
		List<BoardVO> boardList = boardService.getBoardList(vo);
		BoardListVO boardListVO = new BoardListVO();
		boardListVO.setBoardList(boardList);
		
		return boardListVO;
	}
Comments