Notice
Recent Posts
Recent Comments
Link
일 | 월 | 화 | 수 | 목 | 금 | 토 |
---|---|---|---|---|---|---|
1 | 2 | 3 | 4 | 5 | ||
6 | 7 | 8 | 9 | 10 | 11 | 12 |
13 | 14 | 15 | 16 | 17 | 18 | 19 |
20 | 21 | 22 | 23 | 24 | 25 | 26 |
27 | 28 | 29 | 30 | 31 |
Tags
- 에라토스테네스의 체
- endif
- scanf()
- handling
- Kafka
- 도커
- 백준
- container
- strlen()
- 2025 1회
- 카프카 서버
- fgets()
- fwrite()
- ifdef
- half-close
- signal
- bootstrap.server
- 1929
- 합격 후기
- 카운팅 정렬
- 구조체
- sizeof()
- SQLD
- 2025
- 10989
- kafka server
- EOF
- Docker
- fread()
- 정보처리기사
Archives
- Today
- Total
팥빵 먹으면서 코딩하는 블로그
controller 05 - RESPONSE 본문
- RestController를 사용한 api controller
package com.example.response.controller;
import com.example.response.dto.User;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.*;
@RestController
@RequestMapping("/api")
public class ApiController {
/*
걍 text
-get
*/
//TEXT
@GetMapping("/text")
public String text(@RequestParam String account){
return account;
}
/*
json object를 내려주는 방법
- post
*/
//JSON
//request -> object mapper -> object -> method -> object -> object mapper -> json -> response
@PostMapping("/json")
public User json(@RequestBody User user){
return user; //client가 잘못 요청해서 400대로 내려가지 않는 이상 200 ok
}
/*
가장 명확하게 응답을 내려줄 수 있는 방법
Response Entity
*/
@PutMapping("/put")
public ResponseEntity<User> put(@RequestBody User user) {
return ResponseEntity.status(HttpStatus.CREATED).body(user);
//response entity라는 곳에 status를 명확하게 지정해줄 수도 있고,
//body에 데이터도 넣어줄 수 있는 방법 -> 이는 마찬가지로 object mapper을 통해 json으로 바뀌어져 내려간다.
}
}
- Controller를 사용한 page controller
resource -> static -> .html 파일을 return하여 웹 페이지로 보여줌
package com.example.response.controller;
import com.example.response.dto.User;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
@Controller
//controller라는 annotation은 String을 리턴하면 리소스를 찾게 되지만
//responsebody를 붙여주게 되면 html 리소스를 찾지 않고 response body 주석을 통해서 json을 객체를 내리겠단 의미
public class PageController {
@RequestMapping("/main")
public String main(){
return "main.html";
}
//ResponseEntity
//원래는 response body를 따로 안내려 주는 게 맞음. 아래 코드들은 test용으로 적어놓은 것.
@ResponseBody
@GetMapping("/user")
public User user() {
var user = new User(); //긴 이름을 가진 class들은 손이 많이 가기 때문에 var을 사용 권장
user.setName("steve");
user.setAge(10);
user.setAddress("fast campus");
return user;
}
}
- 위에서 사용된 User 객체
package com.example.response.dto;
import com.fasterxml.jackson.annotation.JsonIgnore;
import com.fasterxml.jackson.annotation.JsonInclude;
import com.fasterxml.jackson.databind.PropertyNamingStrategy;
import com.fasterxml.jackson.databind.annotation.JsonNaming;
@JsonNaming(value = PropertyNamingStrategy.SnakeCaseStrategy.class)
@JsonInclude(JsonInclude.Include.NON_NULL) //null값으로 내려지는 변수들이 body에 내려지지 않게 하는 주석
//-> 보통은 잘 안 쓰는데 null을 지원하지 않는 언어를 같이 쓰게 된다면 이걸 써야 함.
public class User {
private String name;
private int age; //입력 값이 없을 때 0이 아니라 null을 받고 싶다면 "int -> Integer"로 바꿔주면 됨.
private String phoneNumber;
private String address;
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public int getAge() {
return age;
}
public void setAge(int age) {
this.age = age;
}
public String getPhoneNumber() {
return phoneNumber;
}
public void setPhoneNumber(String phoneNumber) {
this.phoneNumber = phoneNumber;
}
public String getAddress() {
return address;
}
public void setAddress(String address) {
this.address = address;
}
@Override
public String toString() {
return "User{" +
"name='" + name + '\'' +
", age=" + age +
", phoneNumber='" + phoneNumber + '\'' +
", address='" + address + '\'' +
'}';
}
}
'study > JAVA SPRING' 카테고리의 다른 글
Object Mapper (0) | 2024.08.29 |
---|---|
controller 00. (0) | 2024.08.29 |
controller 04 - delete (0) | 2024.08.28 |
controller 03 - PUT (0) | 2024.08.28 |
controller 02 - POST (0) | 2024.08.25 |