AbstractJackson2HttpMessageConverter

Seung jun Cha·2024년 7월 22일
0

1. AbstractJackson2HttpMessageConverter

  • JSON 데이터를 직렬화하거나 역직렬화하는 데 사용되는 메시지 컨버터의 기본 구현체

1. MultipartFile와 DTO를 한 번에 받을 수 있는 요청

  • 부모 클래스 AbstractJackson2HttpMessageConverter의 생성자를 호출하여 초기화합니다.

  • objectMapper: Jackson 라이브러리의 ObjectMapper 객체를 전달합니다. 이는 JSON 데이터의 직렬화 및 역직렬화에 사용됩니다.

  • MediaType.APPLICATION_OCTET_STREAM: 이 컨버터가 처리할 미디어 타입을 지정합니다. application/octet-stream은 일반적으로 이진 데이터 스트림을 의미합니다.

 @PostMapping(consumes = {
            MediaType.APPLICATION_JSON_VALUE,
            MediaType.MULTIPART_FORM_DATA_VALUE
    })
    public ResponseEntity<BlogUserDto.BlogUserResponse> signUp(
            @RequestPart @Valid BlogUserDto.IndividualSignupRequestDto request,
            @RequestPart(required = false) MultipartFile file
    ) {
        boolean isEmailVerified = smtpEmailService.isEmailVerified(request.email());
        if (!isEmailVerified) {
            throw new EmailAuthenticationException(request.email());
        }

        BlogUserDto.BlogUserResponse blogUserResponse = blogUserService.individualGeneralSignUp(request, file);
        return ResponseEntity.ok(blogUserResponse);
    }
HttpMediaTypeNotSupportedException: 
Content type 'application/octet-stream' not supported

dto와 MultipartFile을 함께 요청하는 경우 Content type을 확실하게 명시해야 한다.

IndividualSignupRequestDto -> Content-type: application/json
MultipartFile file -> Content-type: image/{image-extension} (ex. image/jpeg)
  • Postman이나 클라이언트에서는 올바르게 입력받은 데이터의 Content type을 지정해서 보내기 때문에 문제를 못느낄 수도 있지만 Swagger의 경우, 파라미터마다 Content type을 지정할 수가 없다.

  • application/octet-stream 타입은 파일 업로드를 처리하기 위한 기본 MIME 타입으로 Swagger UI는 파일 업로드를 처리할 때 이 타입을 사용한다. 그래서 multipart/form-data 타입으로 들어온 요청을 타입에러로 처리하지 못한다.
    ‘application/octet-stream’ 사용을 비활성화함으로서, 에러를 방지할 수 있다.

  • application/octet-stream 미디어 타입을 처리할 수 있도록 설정되어 있지만, 실제로는 현재 구현에서 이 미디어 타입에 대해 쓰기 작업을 수행하지 않는다. 여기서 쓰기 작업이란 Java 객체에서 HTTP 응답 본문으로(JSON) 변환하는 작업을 의미합니다. 즉, 서버에서 클라이언트로 데이터를 전달하기 위한 과정입니다.

3. 직렬화와의 차이

  • 직렬화는 데이터를 변환하는 기술적인 과정이고, 쓰기 작업은 이 변환된 데이터를 HTTP 응답으로 포함시키는 프로세스이다.

0개의 댓글