Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

ResponseEntity doesnot accept text/csv: SpringBoot

Iam trying to create an API that accept both files like CSV and json body request. I tried using ResponseEntity object in spring boot.

The endpoint looks as below.

    @PostMapping(value="/csv",consumes=MediaType.ALL_VALUE)
    public void createConsumer(RequestEntity<?> data){



    }

The content headers is set via postman.

The Content-Type is text/csv and Accept is */*.

The error thrown is

2021-03-12 19:28:10.344  WARN 5780 --- [nio-8089-exec-2] .w.s.m.s.DefaultHandlerExceptionResolver : Resolved [org.springframework.web.HttpMediaTypeNotSupportedException: Content type 'text/csv' not supported]

How can I solve this?

like image 312
pacman Avatar asked Jun 05 '26 20:06

pacman


1 Answers

Write 2 methods. If you use file as request body, use MulpartFile and corresponding 'consumes=..', and for json use @RequestBody:

  @PostMapping(value="/csv", consumes = MULTIPART_FORM_DATA_VALUE)
        public void createConsumer(@RequestParam MultipartFile file){
          
        }

@PostMapping(value="/csv", consumes = APPLICATION_JSON_VALUE)
        public void createConsumerFromJson(@RequestBody SomeObject json){
          
        }

Set content type as form-data in body in Postman for MultipartFile, like

enter image description here

like image 98
Valeriy K. Avatar answered Jun 08 '26 09:06

Valeriy K.



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!