简体   繁体   中英

Error when performing "POST" operation in spring?

I have sent the data from the angular service class. Here i have sent three parameters to the Api for the POST operation给 Api 进行 POST 操作

export class LetterService {

  private baseUrl = 'http://localhost:8080/api/letter';
   constructor(private http: HttpClient) { }

   saveThree(letter: Object,documents: Object,empList: Object): Observable<Object> {
    return this.http.post(`${this.baseUrl}` + `/create`, {letter,documents,empList});
  }

And on the Spring boot side I used this @RequestBody to map the coming JSON data from service. :

@PostMapping(value = "/letter/create")
    public String postAllThree(@RequestBody LetterDto letterDto,
            @RequestBody List<Document> document,@RequestBody SelectionCustomOfficeDto selectionCustomOfficeDto) {
        
        
        ClkLetter clkLetter=clkLetterRepository.findById((long)1).get();
        
        Selection selection=selectionRepository.findById((long)letterDto.getSelectionNo()).get();
        
        Assessment assessment=assessmentRepository.findById((long)letterDto.getAssessmentNo()).get();
    
        
        
    Letter letter=letterRepository.save(new Letter(clkLetter,letterDto.getInOut(),letterDto.getInOutNo(),letterDto.getInOutDate(),letterDto.getLetterIssuedSubBy(),letterDto.getLetterFile(),letterDto.getRepresentativeName()
                ,selection,assessment));
        
     for(Document docume:document)  {
         
         if(docume.isChecked()) {
             letterDocRepository.save(new LetterDoc(letter,docume,"a"));
         }
     }
        
        return  "success";
    }

The error I am getting is:

Why am i getting this error ? is the parameter i am sending from angular is not matching or anything wrong in my @Postmapping operation? i saw other articles and questions also but it didn't work .

You are only allowed to have one @RequestBody. Combine {letter,documents,empList} in one DTO POJO.

public class MyDTO {
    private Object letter;
    private List<Object> documents;
    private List<Object> empList;
// getters and setters
}

For primitive type request parameters you might use @RequestParam and append them in the URL

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM