From b0c6de3dceafd2d504326311159bf3658bb5fcf4 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=EC=9E=A5=EC=98=81=EC=9E=AC?= Date: Thu, 16 Jan 2025 17:29:46 +0900 Subject: [PATCH] =?UTF-8?q?feat:=20application/octet-stream=20=EB=B3=80?= =?UTF-8?q?=ED=99=98=EB=A1=9C=EC=A7=81=20=EC=B6=94=EA=B0=80?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../backend/global/config/web/WebConfig.java | 19 +++++++++-- .../util/OctetStreamReadMsgConverter.java | 33 +++++++++++++++++++ 2 files changed, 50 insertions(+), 2 deletions(-) create mode 100644 backend/src/main/java/org/example/backend/global/util/OctetStreamReadMsgConverter.java diff --git a/backend/src/main/java/org/example/backend/global/config/web/WebConfig.java b/backend/src/main/java/org/example/backend/global/config/web/WebConfig.java index ca3d8ab6..80f1d116 100644 --- a/backend/src/main/java/org/example/backend/global/config/web/WebConfig.java +++ b/backend/src/main/java/org/example/backend/global/config/web/WebConfig.java @@ -1,19 +1,34 @@ package org.example.backend.global.config.web; +import org.example.backend.global.util.OctetStreamReadMsgConverter; +import org.springframework.beans.factory.annotation.Autowired; import org.springframework.context.annotation.Configuration; -import org.springframework.web.method.support.HandlerMethodArgumentResolver; +import org.springframework.http.converter.HttpMessageConverter; import org.springframework.web.servlet.config.annotation.CorsRegistry; import org.springframework.web.servlet.config.annotation.WebMvcConfigurer; import java.util.List; + @Configuration public class WebConfig implements WebMvcConfigurer { + private OctetStreamReadMsgConverter octetStreamReadMsgConverter; + + @Autowired + public WebConfig(OctetStreamReadMsgConverter octetStreamReadMsgConverter) { + this.octetStreamReadMsgConverter = octetStreamReadMsgConverter; + } + + @Override + public void extendMessageConverters(List> converters) { + converters.add(octetStreamReadMsgConverter); + } + @Override public void addCorsMappings(CorsRegistry corsRegistry) { corsRegistry.addMapping("/**") .allowedOrigins("http://localhost:3000", - "http://sejong-bioconvergence-temp.s3-website.ap-northeast-2.amazonaws.com"); + "http://sejong-bioconvergence-temp.s3-website.ap-northeast-2.amazonaws.com"); } } \ No newline at end of file diff --git a/backend/src/main/java/org/example/backend/global/util/OctetStreamReadMsgConverter.java b/backend/src/main/java/org/example/backend/global/util/OctetStreamReadMsgConverter.java new file mode 100644 index 00000000..71265eb7 --- /dev/null +++ b/backend/src/main/java/org/example/backend/global/util/OctetStreamReadMsgConverter.java @@ -0,0 +1,33 @@ +package org.example.backend.global.util; + +import com.fasterxml.jackson.databind.ObjectMapper; +import java.lang.reflect.Type; +import org.springframework.beans.factory.annotation.Autowired; +import org.springframework.http.MediaType; +import org.springframework.http.converter.json.AbstractJackson2HttpMessageConverter; +import org.springframework.stereotype.Component; + +@Component +public class OctetStreamReadMsgConverter extends AbstractJackson2HttpMessageConverter { + @Autowired + public OctetStreamReadMsgConverter(ObjectMapper objectMapper) { + super(objectMapper, MediaType.APPLICATION_OCTET_STREAM); + } + + // 기존 application/octet-stream 타입을 쓰기로 다루는 메시지 컨버터가 이미 존재 (ByteArrayHttpMessageConverter) + // 따라서 해당 컨버터는 쓰기 작업에는 이용하면 안됨 + @Override + public boolean canWrite(Class clazz, MediaType mediaType) { + return false; + } + + @Override + public boolean canWrite(Type type, Class clazz, MediaType mediaType) { + return false; + } + + @Override + protected boolean canWrite(MediaType mediaType) { + return false; + } +} \ No newline at end of file