当前位置: 首页 > 知识库问答 >
问题:

如何解决使用RESTTemplate传递MultipartFile时“没有为类java.io.ByteArrayInputStream找到序列化程序”的错误?

江同化
2023-03-14

我试图将spring MultipartFile从我的应用程序传递到微服务,并使用RestTemplate,如下所示,

    HttpHeaders headers = new HttpHeaders();
    headers.setContentType(MediaType.MULTIPART_FORM_DATA);

    MultiValueMap<String, Object> body= new LinkedMultiValueMap<>(); 
    body.add("circularAttachment", souqBean.getCircularAttachment()); //MultipartFile 
    body.add("circularEntryId", souqBean.getCircularEntryId());
    body.add("circularTitle", souqBean.getCircularTitle());


    HttpEntity<?> entity = new HttpEntity<MultiValueMap<String, Object>>(body,headers); 


     ResponseEntity<Boolean> response =    restTemplate.postForEntity("http://localhost:8081/circular-save", entity, Boolean.class);
     status=response.getBody();

有关更多信息,下面是getcircularattachment()方法

public MultipartFile getCircularAttachment() {
        return circularAttachment;
    }
ResponseEntity<Boolean> response =    restTemplate.postForEntity("http://localhost:8081/circular-save", entity, Boolean.class);
 status=response.getBody();
