스프링 MVC - 기본 기능

주리링·2021년 8월 20일

스프링 MVC

목록 보기
9/9

요청 매핑

요청 매핑을 만들어보자!

@RestController
public class MappingController {

    private Logger log= LoggerFactory.getLogger(getClass());

    @RequestMapping("/hello-basic")
    public String hellobasic(){
        log.info("hellobasic");
        return "ok";
    }
}

@RestController vs @Controller

@Controller는 반환 값이 String 이면 뷰 이름으로 인식된다.
그래서 뷰를 찾고 뷰가 랜더링 된다.
@RestController 는 반환 값으로 뷰를 찾는 것이 아니라, HTTP 메시지 바디에 바로 입력한다.
따라서 실행 결과로 ok 메세지를 받을 수 있다.
@ResponseBody 와 관련이 있는데, 뒤에서 더 자세히 설명한다.

@RequestMapping("/hello-basic")
/hello-basic URL 호출이 오면 이 메서드가 실행되도록 매핑한다.
대부분의 속성을 배열[] 로 제공하므로 다중 설정이 가능하다. {"/hello-basic", "/hello-go"}

HTTP 메서드 맵핑 축약

@RequestMapping에서 method = RequestMethod.GET라는 조건을 줄 수 있는데 줄여서 @GetMapping으로 바꿀 수 있다.
@GetMapping의 코드를 보면 내부에서 @RequestMapping 과 method 를 지정해서 사용하는 것을 확인할 수 있다.

@GetMapping
@PostMapping
@PutMapping
@DeleteMapping
@PatchMapping

PathVariable(경로 변수) 사용

@GetMapping("/mapping/{userId}")
public String mappingPath(@PathVariable("userId") String data) {
log.info("mappingPath userId={}", data);
return "ok";
}

최근 HTTP API는 다음과 같이 리소스 경로에 식별자를 넣는 스타일을 선호한다.
/mapping/userA
/users/1

@RequestMapping 은 URL 경로를 템플릿화 할 수 있는데, @PathVariable 을 사용하면 매칭 되는 부분을 편리하게 조회할 수 있다.
@PathVariable 의 이름과 파라미터 이름이 같으면 생략할 수 있다.

특정 파라미터 조건 매핑

잘 사용하지는 않는다.
특정 파라미터가 있거나 없는 조건을 추가할 수 있다.

@GetMapping(value = "/mapping-header", headers = "mode=debug")
public String mappingHeader() {
    log.info("mappingHeader");
    return "ok";
}

파라미터 매핑과 비슷하지만, HTTP 헤더를 사용한다.
Postman으로 테스트 해야 한다.

HTTP요청

매핑 방법을 이해했으니, 이제부터 HTTP 요청이 보내는 데이터들을 스프링 MVC로 어떻게 조회하는지 알아보자.
클라이언트에서 서버로 요청 데이터를 전달할 때는 주로 다음 3가지 방법을 사용한다.

GET - 쿼리 파라미터
/url?username=hello&age=20
메시지 바디 없이, URL의 쿼리 파라미터에 데이터를 포함해서 전달
예) 검색, 필터, 페이징등에서 많이 사용하는 방식
POST - HTML Form
content-type: application/x-www-form-urlencoded
메시지 바디에 쿼리 파리미터 형식으로 전달 username=hello&age=20
예) 회원 가입, 상품 주문, HTML Form 사용
HTTP message body에 데이터를 직접 담아서 요청
HTTP API에서 주로 사용, JSON, XML, TEXT
데이터 형식은 주로 JSON 사용
POST, PUT, PATCH

GET - 쿼리 파라미터방식과 POST - HTML Form을 이용하는 방식은 모두 HttpServletRequest 의 request.getParameter() 를 사용하면 다음 두가지 요청 파라미터를 조회할 수 있다.

HTTP 메시지 바디를 통해 데이터가 직접 데이터가 넘어오는 경우는 HTTP 메시지 바디의 데이터를 InputStream 을 사용해서 직접 읽을 수 있다.

요청 파라미터

먼저 테스트용 HTML Form을 만들어야 한다.

