Quantcast
Channel: Active questions tagged rest - Stack Overflow
Viewing all articles
Browse latest Browse all 3630

Posting a file and JSON data to Spring rest service

$
0
0

I am building a Spring rest service for uploading a file. There is a form that consists of various field and one field for uploading a file. On submitting that form, I am sending a multipart form request i.e. Content-Type as multipart/form-data.

So I tried with below

@RequestMapping(value = "/companies", method = RequestMethod.POST)    public void createCompany(@RequestBody CompanyDTO companyDTO, @RequestParam(value = "image", required = false) MultipartFile image){.................   

But, the above didn't work. So for time being,i sent JSON data as String and forming Company Object from that String in rest service like

 @RequestMapping(value = "/companies", method = RequestMethod.POST)        public void createCompany(@RequestParam("companyJson") String companyJson, @RequestParam(value = "image",required = false) MultipartFile image) throws JsonParseException, JsonMappingException, IOException{            CompanyDTO companyDTO =  new ObjectMapper().readValue(companyJson, CompanyDTO.class);.............................

Can't I send JSON data with @RequestBody without passing JSON as String?


Viewing all articles
Browse latest Browse all 3630

Trending Articles



<script src="https://jsc.adskeeper.com/r/s/rssing.com.1596347.js" async> </script>