10

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?

4

4 Answers 4

0

Appending the values to the URL what u have been doing now using @RequestParam.

@RequestParam annotation will not work for complex JSON Objects , it is specifi for Integer or String .

If it is a Http POST method , use of @RequestBody will make the Spring to map the incoming request to the POJO what u have created (condition: if the POJO maps the incoming JSON)

Sign up to request clarification or add additional context in comments.

1 Comment

Multipart request is used to handle files , u can skip it off when u don't need it
0

create FormData() and append your json and file

        if (form.validate()) {
        var file = $scope.file;
        var fd = new FormData();
        fd.append('jsondata', $scope.jsonData);  
        fd.append('file', file);
        MyService.submitFormWithFile('doc/store.html', fd, '', (response){
             console.log(response)
        });
    }

//Service called in above

    MyService.submitFormWithFile = function(url, data, config, callback) {
    $http({
        method : 'POST',
        url : url,
        headers : {
            'Content-Type' : undefined
        },
        data : data,
        transformRequest : function(data, headersGetterFunction) {
            return data;
        }
    }).success(function(response, status, header, config) {
        if (status === 200) {
            callback(response);
        } else {
            console.log("error")
        }
    }).error(function(response, status, header, config) {           
            console.log(response);
    });
};

// in your java part using ObjectMapper

//it is like  string
fd.append('jsondata', JSON.stringify($scope.jsonData));



  @Autowired
  private ObjectMapper mapper;

 @RequestMapping(value = "/companies", method = RequestMethod.POST)
   public void createCompany(@RequestParam String jsondata,
        @RequestParam(required = true) MultipartFile file){


        CompanyDto companyDto=mapper.readValue(jsondata, CompanyDTO.class);
       ......
 }

Comments

0

Use below code snippet:

    @RequestMapping(value= "/path", method=RequestMethod.POST, produces=MediaType.APPLICATION_JSON_VALUE)
    public ResponseObject methodName(MyData input, @RequestParam(required=false) MultipartFile file) {
        // To Do
    }

Comments

0

While consuming a form-data input, the request parameters can only be in key-value format. Hence, you cannot directly use a POJO as a request parameter.

For your use case, you can do something like:

// RequestBody will be the JSON body which can be mapped to a POJO.
public void createCompany(Multipart file, String requestBody) {
  // Parse the requestBody into POJO.
}

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.