2

The problem is binding javascript arraylist with RequestParam annotation. I have a form data like this works fine if there is no nokDataList. nokInfoDatas is an javascript array that in console it looks: enter image description here

var requestData = JSON.stringify(nokInfoDatas);
    console.log("nokInfoDatas");
    console.log(nokInfoDatas);
    console.log("requestData");
    console.log(requestData);
var fd = new FormData();
        fd.append('photo', file);
        fd.append('testId', testId);
        fd.append('nokDataList', nokInfoDatas);
var ajaxData = {
            type: "POST",
            data: fd,
            processData : false,
            contentType : false,
            url: sendUrl,
            headers: headersData,
    };

Backend side:

public @ResponseBody String answer(HttpServletRequest request, 
            @RequestParam(value = "photo") MultipartFile photo,
            @RequestParam(value = "testId") String testId,
        @RequestParam(value = "nokDataList") List<NokDataDTO> nokInfoDtos
            )
4
  • 1
    can you post the entity class ? (NokDataDTO) Commented Dec 20, 2017 at 8:11
  • @harshil it is a basic java class that has : private Long id; private String nokInfo; private String nokData; Commented Dec 20, 2017 at 8:15
  • does it have setters ? Commented Dec 20, 2017 at 8:17
  • @harshil yes both have setters and getters Commented Dec 20, 2017 at 8:21

1 Answer 1

2

You can try as follows :

Create a Blob with your JSON data (requestData) :

var requestData = JSON.stringify(nokInfoDatas);
var fd = new FormData();
fd.append('photo', file);
fd.append('testId', testId);
fd.append('nokDataList', new Blob([requestData], {
  type: "application/json"
}));

Change @RequestParam to @RequestPart for parsing JSON with multi-part.

public @ResponseBody String answer(HttpServletRequest request, 
       @RequestParam(value = "photo") MultipartFile photo,
       @RequestParam(value = "testId") String testId,
       @RequestPart(value = "nokDataList") List<NokDataDTO> nokInfoDtos
      )
Sign up to request clarification or add additional context in comments.

5 Comments

Did not work for me, same problem about binding. By the way I did not use any kind of type like json because when I use to pass file,it blows.Maybe that is why your way didnt work?
@DogaOzdemir I tried the code and work properly. Maybe you need also Spring multi-part configuration. You can check my configuration here. I'm using Spring 4.3.3.RELEASE.
after deleting file append,binding worked fine! But there is a weird thing that List<NokDataDTO> nokInfoDtos inside LinkedHashMap values
@DogaOzdemir try removing headers:headersData your ajax method. I tried without it.
Solved whit this : List<NokDataDTO> nList= mapper.convertValue(nokInfoDtos, new TypeReference<List<NokDataDTO>>() { }); thanks for help anyway!

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.