<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<title>Title</title>
</head>
<body>
<form action="/request-param-v1" method="post">
username: <input type="text" name="username" />
age: <input type="text" name="age" />
<button type="submit">전송</button>
</form>
</body>
</html>

request.getParameter()

@RequestMapping("/request-param-v1")
    public void requestParamV1(HttpServletRequest request, HttpServletResponse
            response) throws IOException {
        String username = request.getParameter("username");
        int age = Integer.parseInt(request.getParameter("age"));
        log.info("username={}, age={}", username, age);
        response.getWriter().write("ok");
    }

HttpServletRequest가 제공하는 방식으로 getParameter메서드를 이용하여 요청 파라미터를 조회했다.
반환 타입이 없으면서 응답(response)에 값을 직접 넣으면 view를 조회하지 않는다.

@RequestParam

@ResponseBody
    @RequestMapping("/request-param-v2")
    public String requestParamV2(
            @RequestParam("username") String memberName,
            @RequestParam("age") int memberAge) {
        log.info("username={}, age={}", memberName, memberAge);
        return "ok";
    }

스프링에서 제공하는 @RequestParam을 사용해서 매개변수에 넣으면 편리하게 사용할 수 있다.
@RequestParam의 name(value) 속성이 파라미터 이름으로 사용하며, 자료형을 임의로 설정할 수 있다.
@ResponseBody : View 조회를 무시하고, HTTP message body에 직접 해당 내용 입력한다.

HTTP 파라미터 이름이 변수 이름과 같으면 @RequestParam(name="xx") 생략 가능하다.

@ResponseBody
@RequestMapping("/request-param-v3")
public String requestParamV3(
@RequestParam String username,
@RequestParam int age) {
  log.info("username={}, age={}", username, age);
  return "ok";
}

String , int , Integer 등의 단순 타입이면 @RequestParam 도 생략 가능하다.

@ResponseBody
@RequestMapping("/request-param-v4")
public String requestParamV4(String username, int age) {
  log.info("username={}, age={}", username, age);
  return "ok";
}

requestParamRequired

@RequestParam 애노테이션을 생략하면 스프링 MVC는 내부에서 required=false 를 적용한다.
required는 파라미터의 필수 여부를 구분하는 것으로, 기본값이 true이다.

@ResponseBody
    @RequestMapping("/request-param-required")
    public String requestParamRequired(
            @RequestParam(required = true) String username,
            @RequestParam(required = false) Integer age) {
        log.info("username={}, age={}", username, age);
        return "ok";
    }

주의
파라미터의 이름만 있고 값이 없는 경우 ex)/request-param?username=
이런 경우에는 빈문자로 통과하게 된다.

또한 @RequestParam(required = false) int age 이런 경우에는 int형에 null을 입력하는 것은 불가능 하므로 500 예외가 발생한다.
따라서 null을 받을 수 있는 Integer로 변경해야한다.

requestParamDefault

 @ResponseBody
    @RequestMapping("/request-param-default")
    public String requestParamDefault(
            @RequestParam(required = true, defaultValue = "guest") String username,
            @RequestParam(required = false, defaultValue = "-1") int age) {
        log.info("username={}, age={}", username, age);
        return "ok";
    }

파라미터에 값이 없는 경우 defaultValue를 사용하면 기본 값을 적용할 수 있다.
defaultValue 는 빈 문자의 경우에도 설정한 기본 값이 적용된다!

파라미터를 Map으로 조회하기 - requestParamMap

@ResponseBody
@RequestMapping("/request-param-map")
public String requestParamMap(@RequestParam Map<String, Object> paramMap) {
log.info("username={}, age={}", paramMap.get("username"),
paramMap.get("age"));
return "ok";
}

@RequestParam Map
Map(key=value)
@RequestParam MultiValueMap
MultiValueMap(key=[value1, value2, ...] ex) (key=userIds, value=[id1, id2])
파라미터의 값이 1개가 확실하다면 Map 을 사용해도 되지만, 그렇지 않다면 MultiValueMap 을 사용하자.

HTTP 요청 파라미터 - @ModelAttribute

아래와 같이 클래스를 만든다.

