0

Sorry about the duplicate, the existing answers are not applicable/did not work for me. Example: Jackson Not Overriding Getter with @JsonProperty

Below is my object, it is still coming back as zipValid and applianceTypeValid however I am looking for isZipValid and isApplianceTypeValid.

During debugging, I noticed that this line jsonResponse.put("statusResponse", new JSONObject(responseStatus)); is where it gets changed to zipValid and applianceTypeValid. responseStatus has the correct values as expected, but the moment responseStatus is loaded intojsonResponse, gone. Don't know is this helps.

package com.dish.wfm.services.model;

import com.fasterxml.jackson.annotation.JsonProperty;
import java.util.List;

public class ValidationDTO {

   private boolean isZipValid;

   private boolean isApplianceTypeValid;

   private List<String> partnerEnabledProductTypes;

   @JsonProperty(value="isZipValid")
   public boolean isZipValid() {
       return isZipValid;
   }

   public void setZipValid(boolean zipValid) {
       isZipValid = zipValid;
   }

   @JsonProperty(value="isApplianceTypeValid")
   public boolean isApplianceTypeValid() {
       return isApplianceTypeValid;
   }

   public void setApplianceTypeValid(boolean applianceTypeValid) {
       isApplianceTypeValid = applianceTypeValid;
   }

   public List<String> getPartnerEnabledProductTypes() {
       return partnerEnabledProductTypes;
   }

   public void setPartnerEnabledProductTypes(List<String> partnerEnabledProductTypes) {
       this.partnerEnabledProductTypes = partnerEnabledProductTypes;
   }
}

Thanks!

Billy
  • 1,049
  • 3
  • 14
  • 23
  • 2
    I just added a main method creating a new ObjectMapper and writing your DTO as a string, and I see isZipValid in the output. Post a complete minimal example reproducing the problem and tell which version of Jackson you're using. – JB Nizet Dec 11 '19 at 23:13
  • From which package `JSONObject` comes from? – Michał Ziober Dec 12 '19 at 11:33
  • 1
    @JB Nizet, I'm not at the office right now however here is my version: ```com.fasterxml.jackson.core jackson-annotations 2.9.9``` – Billy Dec 12 '19 at 14:37
  • 1
    @MichałZiober org.json.JSONObject is the package. – Billy Dec 12 '19 at 14:50
  • @NikolaiShevchenko `responseStatus = {ResponseStatus@8340} statusCode = "0" statusMessage = "OK" flagName = null isFlagStatus = false status = true sealedFlagName = null isSealedFlagStatus = false sealedStatus = false isZipValid = true isApplianceTypeValid = true jsonResponse = {JSONObject@8341} "{"queues":{"agentName":"","partnerEnabledProductTypes":[],"cancelFlag":"","productType":"","status":"COMPLETED"}}"` is the object before inserting into `JSONObject` – Billy Dec 12 '19 at 14:53
  • Why do you use `org.json.JSONObject`? You can just write: `jsonResponse.put("statusResponse", responseStatus);` – Michał Ziober Dec 12 '19 at 14:56
  • 1
    @MichałZiober Worked. Thanks! – Billy Dec 12 '19 at 16:28

1 Answers1

0

You should not use org.json.JSONObject if you already use Jackson. Just write it as below:

jsonResponse.put("statusResponse", responseStatus);
Michał Ziober
  • 37,175
  • 18
  • 99
  • 146