Spring controller data types

HttpServletRequest

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17

function httpServletRequestSample() {
let data = {
"name" : "kidongyun",
"age" : 26
}

$.ajax({
url:'/httpServletRequestSample',
type:'get',
data: data,
success:function(){

}
})
}

1
2
3
4
5
6
7
8
9
10

@RequestMapping(value = "/httpServletRequestSample")
public void httpServletRequestSample(HttpServletRequest request){

String name = request.getParameter("name");
String age = request.getParameter("age");

System.out.println("HttpServletRequest(Object -> Primitive) : " + name + ", " + age);
}

@RequestParam

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17

function requestParamSample() {
let data = {
"name" : "kidongyun",
"age" : 26
}

$.ajax({
url:'/requestParamSample',
type:'get',
data: data,
success:function(){

}
})
}

1
2
3
4
5
6

@RequestMapping(value = "/requestParamSample")
public void requestParamSample(@RequestParam String name, @RequestParam int age) {
System.out.println("RequestParam(Object -> Primitive) : " + name + ", " + age);
}

@RequestBody Map

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18

function requestBodyMapTypeSample() {
let data = {
"name" : "kidongyun",
"age" : 26
}

$.ajax({
url:'/requestBodyMapTypeSample',
type:'post',
contentType: 'application/json',
data: JSON.stringify(data),
success:function(){

}
})
}

1
2
3
4
5
6

@RequestMapping(value = "/requestBodyMapTypeSample")
public void requestBodyMapTypeSample(@RequestBody Map<String, Object> param) {
System.out.println("RequestBody(Json -> Map) : " + param.get("name") + ", " + param.get("age"));
}

@RequestBody VO

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18

function requestBodyVOTypeSample() {
let data = {
"name" : "kidongyun",
"age" : 26
}

$.ajax({
url:'/requestBodyVOTypeSample',
type:'post',
contentType: 'application/json',
data: JSON.stringify(data),
success:function(){

}
})
}

1
2
3
4
5
6

@RequestMapping(value = "/requestBodyVOTypeSample")
public void requestBodyVOTypeSample(@RequestBody Human human) {
System.out.println("RequestBody(Json -> VO) : " + human.getName() + ", " + human.getAge());
}

ModelAndView

1
2
3
4
5
6
7
8
9
10
11
12

<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<html>
<head>
<title>ModelAndViewSamplePage</title>
</head>
<body>
${name}
${age}
</body>
</html>

1
2
3
4
5
6
7
8
9
10
11
12

@RequestMapping(value = "/modelAndViewSample")
public ModelAndView modelAndViewSample() {
ModelAndView view = new ModelAndView();

view.addObject("name", "kidongyun");
view.addObject("age", 26);
view.setViewName("/modelAndViewSamplePage");

return view;
}

ViewResolver

1
2
3
4
5
6
7
8
9
10
11

<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<html>
<head>
<title>ViewResolverSample</title>
</head>
<body>
ViewResolverSample
</body>
</html>

1
2
3
4
5
6

@RequestMapping(value = "/viewResolverSample")
public String viewResolverSample() {
return "viewResolverSample";
}

HttpServletResponse

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15

@RequestMapping(value = "/httpServletResponseSample")
public void httpServletResponseSample(HttpServletResponse response) throws ServletException, IOException {
response.setContentType("text/html;charset=UTF-8");
PrintWriter out = response.getWriter();

out.println("<html>");
out.println("<head><title>httpServletResponseSample</title></head>");
out.println("<body>");
out.println("name : kidongyun");
out.println("age : 26");
out.println("</body>");
out.println("</html>");
}

@ResponseBody

1
2
3
4
5
6
7
8
9
10
11

function responseBodyStringTypeSample() {
$.ajax({
url:'/responseBodyStringTypeSample',
type:'get',
success:function(data){
console.log(data);
}
})
}

1
2
3
4
5
6
7

@ResponseBody
@RequestMapping(value = "/responseBodyStringTypeSample")
public String responseBodyStringTypeSample() {
return "It's plain String Type";
}

Share