@Data
public class HelloData {
    private String username;
    private int age;
}

(@Data는 @Getter , @Setter , @ToString , @EqualsAndHashCode , @RequiredArgsConstructor 를 자동으로 적용해준다.)

실제 개발을 하면 아래의 코드처럼 요청 파라미터를 받아서 필요한 객체를 만들고 그 객체에 값을 넣어주어야 한다.

HelloData data = new HelloData();
data.setUsername(username);
data.setAge(age);

이 동작들을 편리하게 해주는 @ModelAttribute에 대해서 알아보자.

@ResponseBody
    @RequestMapping("/model-attribute-v1")
    public String modelAttributeV1(@ModelAttribute HelloData helloData) {
        log.info("username={}, age={}", helloData.getUsername(),
                helloData.getAge());
        return "ok";
    }

위와 같이 코드와 같이 작성하면 스프링 MVC는 HelloData 객체를 생성하고 요청 파라미터의 이름으로 HelloData객체의 프로퍼티를 찾는다.
그리고 해당 프로퍼티의 setter를 호출해서 파라미터의 값을 입력(바인딩)한다.

프로퍼티란
객체에 getUsername() , setUsername() 메서드가 있으면, 이 객체는 username 이라는 프로퍼티를 가지고 있다.
username 프로퍼티의 값을 변경하면 setUsername() 이 호출되고, 조회하면 getUsername() 이 호출된다.

위에서 @RequestParam을 생략했던 것처럼 @ModelAttribute도 생략할 수 있다!

@ResponseBody
    @RequestMapping("/model-attribute-v2")
    public String modelAttributeV2(HelloData helloData) {
        log.info("username={}, age={}", helloData.getUsername(),
                helloData.getAge());
        return "ok";
    }

이때, @RequestParam와 @ModelAttribute의 생략으로 여러 매개변수가 있을 때 스프링은 다음과 같은 규칙을 적용한다.
String , int , Integer 같은 단순 타입 = @RequestParam
나머지 = @ModelAttribute (argument resolver 로 지정해둔 타입 외)

HTTP 요청 메시지

요청 파라미터와 다르게, HTTP 메시지 바디를 통해 데이터가 직접 데이터가 넘어오는 경우는
@RequestParam , @ModelAttribute 를 사용할 수 없다.
물론 HTML Form 형식으로 전달되는 경우는 요청 파라미터를 사용할 수 있다.

단순 텍스트

먼저 가장 단순한 텍스트 메시지를 HTTP 메시지 바디에 담아서 전송하고, 읽어보자.
HTTP 메시지 바디의 데이터를 InputStream 을 사용해서 직접 읽을 수 있다.

@Slf4j
@Controller
public class RequestBodyStringController {
    @PostMapping("/request-body-string-v1")
    public void requestBodyString(HttpServletRequest request,
                                  HttpServletResponse response) throws IOException {
        ServletInputStream inputStream = request.getInputStream();
        String messageBody = StreamUtils.copyToString(inputStream,
                StandardCharsets.UTF_8);
        log.info("messageBody={}", messageBody);
        response.getWriter().write("ok");
    }
}

HttpServletRequest과 HttpServletResponse대신 Input, Output 스트림, Reader를 매개변수로 받아서 사용 할 수 있다.

@PostMapping("/request-body-string-v2")
    public void requestBodyStringV2(InputStream inputStream, Writer responseWriter)
            throws IOException {
        String messageBody = StreamUtils.copyToString(inputStream,
                StandardCharsets.UTF_8);
        log.info("messageBody={}", messageBody);
        responseWriter.write("ok");
    }

HttpEntity를 사용해서 HTTP header, body 정보를 더 편리하게 조회할 수 있다.

@PostMapping("/request-body-string-v3")
public HttpEntity<String> requestBodyStringV3(HttpEntity<String> httpEntity) {
    String messageBody = httpEntity.getBody();
    log.info("messageBody={}", messageBody);
    return new HttpEntity<>("ok");
}

