Here is my controller:
@RestController
@RequestMapping("/api/product")
public class ProductController {
@Autowired
ProductService productService;
@PreAuthorize(value = "hasRole('ADD_PRODUCT')")
@PostMapping(value = "/add", consumes = {
MediaType.MULTIPART_FORM_DATA_VALUE,
MediaType.APPLICATION_JSON_VALUE
})
public HttpEntity<?> addProduct(@CurrentUser User user,
@RequestPart("files") MultipartFile multipartFile,
@Valid @RequestPart("info") ProductDto productDto) {
ApiResponse apiResponse = productService.addProduct(user, productDto, multipartFile);
return ResponseEntity.status(apiResponse.isSuccess() ? 201 : 409).body(apiResponse);
}
}
It should receive a MultipartFile and Json object and my ProductDto class is:
@Data
@AllArgsConstructor
@NoArgsConstructor
public class ProductDto {
@NotNull(message = "Name can't be empty")
@NotBlank(message = "Name can't be blank")
private String name;
@Length(max = 1000)
private String description;
@NotNull(message = "Price can't be empty")
private double price;//Evaluated in the $
@NotNull(message = "You should choose one of the categories")
private UUID categoryId;
}
I'm finding difficulties when I try to send request
But it always giving me 403 Forbidden and I don't know the reason This request is not coming to addProduct() method at all and I have permission "ADD_PRODUCT". @CurrentUser annotation class is
@Documented
@Retention(value = RetentionPolicy.RUNTIME)
@Target(ElementType.PARAMETER)
@AuthenticationPrincipal
public @interface CurrentUser { }
Not any error or exception is thrown as well
I have tried @RequestParam and @RequestPart both annotations I want to learn how to deal with situations like this when sending MultipartFile and Json object simultaneously. How can I do that in Spring boot
The 403 in postman I usually get it when I use http
instead of https
in the URL. I think you can try editing the postman URL or if that doesn't work try to tell the spring boot app to use simple http.
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.