【问题标题】:Multipartfile charset=UTF-8 is not supported spring boot api restMultipartfile charset=UTF-8 不支持 spring boot api rest
【发布时间】:2023-02-14 07:01:49
【问题描述】:

代码运行正常,我已经尝试了各种方法来解决它,但我做不到,这可能是在我将 MultipartFile 转换为数组后发生的

@RestController
@RequestMapping("products")
public class ProductController {

    @Autowired
    private ProductService productService;

    @PostMapping(consumes = MediaType.MULTIPART_FORM_DATA_VALUE)
    @Transactional
    public ResponseEntity<ShowProductsDTO> registerProduct(
            @RequestBody @Valid ProductDTO dto,
            @RequestParam(name = "files", required = true) MultipartFile[] files, 
            UriComponentsBuilder uriBuilder) {
        ShowProductsDTO showProductsDTO = null;
        try {
            showProductsDTO = productService.save(dto, files);
        } catch (IOException e) {
            e.printStackTrace();
        }

        var uri = uriBuilder.path("/products/{id}").buildAndExpand(showProductsDTO.id()).toUri();
        return ResponseEntity.created(uri).body(showProductsDTO);

    }

数据传输协议

public record ProductDTO(
    @NotBlank
    String name,
    @NotBlank
    String description,
    @NotNull
    @NumberFormat
    BigDecimal price,
    @NumberFormat
    @NotNull
    Integer quantity,
    @NotNull
    Boolean active,
    @NotNull
    Long sub_category_id
    ) {

}

错误控制台

已解决 [org.springframework.web.HttpMediaTypeNotSupportedException: 内容类型 '多部分/表单数据;边界=------------------------816548045966415708649211;字符集=UTF-8' 不支持]

邮递员身体>原始> json

{
    "name": "Nome do produto",
    "description": "descricao do produto",
    "price": "2500.00",
    "quantity": "2",
    "active": "true",
    "sub_category_id": "1"
}

邮递员>正文>表单数据

KEY "files", TYPE file, VALUE uma imagem minha em png

错误邮递员

{
    "timestamp": "2023-01-11T06:15:43.455+00:00",
    "status": 415,
    "error": "Unsupported Media Type",
    "message": "Content-Type 'multipart/form-data;boundary=--------------------------056640214920648036756520;charset=UTF-8' is not supported.",
    "path": "/products"
}

产品实体

@Table(name = "products")
@Entity(name = "Product")
@Getter
@Setter
@NoArgsConstructor
@EqualsAndHashCode(of = "id")
public class Product {

    @Id
    @GeneratedValue(strategy = GenerationType.IDENTITY)
    private Long id;

    @Column(length = 100, unique = true, nullable = false)
    private String name;
    @Column(nullable = false, columnDefinition = "TEXT")
    private String description;
    @Column(length = 8, nullable = false, columnDefinition = "NUMERIC(8,2)")
    private BigDecimal price;
    @Column(nullable = false, columnDefinition = "INT")
    private Integer quantity;
    @Column(nullable = false, columnDefinition = "BOOLEAN")
    private Boolean active;

    @CollectionTable(name = "products_files", 
            joinColumns = 
            @JoinColumn(name = "product_id", referencedColumnName = "id"))
    private List<String> productFiles;

    @JoinColumn(name = "sub_category_id")
    @ManyToOne(fetch = FetchType.EAGER)
    private SubCategory subCategory;

我该如何解决这个错误?

【问题讨论】:

    标签: spring spring-boot


    【解决方案1】:

    将您的属性更改为@PostMapping(consumes = MediaType.APPLICATION_JSON_VALUE)

    并使用Postman body &gt; raw &gt; json 调用您的 api。

    问题是,Content-Type: form-data 处理文件请求。

    【讨论】:

    • 我刚刚编辑了帖子并放置了产品实体,这不是你的错,因为它不清楚但我需要接收照片和视频文件,因为商店中的每个产品都会有照片或视频
    【解决方案2】:

    还没解决,求大神帮忙

    【讨论】:

      猜你喜欢
      • 2019-12-16
      • 2023-02-15
      • 2018-08-06
      • 1970-01-01
      • 2018-08-19
      • 2020-04-13
      • 2021-04-12
      • 2018-08-21
      • 1970-01-01
      相关资源
      最近更新 更多