简体   繁体   中英

Spring request param not getting data from angular

I am sending a post request with http param and on server side i am accepting in @Requestparm but when i click on submit button i am getting error as Required String parameter 'subjectId' is not present I don't know why this error is comming as when i test same api in postman its working fine.I am sending as form-data from postman to test and works fine.but not from angular.what can be the issue.

Angular backend service code:

  public postStudentAssignment(communication: Communication){
    console.log(communication);
    // const headers = new HttpHeaders();
    // headers.append('Content-Type', 'application/json');
    // headers.append('Accept', 'application/json');

    let httpParams = new HttpParams()
    httpParams = httpParams.append('clientId',communication.clientId.toString())
    httpParams = httpParams.append('subjectId',communication.subjectId.toString())
    httpParams = httpParams.append('studentGradeDivisionId',communication.studentGradeDivisionId.toString())
    httpParams = httpParams.append('type',"Assignment")
    httpParams = httpParams.append('headline',communication.headline)
    httpParams = httpParams.append('communicationText',communication.communicationText)
    httpParams = httpParams.append('attachmentFileName',communication.attachmentFileName)
    httpParams = httpParams.append('attachment',communication.attachment)
    httpParams = httpParams.append('attachedPaperId',(!communication.attachedPaperId)?null:communication.attachedPaperId.toString())
    httpParams = httpParams.append('dueDate',communication.dueDate!=null?communication.dueDate.toString():null)
    httpParams = httpParams.append('isShared',communication.isShared.toString())
    httpParams = httpParams.append('createUserId',communication.createUserId.toString())
    console.log(httpParams)
    return this.http.post(BackendService.STUDENT_ASSIGNMENT,{parmas: httpParams, responseType:'text'});
   }

Server side (spring) code:

    @PreAuthorize("hasAnyRole('TEACHER')")
@PostMapping()
public ResponseEntity<String> insertCommunicationDetails(
        @RequestParam("subjectId") String subjectId,
        @RequestParam("studentGradeDivisionId") String studentGradeDivisionId,
        @RequestParam("type") String type, 
        @RequestParam("headline") String headline,
        @RequestParam("communicationText") String communicationText,
        @RequestParam("attachmentFileName") String attachmentFileName,
        @RequestParam("attachment") MultipartFile attachment, 
        @RequestParam("attachedPaperId") String attachedPaperId,
        @RequestParam("dueDate") String dueDate, 
        @RequestParam("isShared") String isShared,
        @RequestParam("clientId") String clientId, 
        @RequestParam("createUserId") Long createUserId) throws ParseException {

    CommunicationDto communicationDto = new CommunicationDto();
    communicationDto.setType(type);
    communicationDto.setSubjectId(Long.valueOf(subjectId));
    communicationDto.setStudentGradeDivisionId(Arrays.asList(studentGradeDivisionId.split(",")).stream().map(s -> Long.parseLong(s.trim())).collect(Collectors.toList()));
    communicationDto.setHeadline(headline);
    communicationDto.setCommunicationText(communicationText);
    communicationDto.setAttachmentFileName(attachmentFileName);
    communicationDto.setAttachment(attachment);
    communicationDto.setAttachedPaperId(Long.valueOf(attachedPaperId));
    communicationDto.setClientId(Long.valueOf(clientId));
    communicationDto.setCreateUserId(createUserId);
    communicationDto.setDueDate(new SimpleDateFormat("dd/MM/yyyy").parse(dueDate));
    communicationDto.setIsShared(Integer.parseInt(isShared));

    try {
        Set<ConstraintViolation<CommunicationDto>> violations = validator.validate(communicationDto);
        if (violations.size() == 0) {
            communicationDao.insertCommunicationDetails(communicationDto);
            return ResponseEntity.ok().body("Added successfully");
        } else {
            LOGGER.error(String.format("Could not insert communication details as data is invalid - %s",
                    communicationDto));
            for (ConstraintViolation<?> violation : violations) {
                LOGGER.error(violation.getMessage());
            }
            return null;
        }

    } catch (Exception e) {
        return ResponseEntity.ok().body(e.getMessage());
    }
}

Is this a typo?

return this.http.post(BackendService.STUDENT_ASSIGNMENT,{parmas: httpParams, responseType:'text'});

I believe this should rather be

return this.http.post(BackendService.STUDENT_ASSIGNMENT,{params: httpParams, responseType:'text'});

Edit: Even worse:

return this.http.post(BackendService.STUDENT_ASSIGNMENT, null, { params: httpParams, responseType:'text' } });

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