rest - Postman : Required request part 'file' is not present -
i wanted upload image rest api through postman. using spring boot framework. here screen shot :
i have not set any headers found in other stack overflow answers gave multipart boundary error.
now, below controller code :
package com.practice.rest.assignment1.controller; import java.io.ioexception; import java.util.list; import org.springframework.beans.factory.annotation.autowired; import org.springframework.web.bind.annotation.pathvariable; import org.springframework.web.bind.annotation.requestbody; import org.springframework.web.bind.annotation.requestmapping; import org.springframework.web.bind.annotation.requestmethod; import org.springframework.web.bind.annotation.requestparam; import org.springframework.web.bind.annotation.restcontroller; import org.springframework.web.multipart.multipartfile; import com.practice.rest.assignment1.model.product; import com.practice.rest.assignment1.service.catalogueservice; import org.codehaus.jackson.jsonparseexception; import org.codehaus.jackson.map.jsonmappingexception; import org.codehaus.jackson.map.objectmapper; @restcontroller @requestmapping("/cataloguecontroller/") public class cataloguecontroller{ @autowired private catalogueservice catalogueservice; @requestmapping(value = "addproduct", method = requestmethod.post , consumes = "multipart/form-data") public product addproduct(@requestparam string productjson, @requestparam multipartfile file) throws jsonparseexception, jsonmappingexception, ioexception { product product = new objectmapper().readvalue(productjson, product.class); byte[] mediabytes = file.getbytes(); product.setimage(mediabytes); return catalogueservice.saveindb(product); } }
now, taking product object consists internally image defined byte[] array. take string , image separately multipart file.
below product class attributes defined :
private long pid; private string model; private string brand; private byte[] image; // want image save private long price; private string currency; private string transmissiontype; private string fueltype;
since , using spring boot , here main class :
package com.practice.rest.assignment1; import org.springframework.boot.springapplication; import org.springframework.boot.autoconfigure.springbootapplication; @springbootapplication public class app { public static void main(string[] args) { springapplication.run(app.class, args); } }
the error on postman :
{ "timestamp": 1478611635977, "status": 400, "error": "bad request", "exception": "org.springframework.web.multipart.support.missingservletrequestpartexception", "message": "required request part 'file' not present", "path": "/cataloguecontroller/addproduct" }
where wrong ?
i think problem lies in json parameter sending. in postman don't need put starting , trailing " represent parameter string. , if use starting , ending " inside json( mean json object properties key , value) should use '(single quote).
Comments
Post a Comment