[spring] error : unsupported Media type

jiyeong·2025년 1월 23일
post-thumbnail

레시피의 글과 사진을 단계별로 포스팅하려는 CRUD API를 구현하는중에 발생한 에러다.

처음에는 글과 사진을 하나의 객체로 묶어서 requestParam으로 전달받으려고 했다.

RecipeDto.java

public class RecipeDto {
    private String title; // 요리 이름
    private String userID; // 작성자 이름
    private List<RecipeStepDto> steps; // 단계별 설명 및 사진 리스트

RecipeStepDto.java

@Data
@NoArgsConstructor 
@AllArgsConstructor
public class RecipeStepDto {
    @NotNull
    private String description;

    private String photoFile;

}

recipeController.java

  @PostMapping("/recipe")
    public ResponseEntity<String> uploadRecipe(
            @RequestPart(value = "recipe" , required = false) RecipeDto recipeDto,  // JSON 데이터

recipe라는 request 객체 내부에서 [{설명1 ,사진1},{설명2 ,사진2}] 으로 이미지파일도 객체내부에 포함하여 전달받으려고 했는데, 실패했다

자바에서는 파일을 multipartFile 타입으로 일반 객체와 분리되어 param으로 전달받을 수 있는 것 같았다.

그래서 multipartFile과 객체를 분리하고,
<recipeController.java>

  @PostMapping("/recipe")
    public ResponseEntity<String> uploadRecipe(
            @RequestPart(value = "recipe" , required = false) RecipeDto recipeDto,  // JSON 데이터
            @RequestParam("files") List<MultipartFile> files) {  // 업로드 파일들

photoFile 를 삭제하여 description에 대한 배열만 객체로 받도록 수정했다
<RecipeStepDto.java>

@Data
@NoArgsConstructor 
@AllArgsConstructor
public class RecipeStepDto {
    @NotNull
    private String description;
}

하지만

unsupported Media Type error 발생.

확인해보니 json 타입임을 명시하지 않아서 발생하는 오류!

해결

포스트맨에 Content-Type 칼럼 에 application/json 추가.

profile
꿈꾸는 개발자

0개의 댓글