org.springframework.http.converter.HttpMessageNotWritableException: Could not write JSON: No serializer found for class java.io.ByteArrayInputStream and no properties discovered to create BeanSerializer (to avoid exception, disable SerializationFeature.FAIL_ON_EMPTY_BEANS) ); nested exception is com.fasterxml.jackson.databind.JsonMappingException: No serializer found for class java.io.ByteArrayInputStream and no properties discovered to create BeanSerializer (to avoid exception, disable SerializationFeature.FAIL_ON_EMPTY_BEANS) ) (through reference chain: org.springframework.web.multipart.commons.CommonsMultipartFile["fileItem"]->org.apache.commons.fileupload.disk.DiskFileItem["inputStream"])


    at org.springframework.http.converter.json.AbstractJackson2HttpMessageConverter.writeInternal(AbstractJackson2HttpMessageConverter.java:293)
    at org.springframework.http.converter.AbstractGenericHttpMessageConverter.writeInternal(AbstractGenericHttpMessageConverter.java:115)
    at org.springframework.http.converter.AbstractHttpMessageConverter.write(AbstractHttpMessageConverter.java:227)
    at org.springframework.http.converter.FormHttpMessageConverter.writePart(FormHttpMessageConverter.java:373)
    at org.springframework.http.converter.FormHttpMessageConverter.writeParts(FormHttpMessageConverter.java:353)
    at org.springframework.http.converter.FormHttpMessageConverter.writeMultipart(FormHttpMessageConverter.java:342)
    at org.springframework.http.converter.FormHttpMessageConverter.write(FormHttpMessageConverter.java:257)
    at org.springframework.http.converter.FormHttpMessageConverter.write(FormHttpMessageConverter.java:89)
    at org.springframework.web.client.RestTemplate$HttpEntityRequestCallback.doWithRequest(RestTemplate.java:897)
    at org.springframework.web.client.RestTemplate.doExecute(RestTemplate.java:658)
    at org.springframework.web.client.RestTemplate.execute(RestTemplate.java:621)
    at org.springframework.web.client.RestTemplate.postForEntity(RestTemplate.java:415)
    at ae.gov.adm.saeed.web.controller.util.CircularsControllerUtil.saveCircularView(CircularsControllerUtil.java:390)
    at ae.gov.adm.saeed.web.controller.CircularsController.saveCircular(CircularsController.java:87)
    at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
    at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62)
    at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
    at java.lang.reflect.Method.invoke(Method.java:498)
    at org.springframework.web.method.support.InvocableHandlerMethod.doInvoke(InvocableHandlerMethod.java:205)
    at org.springframework.web.method.support.InvocableHandlerMethod.invokeForRequest(InvocableHandlerMethod.java:133)
    at org.springframework.web.servlet.mvc.method.annotation.ServletInvocableHandlerMethod.invokeAndHandle(ServletInvocableHandlerMethod.java:97)
    at org.springframework.web.servlet.mvc.method.annotation.RequestMappingHandlerAdapter.invokeHandlerMethod(RequestMappingHandlerAdapter.java:827)
    at org.springframework.web.servlet.mvc.method.annotation.RequestMappingHandlerAdapter.handleInternal(RequestMappingHandlerAdapter.java:738)
    at org.springframework.web.servlet.mvc.method.AbstractHandlerMethodAdapter.handle(AbstractHandlerMethodAdapter.java:85)
    at org.springframework.web.servlet.DispatcherServlet.doDispatch(DispatcherServlet.java:967)
    at org.springframework.web.servlet.DispatcherServlet.doService(DispatcherServlet.java:901)
    at org.springframework.web.servlet.FrameworkServlet.processRequest(FrameworkServlet.java:970)
    at org.springframework.web.servlet.FrameworkServlet.doPost(FrameworkServlet.java:872)
    at javax.servlet.http.HttpServlet.service(HttpServlet.java:660)
    at org.springframework.web.servlet.FrameworkServlet.service(FrameworkServlet.java:846)
    at javax.servlet.http.HttpServlet.service(HttpServlet.java:741)
    at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:231)
    at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:166)
    at org.apache.tomcat.websocket.server.WsFilter.doFilter(WsFilter.java:53)
    at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:193)
    at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:166)
    at ae.gov.adm.saeed.web.security.AuthFilter.doFilter(AuthFilter.java:335)
    at ae.gov.adm.saeed.web.security.AuthFilter.doFilter(AuthFilter.java:610)
    at ae.gov.adm.common.web.filter.AbstractFilter.doFilter(AbstractFilter.java:47)
    at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:193)
    at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:166)
    at org.springframework.web.filter.CharacterEncodingFilter.doFilterInternal(CharacterEncodingFilter.java:197)
    at org.springframework.web.filter.OncePerRequestFilter.doFilter(OncePerRequestFilter.java:107)
    at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:193)
    at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:166)
    at org.apache.catalina.core.StandardWrapperValve.invoke(StandardWrapperValve.java:199)
    at org.apache.catalina.core.StandardContextValve.invoke(StandardContextValve.java:96)
    at org.apache.catalina.authenticator.AuthenticatorBase.invoke(AuthenticatorBase.java:502)
    at org.apache.catalina.core.StandardHostValve.invoke(StandardHostValve.java:140)
    at org.apache.catalina.valves.ErrorReportValve.invoke(ErrorReportValve.java:81)
    at org.apache.catalina.valves.AbstractAccessLogValve.invoke(AbstractAccessLogValve.java:651)
    at org.apache.catalina.core.StandardEngineValve.invoke(StandardEngineValve.java:87)
    at org.apache.catalina.connector.CoyoteAdapter.service(CoyoteAdapter.java:342)
    at org.apache.coyote.http11.Http11Processor.service(Http11Processor.java:417)
    at org.apache.coyote.AbstractProcessorLight.process(AbstractProcessorLight.java:66)
    at org.apache.coyote.AbstractProtocol$ConnectionHandler.process(AbstractProtocol.java:754)
    at org.apache.tomcat.util.net.NioEndpoint$SocketProcessor.doRun(NioEndpoint.java:1376)
    at org.apache.tomcat.util.net.SocketProcessorBase.run(SocketProcessorBase.java:49)
    at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1149)
    at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:624)
    at org.apache.tomcat.util.threads.TaskThread$WrappingRunnable.run(TaskThread.java:61)
    at java.lang.Thread.run(Thread.java:748)
