๐Ÿ“Œ Spring @RestController ํ™œ์šฉ ๋ฐ JSON/XML ๋ฐ˜ํ™˜ ์˜ˆ์ œ

My Pale Blue Dotยท2025๋…„ 4์›” 28์ผ
0

SPRING

๋ชฉ๋ก ๋ณด๊ธฐ
21/36
post-thumbnail

๐Ÿ“Œ Spring @RestController ํ™œ์šฉ ๋ฐ JSON/XML ๋ฐ˜ํ™˜ ์˜ˆ์ œ

๐Ÿ“… ๋‚ ์งœ

2025-04-28


๐Ÿ“ ํ•™์Šต ๋‚ด์šฉ

1๏ธโƒฃ @RestController ๊ฐœ๋…

  • @RestController๋Š” ์Šคํ”„๋ง์—์„œ RESTful API๋ฅผ ๊ตฌํ˜„ํ•  ๋•Œ ์‚ฌ์šฉํ•˜๋Š” ์–ด๋…ธํ…Œ์ด์…˜.
  • ๋ฐ˜ํ™˜ ๋ฐ์ดํ„ฐ๋ฅผ View๊ฐ€ ์•„๋‹Œ, HTTP Response Body๋กœ ์ง์ ‘ ์ „์†ก.
  • ์ฃผ๋กœ JSON, XML, Plain Text ํ˜•ํƒœ๋กœ ์‘๋‹ต.

2๏ธโƒฃ pom.xml ์˜์กด์„ฑ ์„ค์ •

<!-- RESTCONTROLLER ๊ด€๋ จ Jackson ๋ผ์ด๋ธŒ๋Ÿฌ๋ฆฌ -->
<dependency>
    <groupId>com.fasterxml.jackson.core</groupId>
    <artifactId>jackson-databind</artifactId>
    <version>2.19.0</version>
</dependency>

<dependency>
    <groupId>com.fasterxml.jackson.dataformat</groupId>
    <artifactId>jackson-dataformat-xml</artifactId>
    <version>2.19.0</version>
</dependency>

<dependency>
    <groupId>com.fasterxml.jackson.core</groupId>
    <artifactId>jackson-core</artifactId>
    <version>2.19.0</version>
</dependency>

<dependency>
    <groupId>com.fasterxml.jackson.datatype</groupId>
    <artifactId>jackson-datatype-jsr310</artifactId>
    <version>2.19.0</version>
</dependency>
  • ์„ค๋ช…
    • JSON, XML ๋ณ€ํ™˜์„ ์œ„ํ•œ ํ•„์ˆ˜ ๋ผ์ด๋ธŒ๋Ÿฌ๋ฆฌ.
    • jackson-datatype-jsr310์€ Java 8์˜ LocalDateTime ์ง€์›.

3๏ธโƒฃ RestController ์˜ˆ์ œ ์ฝ”๋“œ ๋ฐ ํ˜ธ์ถœ ๋ฐฉ๋ฒ•

์ปจํŠธ๋กค๋Ÿฌ ๊ธฐ๋ณธ ๊ฒฝ๋กœ:

@RequestMapping("/rest")

โ‡’ ๋ชจ๋“  API ํ˜ธ์ถœ ์‹œ /rest๋กœ ์‹œ์ž‘


โœ… 1. ํ…์ŠคํŠธ ๋ฐ˜ํ™˜ ์˜ˆ์ œ

@GetMapping(value = "/getText", produces = MediaType.TEXT_PLAIN_VALUE)
public String f1() {
    return "HELLO WORLD";
}
  • ํ˜ธ์ถœ URL
    GET http://localhost:8080/rest/getText
    
  • ์‘๋‹ต ํƒ€์ž…: text/plain
  • ์‘๋‹ต ๊ฒฐ๊ณผ:
    HELLO WORLD
    

โœ… 2. JSON ๋ฐ˜ํ™˜ ์˜ˆ์ œ

@GetMapping(value = "/getJson", produces = MediaType.APPLICATION_JSON_UTF8_VALUE)
public MemoDto f2() {
    return new MemoDto(11, "ABCD", "FFFF", LocalDateTime.now());
}
  • ํ˜ธ์ถœ URL
    GET http://localhost:8080/rest/getJson
    
  • ์‘๋‹ต ํƒ€์ž…: application/json
  • ์˜ˆ์‹œ ์‘๋‹ต
{
  "id": 11,
  "text": "ABCD",
  "writer": "FFFF",
  "createAt": [2025, 4, 28, 10, 39, 23, 310212300]
}

