Spring/웹 개발 101
2.2.4 매개변수를 넘겨받는 방법
cosmohoo
2022. 4. 8. 01:17
반응형
@PathVariable 을 사용하면 됩니다.
package com.example.demo.controller;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
@RestController
@RequestMapping("test") //resource
public class TestController {
@GetMapping("/{id}")
public String testControllerWithPathVariables(@PathVariable(required = false) int id)
{
return "Hello World! id : " +id;
}
}
=> required가 false이므로 굳이 값을 쓰지 않아도 정상적으로 출력됩니다. (물론 id의 값은 안나오겠지만요)
=> 책은 이렇게 써있는데 왜 제가 쓰면 에러가 날까요?
@RequestParam을 사용하면 됩니다.
-?id={id}와 같이 매개변수로 넘어오는 값을 변수로 받을 수 있습니다.
package com.example.demo.controller;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
@RestController
@RequestMapping("test") //resource
public class TestController {
@GetMapping("/testRequestParam")
public String testControllerRequestParama(@RequestParam(required = false) int id)
{
return "Hello World! id : " +id;
}
}
@RequestBody을 사용하면 됩니다.
-반환하고자 하는 리소스가 복잡할 때 사용합니다. EX) JSON
package com.example.demo.controller;
import com.example.demo.dto.ResponseDTO;
import com.example.demo.dto.TestRequestBodyDTO;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import java.util.ArrayList;
import java.util.List;
@RestController
@RequestMapping("test") //resource
public class TestController {
@GetMapping("/testRequestBody")
public String testControllerRequestBody(@RequestBody TestRequestBodyDTO TestRequestBodyDTO)
{
return "Hello World! id " +TestRequestBodyDTO.getId() + " Message : "+ TestRequestBodyDTO.getMessage();
}
}
package com.example.demo.dto;
import lombok.Data;
@Data
public class TestRequestBodyDTO {
private int id;
private String message;
}
@RestController : @Controller + @ResponseBody
package com.example.demo.controller;
import com.example.demo.dto.ResponseDTO;
import com.example.demo.dto.TestRequestBodyDTO;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import java.util.ArrayList;
import java.util.List;
@RestController
@RequestMapping("test") //resource
public class TestController {
@GetMapping("/testResponseBody")
public ResponseDTO<String> testControllerResponseBody(@RequestBody TestRequestBodyDTO TestRequestBodyDTO)
{
List<String> list = new ArrayList<>();
list.add("Hello World! I'm a ResponseDTO");
ResponseDTO<String> response = ResponseDTO.<String>builder().data(list).build();
return response;
}
}
@ResponseEntity
- HTTP 응답의 바디뿐만 아니라 여러 다른 매개변수들, 예를 들어 status나 header를 조작하고 싶을 때 사용합니다.
package com.example.demo.controller;
import com.example.demo.dto.ResponseDTO;
import com.example.demo.dto.TestRequestBodyDTO;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import java.util.ArrayList;
import java.util.List;
@RestController
@RequestMapping("test") //resource
public class TestController {
@GetMapping("/testResponseEntity")
public ResponseEntity<?> testControllerResponseEntity()
{
List<String> list = new ArrayList<>();
list.add("Hello World! I'm a ResponseEntity & you got 400");
ResponseDTO<String> response = ResponseDTO.<String>builder().data(list).build();
//http status를 400으로 설정
return ResponseEntity.badRequest().body(response);
}
}
=> responseDTO를 리턴한것과 비교했을 때 BODY에는 아무런 차이가 없다. 단지 헤더와 HTTP Status를 조작할 수 있다는 점만이 다르다.
package com.example.demo.controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
@RestController
@RequestMapping("todo")
public class TodoController {
//testTodo메서드 작성하기
}
반응형