
์ฟผ๋ฆฌ ํ๋ผ๋ฏธํฐ๋ URL์ ์๋์ ๊ฐ์ด ?๋ฅผ ์์์ผ๋ก ๋ณด๋ผ ์ ์๋ค.
์ถ๊ฐ ํ๋ผ๋ฏธํฐ๋ &์ผ๋ก ๊ตฌ๋ถํ ์ ์๋ค.
http://localhost:8080/request-param?username=hello&age=20
์๋ฒ์์๋ HttpServletRequest๊ฐ ์ ๊ณตํ๋ ๋ฉ์๋๋ฅผ ํตํด ์ฟผ๋ฆฌ ํ๋ผ๋ฏธํฐ๋ฅผ ํธ๋ฆฌํ๊ฒ ์กฐํํ ์ ์๋ค.
String username = request.getParameter("username"); //๋จ์ผ ํ๋ผ๋ฏธํฐ ์กฐํ
Enumeration<String> parameterNames = request.getParameterNames(); //ํ๋ผ๋ฏธํฐ ์ด๋ฆ๋ค ๋ชจ๋ ์กฐํ
Map<String, String[]> parameterMap = request.getParameterMap(); //ํ๋ผ๋ฏธํฐ๋ฅผ Map์ผ๋ก ์กฐํ
String[] usernames = request.getParameterValues("username"); //๋ณต์ ํ๋ผ๋ฏธํฐ ์กฐํ
username=hello&username=kim&username=dong์ผ ๊ฒฝ์ฐ ์ด๋ป๊ฒ ์กฐํ๋ฅผ ํด์ผํ ๊น?request.getParameter๋ ํ๋์ ํ๋ผ๋ฏธํฐ ์ด๋ฆ์ ๋ํด์ ๋จ ํ๋์ ๊ฐ๋ง ์์ ๋ ์ฌ์ฉํด์ผ ํ๋ค.- ์ค๋ณต์ผ ๊ฒฝ์ฐ
request.getParameter()๋ฅผ ์ฌ์ฉํ๋ฉดrequest.getParameterValues()์ ์ฒซ ๋ฒ์งธ ๊ฐ์ ๋ฐํํ๋ค.
<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<title>Title</title>
</head>
<body>
<form action="/request-param" method="post">
username: <input type="text" name="username" />
age: <input type="text" name="age" />
<button type="submit">์ ์ก</button>
</form>
</body>
</html>
ํน์ง
application/x-www.form-urlencodedusername=hello&age=20์ ๋ฆฌ
request.getParameter()๋ GET URL ์ฟผ๋ฆฌ ํ๋ผ๋ฏธํฐ ํ์๋ ์ง์, POST HTML Form ํ์๋ ์ง์ํ๋ค.์ฐธ๊ณ
- content-type์ HTTP ๋ฉ์์ง ๋ฐ๋์ ๋ฐ์ดํฐ ํ์์ ์ง์
- GET URL ์ฟผ๋ฆฌ ํ๋ผ๋ฏธํฐ ํ์์ผ๋ก ํด๋ผ์ด์ธํธ์์ ์๋ฒ๋ก ๋ฐ์ดํฐ๋ฅผ ์ ๋ฌํ ๋๋ HTTP ๋ฉ์์ง ๋ฐ๋๋ฅผ ์ฌ์ฉํ์ง ์๊ธฐ ๋๋ฌธ์ content-type์ด ์๋ค.
- POST HTML Form ํ์์ผ๋ก ๋ฐ์ดํฐ๋ฅผ ์ ๋ฌํ๋ฉด HTTP ๋ฉ์์ง ๋ฐ๋์ ํฌํจ๋ ๋ฐ์ดํฐ๊ฐ ์ด๋ค ํ์์ธ์ง content-type์ ๊ผญ ์ง์ ํด์ผ ํ๋ค.
- ์ด๋ ๊ฒ ํผ์ผ๋ก ๋ฐ์ดํฐ๋ฅผ ์ ์กํ๋ ํ์์
application/x-www-form-urlencoded๋ผ๊ณ ํ๋ค.
์ฐธ๊ณ
- inputStream์ byte ์ฝ๋๋ฅผ ๋ฐํํ๋ค. byte ์ฝ๋๋ฅผ ์ฐ๋ฆฌ๊ฐ ์ฝ์ ์ ์๋ ๋ฌธ์(String)๋ก ๋ณด๋ ค๋ฉด ๋ฌธ์ํ (Charset)๋ฅผ ์ง์ ํด์ฃผ์ด์ผ ํ๋ค.
- ์์ฆ์
euc-kr๋ณด๋คutf-8ํํ๋ก ์ธ์ฝ๋ฉ ํ๋ ๊ฒฝ์ฐ๊ฐ ๋ง์์utf-8์ ์ฌ์ฉํ์.
JSON ํ์ ์ ์ก
- POST
http://localhost:8080/request-body-json- content-type : application/json
- message body :
{"username" : "hello", "age" : 20}- ๊ฒฐ๊ณผ :
messageBody = {"username" : "hello", "age" : 20}
Json ํ์ ํ์ฑ ์ถ๊ฐ
import lombok.Getter;
import lombok.Setter;
@Getter @Setter
public class HelloData {
private String username;
private int age;
}
Json ํํ๋ก ์์ฒญ์ด ์จ๊ฒ์ ํ์ธํ๋ ค๋ฉด inputStream์ ์ฌ์ฉํ๋ฉด๋๋ค.
request๋ฅผ inputStream์ผ๋ก ๋ฐ์ผ๋ฉด ์ด๋ฐ์์ผ๋ก ์ค๊ฒ๋๋ค.
{messageBody = {"username" : "hello", "age" : 20}}
Jsonํ์์ ์์ฒญ์ ๊ฐ์ฒด์ ํํ๋ก ๋ฐ์ผ๋ ค๋ฉด?
์ฐธ๊ณ
- JSON ๊ฒฐ๊ณผ๋ฅผ ํ์ฑํด์ ์ฌ์ฉํ ์ ์๋ ์๋ฐ ๊ฐ์ฒด๋ก ๋ณํํ๋ ค๋ฉด Jaskson, Gson๊ฐ์ JSON ๋ณํ ๋ผ์ด๋ธ๋ฌ๋ฆฌ๋ฅผ ์ถ๊ฐํด์ ์ฌ์ฉํด์ผ ํ๋ค.
- ์คํ๋ง ๋ถํธ๋ก Spring MVC๋ฅผ ์ ํํ๋ฉด ๊ธฐ๋ณธ์ผ๋ก Jackson ๋ผ์ด๋ธ๋ฌ๋ฆฌ(ObjectMapper)๋ฅผ ํจ๊ป ์ ๊ณตํ๋ค. -> ์ด๊ฑฐ ์ฌ์ฉํ๋ฉด ์๋ฐ ๊ฐ์ฒด๋ก ์ฝ๊ฒ ๋ณํํ ์ ์๋ค.
// Jackson ๋ผ์ด๋ธ๋ฌ๋ฆฌ ์ฌ์ฉ
private ObjectMapper objectMapper = new ObjectMapper();
HelloData helloData = objectMapper.readValue(messageBody, HelloData.class);
System.out.println("helloData.username: " + helloData.getUsername());
System.out.println("helloData.age: " + helloData.getAge());
//๊ฐ์ฒด๋ก ๋ณํํ ์ฐ์ด๋ณธ ๊ฒฐ๊ณผ
helloData.username: hello
helloData.age: 20