HttpEntity는 메시지 바디의 정보를 직접 조회하며, 메시지 바디 정보를 직접 반환하여 응답에서도 사용이 가능하다.
HttpEntity를 상속받은 다음 객체들도 같은 기능을 제공한다.
RequestEntity
HttpMethod, url 정보가 추가, 요청에서 사용
ResponseEntity
HTTP 상태 코드 설정 가능, 응답에서 사용
return new ResponseEntity("Hello World", responseHeaders, HttpStatus.CREATED)

@RequestBody를 사용하면 HTTP 메시지 바디 정보를 편리하게 조회할 수 있다.

@ResponseBody
@PostMapping("/request-body-string-v4")
public String requestBodyStringV4(@RequestBody String messageBody) {
    log.info("messageBody={}", messageBody);
    return "ok";
}

요청 파라미터 vs HTTP 메시지 바디

요청 파라미터를 조회하는 기능: @RequestParam , @ModelAttribute
HTTP 메시지 바디를 직접 조회하는 기능: @RequestBody

JSON

이번에는 HTTP API에서 주로 사용하는 JSON 데이터 형식을 조회해보자.

@Slf4j
@Controller
public class RequestBodyJsonController {
  private ObjectMapper objectMapper = new ObjectMapper();
  @PostMapping("/request-body-json-v1")
  public void requestBodyJsonV1(HttpServletRequest request,
                                HttpServletResponse response) throws IOException {
      ServletInputStream inputStream = request.getInputStream();
      String messageBody = StreamUtils.copyToString(inputStream,
              StandardCharsets.UTF_8);
      log.info("messageBody={}", messageBody);
      HelloData data = objectMapper.readValue(messageBody, HelloData.class);
      log.info("username={}, age={}", data.getUsername(), data.getAge());
      response.getWriter().write("ok");
  }
}

HttpServletRequest를 사용해서 직접 HTTP 메시지 바디에서 데이터를 읽어와서, 문자로 변환한다.
문자로 된 JSON 데이터를 Jackson 라이브러리인 objectMapper 를 사용해서 자바 객체로 변환한다.

@ResponseBody
  @PostMapping("/request-body-json-v2")
  public String requestBodyJsonV2(@RequestBody String messageBody) throws
          IOException {
      HelloData data = objectMapper.readValue(messageBody, HelloData.class);
      log.info("username={}, age={}", data.getUsername(), data.getAge());
      return "ok";
  }

이전에 학습했던 @RequestBody 를 사용해서 HTTP 메시지에서 데이터를 꺼내고 messageBody에 저장한다.
문자로 된 JSON 데이터인 messageBody 를 objectMapper 를 통해서 자바 객체로 변환한다.

	@ResponseBody
  @PostMapping("/request-body-json-v3")
  public String requestBodyJsonV3(@RequestBody HelloData data) {
      log.info("username={}, age={}", data.getUsername(), data.getAge());
      return "ok";
  }

@RequestBody 에 직접 만든 객체를 지정할 수 있다.
HttpEntity, @RequestBody 를 사용하면 HTTP 메시지 컨버터가 HTTP 메시지 바디의 내용을 우리가 원하는 문자나 객체 등으로 변환해준다.
HTTP 메시지 컨버터는 문자 뿐만 아니라 JSON도 객체로 변환해주는데, 우리가 방금 V2에서 했던 작업을 대신 처리해준다.
여기에서는 단순 타입이 아닌 매개변수는 @ModelAttribute가 적용되므로 @RequestBody는 생략이 불가능하다.

물론 앞서 배운 것과 같이 HttpEntity를 사용해도 된다.

	@ResponseBody
  @PostMapping("/request-body-json-v4")
  public String requestBodyJsonV4(HttpEntity<HelloData> httpEntity) {
      HelloData data = httpEntity.getBody();
      log.info("username={}, age={}", data.getUsername(), data.getAge());
      return "ok";
  }

응답의 경우에도 @ResponseBody 를 사용하면 해당 객체를 HTTP 메시지 바디에 직접 넣어줄 수 있다.
물론 이 경우에도 HttpEntity 를 사용해도 된다.

