java 未找到sun.nio.ch.ChannelInputStream类的序列化程序,也未发现用于创建BeanSerializer的属性

8ehkhllq  于 2023-05-15  发布在  Java
关注(0)|答案(1)|浏览(666)

我正在尝试上传一个文件作为xlsx到一个文档。

@Autowired
HttpHeaders headers;
    

        headers.setContentType(MediaType.MULTIPART_FORM_DATA);          
        MultiValueMap<String, Object> body = new LinkedMultiValueMap<String, Object>();
        File fileToUpload = new File(attachmentLocation); // /tmp/xyz.xlsx
        body.add("file", new FileSystemResource(fileToUpload));    
        HttpEntity<MultiValueMap<String, Object>> requestEntity = new HttpEntity<>(body, headers);    
        try {
            ResponseEntity<byte[]> response = restTemplate.exchange(myURL,
                    HttpMethod.POST, requestEntity, byte[].class);
        }
        catch (RestClientException e) {
            e.printStackTrace();
        }

我收到以下错误消息:

org.springframework.http.converter.HttpMessageConversionException: Type definition error: [simple type, class sun.nio.ch.ChannelInputStream]; nested exception is com.fasterxml.jackson.databind.exc.InvalidDefinitionException: No serializer found for class sun.nio.ch.ChannelInputStream and no properties discovered to create BeanSerializer (to avoid exception, disable SerializationFeature.FAIL_ON_EMPTY_BEANS) (through reference chain: org.springframework.util.LinkedMultiValueMap["file"]->java.util.ArrayList[0]->org.springframework.core.io.FileSystemResource["inputStream"])
iovurdzv

iovurdzv1#

这对我很有效。您需要在Jackson中禁用FAIL_ON_EMPTY_BEANS。

import com.fasterxml.jackson.databind.ObjectMapper;
import com.fasterxml.jackson.databind.SerializationFeature;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;

@Configuration
public class ObjectMapperConfiguration {

  @Bean
  public ObjectMapper objectMapper() {
    return new ObjectMapper().disable(SerializationFeature.FAIL_ON_EMPTY_BEANS);
  }
}

相关问题