반응형
VO객체를 만들어 JSON으로 출력을 하다보면 상황에 따라 필요 없는 값을 모두 보여준다.
기본적인 스프링 MVC 구조를 예를 들어보겠다!
/* VO */
public class BoardVO {
private String id;
private String title;
private String contents;
private String link;
/* Getter */
/* Setter */
/* ToString */
}
/* DAO */
@Repository
public class BoardDAO {
@Autowired
private SqlSession session;
private String namespace = "com.xxx.xxx.xxx.mapper.";
public List<BoardVO> selectBoardList() {
return session.selectList(namespace + "selectBoardList");
}
}
/* Service */
@Service
public class BoardService {
@Autowired
private BoardDAO boardDAO;
public List<BoardVO> selectBoardList() {
return boardDAO.selectBoardList();
}
}
/* Controller */
@RestController
@RequestMapping("/board")
public class BoardController {
@AutoWired
private BoardService boardService;
@GetMapping(value = "/list")
public ResponseEntity<RestModel> list() throws Exception {
HashMap<String, Object> resultMap = new HashMap<String, Object>();
List<BoardVO> boardList = boardService.selectBoardList();
if(boardList == null) {
/* 에러 처리 */
} else {
resultMap.put("boardList", boardList);
}
/* REST 생략 */
return Util.ok;
}
기본적인 스프링을 이용해 게시판을 만들 때 사용하는 구조이다.
이 때 mapper에 있는 selectBoardList 가 이런 상황이라면 출력은 어떻게 될까?
<? xml --생략-->
<!DOCTYPE mapper --생략-->
<mapper namespace="com.xxx.xxx.xxx.mapper">
<select id="selectBoardList" resultType="com.xxx.xxx.xxx.xx.xxx">
SELECT id
, title
, contents
FROM board
</select>
</mapper>
출력은 이렇게 나올 것이다.
"boardList" [
{
"id" : "test_id" ,
"title" : "test_title" ,
"content" : "test_content",
"link" : null
}
]
지금이야 보기에 괜찮지만 변수가 많아진다면 불필요한 null 값을 제거하는게 가독성이 좋고 능률이 올라간다.
VO 윗부분에 어노테이션 하나만 추가한다면 null 값을 제거할 수 있다.
/* VO */
@JsonInclude(JsonInclude.Include.NON_NULL)
public class BoardVO {
private String id;
private String title;
private String contents;
private String link;
/* Getter */
/* Setter */
/* ToString */
}
JsonInclude 어노테이션을 추가했을 때의 출력은 다음과 같다.
"boardList" [
{
"id" : "test_id" ,
"title" : "test_title" ,
"content" : "test_content"
}
]
이렇게 null 값이나 ""값 등을 제거할 수 있다.
반응형
'Spring' 카테고리의 다른 글
[Spring] 게시판 페이징 처리 (사용하는 이유 및 특징, MyBatis, JPA) (0) | 2023.11.02 |
---|---|
API 문서 자동화: Swagger와 Spring REST Docs의 특징과 장단점 (0) | 2023.10.13 |
[Spring] JPA vs MyBatis 정리 (특징, 장점, 단점 등) (0) | 2022.06.24 |
[Spring] 프로퍼티(properties) 파일을 이용한 값 설정 (0) | 2022.06.16 |
[Spring] enum(이늄) 사용자 레벨 관리 기능 (0) | 2022.06.15 |
반응형
VO객체를 만들어 JSON으로 출력을 하다보면 상황에 따라 필요 없는 값을 모두 보여준다.
기본적인 스프링 MVC 구조를 예를 들어보겠다!
/* VO */
public class BoardVO {
private String id;
private String title;
private String contents;
private String link;
/* Getter */
/* Setter */
/* ToString */
}
/* DAO */
@Repository
public class BoardDAO {
@Autowired
private SqlSession session;
private String namespace = "com.xxx.xxx.xxx.mapper.";
public List<BoardVO> selectBoardList() {
return session.selectList(namespace + "selectBoardList");
}
}
/* Service */
@Service
public class BoardService {
@Autowired
private BoardDAO boardDAO;
public List<BoardVO> selectBoardList() {
return boardDAO.selectBoardList();
}
}
/* Controller */
@RestController
@RequestMapping("/board")
public class BoardController {
@AutoWired
private BoardService boardService;
@GetMapping(value = "/list")
public ResponseEntity<RestModel> list() throws Exception {
HashMap<String, Object> resultMap = new HashMap<String, Object>();
List<BoardVO> boardList = boardService.selectBoardList();
if(boardList == null) {
/* 에러 처리 */
} else {
resultMap.put("boardList", boardList);
}
/* REST 생략 */
return Util.ok;
}
기본적인 스프링을 이용해 게시판을 만들 때 사용하는 구조이다.
이 때 mapper에 있는 selectBoardList 가 이런 상황이라면 출력은 어떻게 될까?
<? xml --생략-->
<!DOCTYPE mapper --생략-->
<mapper namespace="com.xxx.xxx.xxx.mapper">
<select id="selectBoardList" resultType="com.xxx.xxx.xxx.xx.xxx">
SELECT id
, title
, contents
FROM board
</select>
</mapper>
출력은 이렇게 나올 것이다.
"boardList" [
{
"id" : "test_id" ,
"title" : "test_title" ,
"content" : "test_content",
"link" : null
}
]
지금이야 보기에 괜찮지만 변수가 많아진다면 불필요한 null 값을 제거하는게 가독성이 좋고 능률이 올라간다.
VO 윗부분에 어노테이션 하나만 추가한다면 null 값을 제거할 수 있다.
/* VO */
@JsonInclude(JsonInclude.Include.NON_NULL)
public class BoardVO {
private String id;
private String title;
private String contents;
private String link;
/* Getter */
/* Setter */
/* ToString */
}
JsonInclude 어노테이션을 추가했을 때의 출력은 다음과 같다.
"boardList" [
{
"id" : "test_id" ,
"title" : "test_title" ,
"content" : "test_content"
}
]
이렇게 null 값이나 ""값 등을 제거할 수 있다.
반응형
'Spring' 카테고리의 다른 글
[Spring] 게시판 페이징 처리 (사용하는 이유 및 특징, MyBatis, JPA) (0) | 2023.11.02 |
---|---|
API 문서 자동화: Swagger와 Spring REST Docs의 특징과 장단점 (0) | 2023.10.13 |
[Spring] JPA vs MyBatis 정리 (특징, 장점, 단점 등) (0) | 2022.06.24 |
[Spring] 프로퍼티(properties) 파일을 이용한 값 설정 (0) | 2022.06.16 |
[Spring] enum(이늄) 사용자 레벨 관리 기능 (0) | 2022.06.15 |