[해야할 일]
0. pom.xml에 라이브러리 등록- 빈 객체 생성
- (1)객체의 리스트 빈 객체 생성
- Controller 생성
- 호출
앞서 우리가 만들고자 하는 JSON 파일은
{"messages":[{"id":1,"message":"안녕","creationTime":1533197269945},{"id":2,"message":"문의","creationTime":1533197269945},{"id":3,"message":"긴급","creationTime":1533197269945}]}
이 형태이다.
0.pom.xml에 라이브러리 등록
0.pom.xml에 라이브러리 등록
1
2
3
4
5
6
7
|
<!-- https://mvnrepository.com/artifact/com.fasterxml.jackson.core/jackson-databind -->
<dependency>
<groupId>com.fasterxml.jackson.core</groupId>
<artifactId>jackson-databind</artifactId>
<version>2.9.5</version>
</dependency>
| cs |
- 빈 객체 생성 [GuestMessage.java]
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
|
package com.bitcampi.mvc0802;
import java.util.Date;
import javax.xml.bind.annotation.XmlAccessType;
import javax.xml.bind.annotation.XmlAccessorType;
import javax.xml.bind.annotation.XmlType;
/*GuestMessage를 Xml 파일로 뽑으려 한다*/
@XmlAccessorType(XmlAccessType.FIELD)
@XmlType(name="", propOrder= {"id", "message", "creationTime"}) //Xml property 지정하기.
public class GuestMessage {
private Integer id;
private String message;
private Date creationTime;
public GuestMessage(Integer id, String message, Date creationTime) {
super();
this.id = id;
this.message = message;
this.creationTime = creationTime;
}
public GuestMessage() {
}
public Integer getId() {
return id;
}
public void setId(Integer id) {
this.id = id;
}
public String getMessage() {
return message;
}
public void setMessage(String message) {
this.message = message;
}
public Date getCreationTime() {
return creationTime;
}
public void setCreationTime(Date creationTime) {
this.creationTime = creationTime;
}
@Override
public String toString() {
return "GuestMessage [id=" + id + ", message=" + message + ", creationTime=" + creationTime + "]";
}
}
|
(2) (1)객체를 받을 리스트 빈 객체 생성 [GuestMessageList.java]
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
|
package com.bitcampi.mvc0802;
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;
@XmlAccessorType(XmlAccessType.FIELD) //이것은 Xml타입이다.
@XmlRootElement(name="message-list") //GuestMessage도 받을 RootXml이다.
public class GuestMessageList {
@XmlElement(name="message") //반환 타입 지정
private List<GuestMessage> messages;
public GuestMessageList() {
}
public GuestMessageList(List<GuestMessage> messages) {
this.messages = messages;
}
public List<GuestMessage> getMessages() {
return messages;
}
public void setMessages(List<GuestMessage> messages) {
this.messages = messages;
}
}
|
(3) Controller 생성
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
|
package com.bitcampi.mvc0802;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
@Controller
public class GuestMessageController {
@RequestMapping("/guestMessage/list.json")
@ResponseBody
public GuestMessageList listJson() {
List<GuestMessage> messages = new ArrayList<GuestMessage>();
messages.add(new GuestMessage( 1 , "안녕", new Date()));
messages.add(new GuestMessage( 2 , "문의", new Date()));
messages.add(new GuestMessage( 3 , "긴급", new Date()));
return new GuestMessageList(messages);
}
}
|
(4) 호출하기
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
|
<%@ taglib uri="http://java.sun.com/jsp/jstl/core" prefix="c" %>
<%@ page session="false" %>
<%@ page language="java" contentType="text/html; charset=UTF-8"
pageEncoding="UTF-8"%>
<html>
<head>
<title>Home</title>
</head>
<body>
<h1>
Spring MVC 2018/08/02
</h1>
<li><a href="file" target="_brank">파일 다운로드</a></li>
<li><a href="pageRanks/xls" target="_brank">엑셀파일 다운로드</a></li>
<li><a href="pageRanks/pdf" target="_brank">PDF파일 다운로드</a></li>
<li><a href="simple" target="_brank">@RequestBody @ResponseBody</a></li>
<li><a href="guestMessage/list.xml" target="_brank">XmlList</a></li>
<li><a href="guestMessage/list.json" target="_brank">JSONList</a></li>
</body>
</html>
|
결과)
댓글 없음:
댓글 쓰기