When rest controller return null body, ajax go to the error method

839 views Asked by At

env: spring boot 2.0,jackson serialization

my rest controller method will not return any body,just like ResponseEntity.ok().build(),but the ajax don't go to success method,it will go to error emthod,and the error shows "Unexpected end of JSON input"

i tried this:
1. add the properties into application.properties : spring.jackson.default-property-inclusion: NON_NULL, it still go to error method;
2. update return result like this ResponseEntity.ok("{}"),it will ok;

@PostMapping(value = "/import")
   public ResponseEntity<Void> importRecipients(@ModelAttribute UserRecipientDto userRecipientDto,
                                                @CurrentUser @ApiIgnore User user,
                                                @RequestParam ImportType importType,
                                                @RequestParam String name,
                                                @RequestParam List<Long> ids) {
       Collection<MailRecipient> count = this.service.getRecipients(userRecipientDto, importType, ids);
       UserMailRecipientGroup userMailRecipientGroup = new UserMailRecipientGroup(name, USER_RECIPIENT, count, user);
       userMailRecipientGroupService.saveMailRecipientGroup(userMailRecipientGroup);
       return ResponseEntity.ok().build();
   }

application.yml:

spring:
  jackson:
      serialization:
        write-dates-as-timestamps: true
        write_dates_with_zone_id: true
      time-zone: GMT
      default-property-inclusion: NON_NULL

ajax code block:

    url: url,
    type: type,
    contentType: "application/json;charset=utf-8",
    headers: {},
    data: param,
    async: true,
    dataType: "json",
    success: function success(data) {
      cb(data);
    }, 
error: function error(request, status_code, text_error) {}

i expect this:
when my rest method return ok().build(), the ajax will go to success method.
i guess ajax convert result to json occur some error, may be some spring boot properties will do it, but i haven't found yet.
thanks!!

3

There are 3 answers

0
steven On

how a silly question is it, I should not doubt my controller. In fact, the problem happens on front-end, my workmate support an error json resolver!!

0
Haran On

Not clear what class your controller is extending and the import of your ok() method.

Maybe try with ResponseEntity

ResponseEntity.ok().build()
2
Jp Naidu On

From return ok().build() you are just returning the Http Status 200 OK Status. Isn't it?

What exactly you have to return to client?

If it is some response you have go for return ResponseEntity.ok().build() or

return Response.ok().entity(someEntity).build();

and im seeing the return type is ResponseEntity from the code.

Please eloborate the problem.

Edit:

Code would be look like

@PostMapping(value = "/import")

public ResponseEntity<String> importRecipients(@ModelAttribute UserRecipientDto userRecipientDto,

        @CurrentUser @ApiIgnore User user, @RequestParam ImportType 

        importType, @RequestParam String name,

        @RequestParam List ids) {


    Collection count = this.service.getRecipients(userRecipientDto, importType, ids);


    UserMailRecipientGroup userMailRecipientGroup = new UserMailRecipientGroup(name, USER_RECIPIENT, count, user);


    userMailRecipientGroupService.saveMailRecipientGroup(userMailRecipientGroup);


    return new ResponseEntity(HttpStatus.OK);


}