【发布时间】: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