728x90
import hello.springmvc.basic.HelloData;
import lombok.extern.slf4j.Slf4j;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.bind.annotation.ResponseStatus;
import org.springframework.web.bind.annotation.RestController;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
@Slf4j
@Controller
@ResponseBody // 클래스에 선언하면 함수에 @ResponseBody 설정 안해도 됨
// @RestController 는 @Controller랑 @ResponseBody 합친것
public class ResponseBodyController {
@GetMapping("/response-body-string-v1")
public void responseBodyV1(HttpServletResponse response) throws IOException {
response.getWriter().write("ok");
}
@GetMapping("/response-body-string-v2")
public ResponseEntity<String> responseBodyV2() {
return new ResponseEntity<>("ok", HttpStatus.OK);
}
@ResponseBody
@GetMapping("/response-body-string-v3")
public String responseBodyV3() {
return "ok";
}
@GetMapping("/response-body-json-v1")
public ResponseEntity<HelloData> responseBodyJsonV1() {
HelloData helloData = new HelloData();
helloData.setUsername("userA");
helloData.setAge(20);
return new ResponseEntity<>(helloData, HttpStatus.OK);
}
@ResponseStatus(HttpStatus.OK)
@ResponseBody
@GetMapping("/response-body-json-v2")
public HelloData responseBodyJsonV2() {
HelloData helloData = new HelloData();
helloData.setUsername("userA");
helloData.setAge(20);
return helloData;
}
}
728x90
'백엔드 > Spring(Boot)' 카테고리의 다른 글
스프링 메시지, 국제화 (0) | 2021.09.23 |
---|---|
스프링 리다이렉트 (0) | 2021.09.11 |
스프링 정적 리소스, 뷰 템플릿 (0) | 2021.09.10 |
HTTP 요청 메시지 (JSON) (0) | 2021.09.10 |
HTTP 요청 메시지 (단순 텍스트) (0) | 2021.09.09 |