Caused by: com.fasterxml.jackson.databind.JsonMappingException: No serializer found for class java.io.ByteArrayInputStream and no properties discovered to create BeanSerializer (to avoid exception, disable SerializationFeature.FAIL_ON_EMPTY_BEANS) ) (through reference chain: org.springframework.web.multipart.commons.CommonsMultipartFile["fileItem"]->org.apache.commons.fileupload.disk.DiskFileItem["inputStream"])
    at com.fasterxml.jackson.databind.ser.impl.UnknownSerializer.failForEmpty(UnknownSerializer.java:59)
    at com.fasterxml.jackson.databind.ser.impl.UnknownSerializer.serialize(UnknownSerializer.java:26)
    at com.fasterxml.jackson.databind.ser.BeanPropertyWriter.serializeAsField(BeanPropertyWriter.java:575)
    at com.fasterxml.jackson.databind.ser.std.BeanSerializerBase.serializeFields(BeanSerializerBase.java:663)
    at com.fasterxml.jackson.databind.ser.BeanSerializer.serialize(BeanSerializer.java:156)
    at com.fasterxml.jackson.databind.ser.BeanPropertyWriter.serializeAsField(BeanPropertyWriter.java:575)
    at com.fasterxml.jackson.databind.ser.std.BeanSerializerBase.serializeFields(BeanSerializerBase.java:663)
    at com.fasterxml.jackson.databind.ser.BeanSerializer.serialize(BeanSerializer.java:156)
    at com.fasterxml.jackson.databind.ser.DefaultSerializerProvider.serializeValue(DefaultSerializerProvider.java:129)
    at com.fasterxml.jackson.databind.ObjectWriter.writeValue(ObjectWriter.java:851)
    at org.springframework.http.converter.json.AbstractJackson2HttpMessageConverter.writeInternal(AbstractJackson2HttpMessageConverter.java:286)
    ... 61 more

怎么解决?谢了。

共有1个答案

谢同化
2023-03-14

服务的签名需要Byte[],因此即使ByteInputStream是可序列化的,它仍然不是正确的发送内容。只需发送那些字节[]

body.add("circularAttachment", 
                   new ByteArrayResource(souqBean.getCircularAttachment().getBytes()));

 类似资料:
  • 使用SpringIntegration,我通过HTTPPOST上传一个文件,并将其路由到服务激活器。 在service activator中,我使用RestTemplate调用另一台服务器,在那里转储文件,但我不明白为什么以下代码会出现以下错误: 我不明白的是,为什么调用RestTemplate时会出现下面的异常。交易所() xml配置 服务激活器使用的bean Stacktrace:: http

  • 问题内容: 我正在处理,&,但出现此错误。 请在下面检查我的实体 我该如何解决? 问题答案: 通过hibernate代理对象进行延迟加载时,我遇到了类似的问题。通过用以下方式注释具有延迟加载的私有属性的类来解决此问题: 我假设您可以在代理对象上添加打破JSON序列化到该批注的属性。 问题在于实体是延迟加载的,序列化是在实体完全加载之前发生的。

  • 问题内容: 我正在处理,&,但出现此错误。 请在下面检查我的实体 我该如何解决? 问题答案: 通过休眠代理对象进行延迟加载时,我遇到了类似的问题。通过用以下方式注释具有延迟加载的私有属性的类来解决此问题: 我假设您可以在代理对象上添加将JSON序列化破坏到该注释的属性。 问题在于实体是延迟加载的,序列化是在实体完全加载之前发生的。

  • 当我试图导航到一个endpoint时,我得到以下错误 类型定义错误:[简单类型,类org.hibernate.proxy.pojo.ByteBuddy.ByteBuddyInterceptor];嵌套异常为com.fasterxml.jackson.databind.exc.InvalidDefinitionException:未找到类org.hibernate.proxy.pojo.ByteBu

  • com.fasterxml.jackson.databind.jsonMappingException:未发现用于java.io.BufferedReader类的序列化程序,也未发现用于创建BeanSerializer的属性(为了避免异常,禁用SerializationFeature.fail_on_empty_beans)(通过引用链:pj.core.bean.actionResult[“dat