1. createSchedule
- scheduleController
package com.example.scheduleappserver.controller;
import com.example.scheduleappserver.dto.ScheduleRequestDto;
import com.example.scheduleappserver.dto.ScheduleResponseDto;
import com.example.scheduleappserver.entity.Schedule;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import java.util.HashMap;
import java.util.Map;
@RestController
@RequestMapping("/api")
public class ScheduleController {
private final Map<Long, Schedule> scheduleMap = new HashMap<>();
@PostMapping("/schedule")
public ScheduleResponseDto createSchedule(@RequestBody ScheduleRequestDto requestDto){
// requestDto받은 것으로 schedule을 새로 만듬
Schedule schedule = new Schedule(requestDto);
// id 부분 처리를 어떻게 해야될지 모르겟다
// scheduleMap에 id와 schedule entity 넣음
scheduleMap.put(schedule.getId(), schedule);
ScheduleResponseDto responseDto = new ScheduleResponseDto(schedule);
return responseDto;
}
}
- Schedule
package com.example.scheduleappserver.entity;
import com.example.scheduleappserver.dto.ScheduleRequestDto;
import lombok.Getter;
import lombok.Setter;
import java.util.Date;
@Getter
@Setter
public class Schedule {
private Long id;
private Integer password;
private String title;
private String content;
private String manager;
// private Date createdAt;
}
- ScheduleResopneseDto
package com.example.scheduleappserver.dto;
import com.example.scheduleappserver.entity.Schedule;
import lombok.Getter;
import java.util.Date;
@Getter
public class ScheduleResponseDto {
private Long id;
private Integer password;
private String title;
private String content;
private String manager;
// private Date createdAt;
}
- ScheduleRequestDto
package com.example.scheduleappserver.dto;
import lombok.Getter;
import java.util.Date;
@Getter
public class ScheduleRequestDto {
private Long id;
private Integer password;
private String title;
private String content;
private String manager;
// private Date createdAt;
}
첫번째 문제점
더보기


Resolved [org.springframework.http.converter.HttpMessageNotReadableException: Required request body is missing
- 바디 부분이 존재하지 않아서 400 오류
- Body에 raw Json형식으로 추가해줘서 오류해결
두번째 문제점
더보기


오류는 나지 않았지만 response된 데이터 값이 null값으로 나옴.
- RequestDto와 매핑이 되지않아서 오류가 발생했다!!!
- 그래서 Schedule과 ScheduleResponseDto에 매핑해주는 코드 추가
public ScheduleResponseDto(Schedule schedule) {
this.id = schedule.getId();
this.password = schedule.getPassword();
this.title = schedule.getTitle();
this.content = schedule.getContent();
this.manager = schedule.getManager();
}
'TIL' 카테고리의 다른 글
| 240521 TIL : REST, REST API, RESTful (0) | 2024.05.22 |
|---|---|
| 240520 TIL (0) | 2024.05.21 |
| 240514 TIL (0) | 2024.05.14 |
| 240510 TIL (0) | 2024.05.10 |
| 240509 TIL (0) | 2024.05.10 |