โœ… 3. ๋‹จ์ผ XML ๋ฐ˜ํ™˜ ์˜ˆ์ œ

@GetMapping(value = "/getXml", produces = MediaType.APPLICATION_XML_VALUE)
public MemoDto f3() {
    return new MemoDto(11, "ABCD", "FFFF", LocalDateTime.now());
}
  • ํ˜ธ์ถœ URL
    GET http://localhost:8080/rest/getXml
    
  • ์‘๋‹ต ํƒ€์ž…: application/xml
  • ์˜ˆ์‹œ ์‘๋‹ต
<MemoDto>
    <id>11</id>
    <text>ABCD</text>
    <writer>FFFF</writer>
    <createAt>2025</createAt>
    ...
</MemoDto>

โœ… 4. XML ๋ฆฌ์ŠคํŠธ ๋ฐ˜ํ™˜ ์˜ˆ์ œ

@GetMapping(value = "/getXmlList", produces = MediaType.APPLICATION_XML_VALUE)
public List<MemoDto> f4() {
    // ๋ฆฌ์ŠคํŠธ ์ƒ์„ฑ ํ›„ ๋ฐ˜ํ™˜
}
  • ํ˜ธ์ถœ URL
    GET http://localhost:8080/rest/getXmlList
    
  • ์‘๋‹ต ํƒ€์ž…: application/xml
  • ์˜ˆ์‹œ ์‘๋‹ต (์ผ๋ถ€)
<List>
  <item>
    <id>0</id>
    <text>A0</text>
    ...
  </item>
  <item>
    <id>1</id>
    <text>A1</text>
    ...
  </item>
</List>

โœ… 5. ResponseEntity ํ™œ์šฉ - ์กฐ๊ฑด๋ถ€ XML ๋ฐ˜ํ™˜

@GetMapping(value = "/getXmlList2/{show}", produces = MediaType.APPLICATION_XML_VALUE)
public ResponseEntity<List<MemoDto>> f5(@PathVariable("show") boolean show) {
    // ์กฐ๊ฑด๋ถ€ ๋ฐ˜ํ™˜
}
  • ํ˜ธ์ถœ URL
    GET http://localhost:8080/rest/getXmlList2/true
    GET http://localhost:8080/rest/getXmlList2/false
    
  • ์„ค๋ช…
    • /true : HTTP 200 OK + XML ๋ฆฌ์ŠคํŠธ ๋ฐ˜ํ™˜
    • /false : HTTP 502 Bad Gateway ๋ฐ˜ํ™˜

โœ… 6. ์ผ๋ฐ˜ Controller + @ResponseBody ์‚ฌ์šฉ

java

@GetMapping("/home/test")
@ResponseBody
public String test() {
    return "{\"message\":\"REST DATA TEST\"}";
}
  • ์„ค๋ช…: ๊ธฐ์กด @Controller์—์„œ๋„ ๋ฉ”์„œ๋“œ ๋‹จ์œ„๋กœ REST ๋ฐฉ์‹ ๊ตฌํ˜„ ๊ฐ€๋Šฅ.
  • URL ํ˜ธ์ถœ: GET http://localhost:8080/home/test
  • ์‘๋‹ต:
json
๋ณต์‚ฌํŽธ์ง‘
{"message":"REST DATA TEST"}

๐Ÿ”ฅ ์ •๋ฆฌ

  • API ํ˜ธ์ถœ ์‹œ URL ํŒจํ„ด๊ณผ produces ์„ค์ •์— ๋”ฐ๋ผ ๋‹ค์–‘ํ•œ ํ˜•์‹์œผ๋กœ ์‘๋‹ต ๊ฐ€๋Šฅ.
  • ๋ธŒ๋ผ์šฐ์ €, Postman, curl ๋“ฑ์„ ํ™œ์šฉํ•ด ํ…Œ์ŠคํŠธ.
  • ResponseEntity๋ฅผ ํ†ตํ•ด ์ƒํƒœ ์ฝ”๋“œ๊นŒ์ง€ ์„ธ๋ฐ€ํ•˜๊ฒŒ ์ œ์–ด.
  • JSON, XML ๋ณ€ํ™˜ ์‹œ ๋‚ ์งœ ํฌ๋งท์— ์ฃผ์˜.

๐Ÿ”— ์ฐธ๊ณ  ์ž๋ฃŒ


profile
Here, My Pale Blue.๐ŸŒ

0๊ฐœ์˜ ๋Œ“๊ธ€