* @RestController
์ปจํธ๋กค๋ฌ ํ๋๋ฅผ ์๋ก ์์ฑํ๋ค.
NoticeController.java
package com.newlecture.web.controller.api;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
//RESTFULํ ๊ฒฐ๊ณผ๋ฌผ์ ๋ฐํํ๋ ํด๋์ค
@RestController("apiNoticeController")
@RequestMapping("/api/notice/")
public class NoticeController {
@RequestMapping("list")
public String list() {
return "notice list";
}
}
@RestController๋ restfulํ ๊ฒฐ๊ณผ๋ฌผ์ ๋ฐํํ๋, ํนํ๋ ์ปจํธ๋กค๋ฌ๋ฅผ ์ํ ์ด๋ ธํ ์ด์ ์ด๋ค. ์ด ์ด๋ ธํ ์ด์ ์ด ์๋ค๋ฉด ๋ฉ์๋์ @ResponseBody๊ฐ ์๋๋ผ๋ ๋ฌธ์์ด์ ๊ทธ๋๋ก ๋ฐํํ๋ค.
@RestController์ ๋ถ์ "apiNoticeController"์ ์ปจํธ๋กค๋ฌ์ ์๋ณ์๋ฅผ ์๋ฏธํ๋ค. ๋ง์ฝ ์๋ณ์, ์ฆ ์ด๋ฆ์ ์ง์ ํ์ง ์๋๋ค๋ฉด ์๋ฌ๊ฐ ๋ฐ์ํ๋๋ฐ, ๊ทธ ์ด์ ๋ ๋ค๋ฅธ customer ํจํค์ง ์์ NoticeController ํด๋์ค๊ฐ ๋ ์กด์ฌํ๊ธฐ ๋๋ฌธ์ด๋ค.
์ด๋ฆ์ ์ง์ ํ์ง ์์์ ๋๋ ๋ฌต์์ ์ผ๋ก ๋ค์๊ณผ ๊ฐ์ด ๋น์ด ์์ฑ๋๋ค.
NoticeController noticeController = new NoticeController();
๋๋
<bean id="noticeController" class="ํจํค์ง.NoticeController">
ํจํค์ง๋ ๋ค๋ฅด๋๋ผ๋ ์์ฑํ๋ ๊ฐ์ฒด์ ์ด๋ฆ ๋๋ bean์ id๊ฐ ๊ฐ์ ์๊ฐ ์๊ธฐ ๋๋ฌธ์ ์๋ณ ์๋ฌ๊ฐ ๋ฐ์ํ๊ฒ ๋๋ ๊ฒ์ด๋ค. ๋ฐ๋ผ์ ์ด๋ฌํ ์๋ฌ๋ฅผ ๋ฐฉ์งํ๊ธฐ ์ํด @RestController("apiNoticeController")์ ๊ฐ์ด ์ด๋ฆ์ ๋ถ์ด๋ ๊ฒ์ด๋ค. ๋ ์ค ํ๋์๋ง ๋ถ์ฌ๋ ๋๊ณ , ๋ ๋ค ๋ถ์ฌ๋ ๋๋ค.
* ํ๊ธ ์ถ๋ ฅ ์ค์
์๋ฌธ์๋ฅผ ๋ฆฌํดํ๋ฉด ์ ์์ ์ผ๋ก ์ถ๋ ฅ๋์ง๋ง, ํ๊ธ ๋ฌธ์๋ฅผ ๋ฆฌํดํ๋ฉด ๋ฌธ์์ด์ด ๊นจ์ง๊ฒ ๋๋ค. ์ด ๊ฒฝ์ฐ xml ์ค์ ์ ์ถ๊ฐํ๋ฉด ๋๋ค.
servlet-context.xml
<!-- ์ปจํธ๋กค๋ฌ์์ RequestMapping ํ ์ ์๊ฒ ํ๋ ํ๊ทธ -->
<mvc:annotation-driven>
<mvc:message-converters>
<!--
@ResponseBody๋ก String ์ฒ๋ฆฌํ ๋ ํ๊ธ ์ฒ๋ฆฌ
๋งคํ๋ url์ ์ฐพ์๊ฐ ํ ๊ฐ์ ResponBody ํ์
์ผ๋ก ๊ฐ์ ๋ฆฌํดํ ๋,
๋ฐํํ๋ ๊ฐ์ด ํ๊ธ์ด๋ผ๋ฉด ๊ฐ๋ก์ฑ์ ํ๊ธ ์ค์ .
-->
<bean class="org.springframework.http.converter.StringHttpMessageConverter">
<property name="supportedMediaTypes">
<list>
<value>text/html;charset=UTF-8</value>
</list>
</property>
</bean>
</mvc:message-converters>
</mvc:annotation-driven>
annotation-driven ํ๊ทธ๋ฅผ ์ด๊ณ message-converters๋ฅผ ์ถ๊ฐํ๋ค. (์ด๋ฆ์์ ์ ์ ์๋ฏ์ด converter๋ ์ฌ๋ฌ ๊ฐ๊ฐ ๋ ์ ์๋ค.)
annotation-driven ์ค์ ์ผ๋ก RequestMapping์ ํตํด ๋งคํ๋ url์ ์ฐพ์๊ฐ ํ ๊ฐ์ ResponBody ํ์
์ผ๋ก ๊ฐ์ ๋ฆฌํดํ ๋, ๋ฐํํ๋ ๊ฐ์ด ํ๊ธ์ด๋ผ๋ฉด ๊ฐ๋ก์ฑ์ ํ๊ธ ์ธ์ฝ๋ฉ์ ์ค์ ํ๋ค.
'๐ปStudy > Spring' ์นดํ ๊ณ ๋ฆฌ์ ๋ค๋ฅธ ๊ธ
์ ๋ ฅ์ ์ข ๋ฅ / QueryString ์ ๋ ฅ (0) | 2021.03.04 |
---|---|
๋ฐ์ดํฐ ํํ / JSON ์ถ๋ ฅํ๊ธฐ (0) | 2021.03.03 |
๋ฌธ์ ์ถ๋ ฅ ๋ฐฉ๋ฒ 4๊ฐ์ง, @ResponseBody (0) | 2021.03.03 |
์ปจํธ๋กค๋ฌ๋ฅผ ์ํ ์ด๋ ธํ ์ด์ ๊ฐ๋ ์ ๋ฆฌ (0) | 2021.03.03 |
์ด๋ ธํ ์ด์ ์ผ๋ก ์๋น์ค ๊ฐ์ฒด ์์ฑํ๊ธฐ(@Service) / ์ปจํธ๋กค๋ฌ ์ ๋ฆฌํ๊ธฐ (@Controller) (0) | 2021.03.03 |