<form action="http://localhost:8080/requestParam" method="GET">
<input type="text" name="number">
<input type="text" name="name">
<button type = "submit">requestParam</button>
</form>
@GetMapping("/requestParam")
public ResponseEntity<Dto> requestParamGet(@RequestParam(name="number") int number,
@RequestParam(name = "name") String name){
Dto dto = new Dto(number, name);
return ResponseEntity.ok(dto);
}
queryString의 GET방식 뿐만 아니라 body에 데이터를 담아오는 POST, 그 외의 PUT, DELETE에도 모두 사용 가능합니다.
<form action="http://localhost:8080/requestParamPost" method="POST">
<input type="text" name="number">
<input type="text" name="name">
<button type = "submit">requestParamPost</button>
</form>
@PostMapping("/requestParamPost")
public ResponseEntity<Dto> requestParamPost(@RequestParam(name="number") int number,
@RequestParam(name = "name") String name){
Dto dto = new Dto(number, name);
return ResponseEntity.ok(dto);
}
Map형식으로 넘어오는 모든 데이터를 한번에 처리하는 것도 가능합니다.
@GetMapping("/requestParamAll")
public ResponseEntity<Dto> requestParamAll(@RequestParam Map<String,Object> allParameters){
for (String key : allParameters.keySet()) {
System.out.println(key +"||"+allParameters.get(key));
}
Dto dto = new Dto((int) allParameters.get("number"), (String) allParameters.get("name"));
return ResponseEntity.ok(dto);
}
@PostMapping("/requestParamAllPost")
public ResponseEntity<Dto> requestParamAllPost(@RequestParam Map<String,Object> allParameters){
for (String key : allParameters.keySet()) {
System.out.println(key +"||"+allParameters.get(key));
}
Dto dto = new Dto((int) allParameters.get("number"), (String) allParameters.get("name"));
return ResponseEntity.ok(dto);
}
queryString이 아닌 url 경로로 넘어온 변수를 처리하기 위한 어노테이션입니다.
<form>
<input type="text" name="number" id = "pathVariableInput">
<button type = "button" onclick="document.location.href = makeUrl()">pathVariable</button>
</form>
@GetMapping("/pathVariable/{number}")
public ResponseEntity<Dto> pathVariable(@PathVariable int number){
System.out.println("number = " + number);
return ResponseEntity.ok(new Dto(number,"dummyName"));
}
<form action="http://localhost:8080/requestBody" method="POST" enctype="application/json">
<input type="text" name="number">
<input type="text" name="name">
<button type = "submit">requestBody</button>
</form>
@PostMapping("/requestBody")
public ResponseEntity<Dto> requestBody(@RequestBody Dto dto){
System.out.println("dto = " + dto);
return ResponseEntity.ok(dto);
}
HttpMediaTypeNotSupportedException
발생<form action="http://localhost:8080/modelAttribute" method="POST">
<input type="text" name="number">
<input type="text" name="name">
<button type = "submit">modelAttribute</button>
</form>
@PostMapping("/modelAttribute")
public ResponseEntity<Dto> modelAttribute(@ModelAttribute Dto dto){
System.out.println("dto = " + dto);
return ResponseEntity.ok(dto);
}
@ToString
@AllArgsConstructor
@NoArgsConstructor
@Getter
@Setter
public class Dto {
private int number;
private String name;
}
http://localhost:8080?number=30&name=Lee
http://localhost:8080/30/Lee