@ResponseBody
  @PostMapping("/request-body-json-v5")
  public HelloData requestBodyJsonV5(@RequestBody HelloData data) {
      log.info("username={}, age={}", data.getUsername(), data.getAge());
      return data;
  }

@RequestBody 요청
->JSON 요청 HTTP 메시지 컨버터 객체
@ResponseBody 응답
->객체 HTTP 메시지 컨버터 JSON 응답

HTTP 응답

스프링(서버)에서 응답 데이터를 만드는 방법은 크게 3가지이다.

  1. 정적 리소스
    웹 브라우저에 정적인 HTML,css,js를 제공할 때, 정적 리소스를 사용한다.

  2. 뷰 템플릿
    웹 브라우저에 동적인 HTML을 제공할 때, 뷰 템플릿을 사용한다.

  3. HTTP메시지
    HTTP API를 제공하는 경우에는 HTML이 아니라 데이터를 전달해야 하므로, HTTP 메시지 바디에 JSON같은 형식으로 데이터를 실어 보낸다.

    이 3가지 방법에 대해서 더 자세하게 알아보자

    정적 리소스

    src/main/resources 는 리소스를 보관하는 곳이고, 또 클래스패스의 시작 경로이다.
    아래와 같은 정적리소스의 경로에 리소스를 넣어두면 스프링 부트가 정적 리소스로 서비스를 제공한다.
    src/main/resources/static

    뷰 템플릿

    뷰 템플릿을 거쳐 HTML이 생성되고, 뷰가 응답을 만들어서 전달한다.
    일반적으로 HTML을 동적으로 생성하는 용도로 사용하지만, 뷰 템플릿이 만들 수 있는 것이라면 뭐든 가능하다.

    아래와 같은 뷰 템플릿의 경로에 리소스를 넣어두면 스프링 부트가 뷰를 응답으로 만들어서 전달한다.
    src/main/resources/templates
    예시를 위해 templates에 response폴더를 만들고 아래와 같은 html파일을 만든다.

    <!DOCTYPE html>
    <html xmlns:th="http://www.thymeleaf.org">
    <head>
    <meta charset="UTF-8">
    <title>Title</title>
    </head>
    <body>
    <p th:text="${data}">empty</p>
    </body>
    </html>
  
Controller를 이용하여 뷰를 랜더링 하기위해서 아래와 같은 3가지 방법을 살펴보자.

@Controller
public class ResponseViewController {
@RequestMapping("/response-view-v1")
public ModelAndView responseViewV1() {
ModelAndView mav = new ModelAndView("response/hello")
.addObject("data", "hello!");
return mav;
}
@RequestMapping("/response-view-v2")
public String responseViewV2(Model model) {
model.addAttribute("data", "hello!!");
return "response/hello";
}
@RequestMapping("/response/hello")
public void responseViewV3(Model model) {
model.addAttribute("data", "hello!!");
}
}

첫 번째 방법은 ModelAndView 객체를 만들고 addObject매서드를 이용하여 data에 값을 넣고 ModelAndView객체를 리턴한다.
두 번째 방법은 @ResponseBody가 없으면 리턴되는 String값으로 뷰 리졸버가 실행되어서 뷰를 찾고 랜더링 하도록 한다.
세 번째 방법은 void를 반환하는 경우 @Controller를 사용하고 HttpServletResponse , OutputStream(Writer) 같은 HTTP 메시지 바디를 처리하는 파라미터가 없으면 요청 URL을 참고해서 논리 뷰 이름으로 사용한다.
  이 방식은 명시성이 너무 떨어지고 설계가 쉽지 않아서 권장하지 않는다.
  
  > https://docs.spring.io/spring-boot/docs/2.4.3/reference/html/appendix-applicationproperties.
html#common-application-properties-templating
  타임리프 관련 추가설정 참고
  
  ### HTTP메시지
  HTML이나 뷰 템플릿을 사용해도 HTTP응답 메시지 바디에 HTML데이터가 담겨서 전달된다.
  여기서 설명하는 내용은 정적 리소스나 뷰 템플릿을 거치지 않고 직접 HTTP 응답 메시지를 전달하는 경우를 말한다!
  
profile
코딩하는 감자

0개의 댓글