3

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?

pacman
  • 725
  • 1
  • 9
  • 28
  • Does this answer your question? [How to accept text/csv as content-type in spring-web servlet?](https://stackoverflow.com/questions/51839422/how-to-accept-text-csv-as-content-type-in-spring-web-servlet) – deadshot Mar 12 '21 at 14:14
  • @deadshot Its not working for me. I have already set `Content-Type` is `text/csv` and `Accept` is `*/*`. – pacman Mar 12 '21 at 14:18
  • Are you uploading files or are you posting CSV and/or JSON data as the request body. Your question suggest the first, your code the second (and obviously it won't work). – M. Deinum Mar 12 '21 at 15:58

1 Answers1

1

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

Valeriy K.
  • 2,616
  • 1
  • 30
  • 53
  • But the problem is that I want to accept data such as json in body as well as file type data – pacman Mar 12 '21 at 15:26