0

this is the JSON I am sending:

{
  "code": "400173258",
  "name": "APPLE pie 2",
  "onlinePrice": "800",
  "mrp": "1000",
  "validity": "123",
  "videoConsultCount": "11",
  "audioConsultCount": "11",
  "textConsultCount": "11",
  "audioVideoDuration": "15",
  "textConsultValidityDays": 0,
  "medicineIncluded": true,
  "medicalTestDetail": null,
  "description": "wOW",
  "showOnWebsite": true,
  "type": "MC"

}

This is my controller:

@RequestMapping("/edit")
@ResponseBody
public HealthPackageCRUDResponse editHealthPackage(@RequestBody AddEditHealthPackageForm healthPackageForm) {
    HealthPackageCRUDResponse response = new HealthPackageCRUDResponse();
    LybrateUser user = WebContextUtils.getCurrentUser();
    if (user == null) {
        LOG.info("user is not logged in.");
        response.setSuccessful(false);
        response.setMessage("Please login to create package.");
        return response;
    }
    if (StringUtils.isEmpty(healthPackageForm.getCode())) {
        LOG.info("Health package code is missing");
        response.setSuccessful(false);
        response.setMessage("There is no such package.");
        return response;
    }
    HealthPackage healthPackage = healthPackageService.getHealthPackageByCode(healthPackageForm.getCode());

    if (healthPackage != null && !HealthPackageStatus.DELETED.code().equalsIgnoreCase(healthPackage.getStatus()) && healthPackage.isEditable()) {
        if (!healthPackage.getUser().getId().equals(user.getUser().getId())) {
            response.setSuccessful(false);
            response.setMessage("You are not authorized to edit this package.");
            return response;
        }
        healthPackage = healthPackageService.editHealthPackage(healthPackage, healthPackageForm, user.getUser(), "Web", true, HealthPackgeType.MULTI_CONSULT_PACKAGE.code());
        response.setSuccessful(true);
        response.setMessage("Package Edited.");
        response.setHealthPackage(healthPackageConverterService.healthPackageToHealthPackageDTO(healthPackage));
    } else {
        response.setSuccessful(false);
        response.setMessage("Not able to create package");
    }
    return response;
}

This is the RequestBody mapping object which works:

public class AddEditHealthPackageForm implements Serializable {

private List<HealthPackageMediaDTO> mediaDTOs;

public AddEditHealthPackageForm(){
    this.mediaDTOs = new ArrayList<HealthPackageMediaDTO>();
}

And this is the version that doesn't work: (It works if I add "mediaDTOs": [] to the json object)

public class AddEditHealthPackageForm implements Serializable {

private List<HealthPackageMediaDTO> mediaDTOs = new ArrayList<HealthPackageMediaDTO>(); // this is initialized outside the default constructor

public AddEditHealthPackageForm(){
    // this is empty so it doesn't work (gives 400 Bad request syntactically incorrect request)
}

Why is this happening? Shouldn't the spring framework simply assign a null value to mediaDTOs field when it doesn't find it in the json. Why does it work when I initialize the list inside the default constructor. It also works when I don't initialize it at all. It also works if I add "mediaDTOs": [] to the json object. I can't understand why such a basic thing needs to be so confusing? Am I making a silly mistake somewhere?

Shasak
  • 790
  • 8
  • 19
  • 1
    bit too complicated to understand. Please post only relevant code... – Jos Sep 23 '15 at 17:18
  • Removed the irrelevant bits – Shasak Sep 23 '15 at 18:34
  • Can we see a stacktrace for the 400 error? –  Sep 23 '15 at 18:40
  • I am sorry I don't know how to enable the debug logging for spring. I only recently started working on this project. The other guys in the team are simply suggesting the working methods because none of them can understand this and don't wish to spend anymore time on it. So till I get one of them to enable it I can't give you the stacktrace – Shasak Sep 23 '15 at 18:46
  • My crystal ball is telling me that maybe the exception is a jackson one complaining about a missing field or something like that (see [this for example](http://stackoverflow.com/questions/4486787/jackson-with-json-unrecognized-field-not-marked-as-ignorable)) if so, the answer is to tell jackson to ignore it (see linked question best answer) –  Sep 23 '15 at 18:59
  • So does spring use Jackson internally? Because we are using Gson for our JSON-Java conversions. – Shasak Sep 23 '15 at 19:02

0 Answers0