On Mon, Nov 5, 2018 at 1:45 PM SPb <[email protected]> wrote:
>
> I have a REST Api which receives an Object as a parameter. Now I am trying to 
> convert this object into a desired java class. I am using Jackson. Since my 
> object consist of some inner complex class in its structure, I receive the 
> object in the form of a LinkedHashMap. Following is the java class in which I 
> want to convert my object into:
>
> public class Parameters {
>   @JsonIgnore
>   private CubeItem cubeItem = null;
>
>   @JsonProperty("location")
>   private String location = null;
>
>   public Parameters CubeItem(CubeItem cubeItem) {
>     this.cubeItem = cubeItem;
>     return this;
>   }
>
>   public Parameters() {
>   }
>
>   /**
>
>    * Get CubeItem
>    * @return CubeItem
>    **/
>   @JsonGetter("cubeItem")
>   @ApiModelProperty(value = "")
>   public CubeItem getCubeItem() {
>     return cubeItem;
>   }
>
>
>
> public void setCubeItem(CubeItem cubeItem) {
>     this.cubeItem = cubeItem;
>   }
>
>   public Parameters location(String location) {
>     this.location = location;
>     return this;
>   }
>
>   /**
>    * Get location
>    * @return location
>    **/
>   @JsonProperty("location")
>   @ApiModelProperty(value = "")
>   public String getLocation() {
>     return location;
>   }
>
>   public void setLocation(String location) {
>     this.location = location;
>   }
>
>
> }
>
> And this is the inner class:
>
> public class CubeItem {
>     /**
>      * Gets or Sets upperCubeHalf
>      */
>     public enum UpperCubeHalfEnum {
>         BLACK("BLACK"),
>
>         WHITE("WHITE"),
>
>         TRANSPARENT("TRANSPARENT");
>
>         private String value;
>
>         UpperCubeHalfEnum(String value) {
>             this.value = value;
>         }
>
>         @Override
>         @JsonValue
>         public String toString() {
>             return String.valueOf(value);
>         }
>
>         @JsonCreator
>         public static UpperCubeHalfEnum fromValue(String text) {
>             for (UpperCubeHalfEnum b : UpperCubeHalfEnum.values()) {
>                 if (String.valueOf(b.value).equals(text)) {
>                     return b;
>                 }
>             }
>             return null;
>         }
>     }
>     public CubeItem(){}
>
>     public CubeItem(UpperCubeHalfEnum upperCubeHalf, LowerCubeHalfEnum 
> lowerCubeHalf, InlayEnum inlay) {
>         this.upperCubeHalf = upperCubeHalf;
>         this.lowerCubeHalf = lowerCubeHalf;
>         this.inlay = inlay;
>     }
>
>     @JsonProperty("UpperCubeHalf")
>     private UpperCubeHalfEnum upperCubeHalf = null;
>
>     /**
>      * Gets or Sets lowerCubeHalf
>      */
>     public enum LowerCubeHalfEnum {
>         BLACK("BLACK"),
>
>         WHITE("WHITE");
>
>         private String value;
>
>         LowerCubeHalfEnum(String value) {
>             this.value = value;
>         }
>
>         @Override
>         @JsonValue
>         public String toString() {
>             return String.valueOf(value);
>         }
>
>         @JsonCreator
>         public static LowerCubeHalfEnum fromValue(String text) {
>             for (LowerCubeHalfEnum b : LowerCubeHalfEnum.values()) {
>                 if (String.valueOf(b.value).equals(text)) {
>                     return b;
>                 }
>             }
>             return null;
>         }
>     }
>
>     @JsonProperty("LowerCubeHalf")
>     private LowerCubeHalfEnum lowerCubeHalf = null;
>
>     /**
>      * Gets or Sets inlay
>      */
>     public enum InlayEnum {
>         RED("RED"),
>
>         WHITE("WHITE"),
>
>         BLUE("BLUE");
>
>         private String value;
>
>         InlayEnum(String value) {
>             this.value = value;
>         }
>
>         @Override
>         @JsonValue
>         public String toString() {
>             return String.valueOf(value);
>         }
>
>         @JsonCreator
>         public static InlayEnum fromValue(String text) {
>             for (InlayEnum b : InlayEnum.values()) {
>                 if (String.valueOf(b.value).equals(text)) {
>                     return b;
>                 }
>             }
>             return null;
>         }
>     }
>
>     @JsonProperty("Inlay")
>     private InlayEnum inlay = null;
>
>     public CubeItem upperCubeHalf(UpperCubeHalfEnum upperCubeHalf) {
>         this.upperCubeHalf = upperCubeHalf;
>         return this;
>     }
>
>     /**
>      * Get upperCubeHalf
>      * @return upperCubeHalf
>      **/
>
>     @ApiModelProperty(value = "")
>     public UpperCubeHalfEnum getUpperCubeHalf() {
>         return upperCubeHalf;
>     }
>
>     public void setUpperCubeHalf(UpperCubeHalfEnum upperCubeHalf) {
>         this.upperCubeHalf = upperCubeHalf;
>     }
>
>     public CubeItem lowerCubeHalf(LowerCubeHalfEnum lowerCubeHalf) {
>         this.lowerCubeHalf = lowerCubeHalf;
>         return this;
>     }
>
>     /**
>      * Get lowerCubeHalf
>      * @return lowerCubeHalf
>      **/
>
>     @ApiModelProperty(value = "")
>     public LowerCubeHalfEnum getLowerCubeHalf() {
>         return lowerCubeHalf;
>     }
>
>     public void setLowerCubeHalf(LowerCubeHalfEnum lowerCubeHalf) {
>         this.lowerCubeHalf = lowerCubeHalf;
>     }
>
>     public CubeItem inlay(InlayEnum inlay) {
>         this.inlay = inlay;
>         return this;
>     }
>
>     /**
>      * Get inlay
>      * @return inlay
>      **/
>
>     @ApiModelProperty(value = "")
>     public InlayEnum getInlay() {
>         return inlay;
>     }
>
>     public void setInlay(InlayEnum inlay) {
>         this.inlay = inlay;
>     }
>
>
>     @Override
>     public boolean equals(Object o) {
>         if (this == o) {
>             return true;
>         }
>         if (o == null || getClass() != o.getClass()) {
>             return false;
>         }
>         CubeItem cubeItem = (CubeItem) o;
>         return Objects.equals(this.upperCubeHalf, cubeItem.upperCubeHalf) &&
>                 Objects.equals(this.lowerCubeHalf, cubeItem.lowerCubeHalf)
>        &&
>                 Objects.equals(this.inlay, cubeItem.inlay);
>     }
>
>     @Override
>     public int hashCode() {
>         return Objects.hash(upperCubeHalf, lowerCubeHalf, inlay);
>     }
>
>
>     @Override
>     public String toString() {
>         StringBuilder sb = new StringBuilder();
>         sb.append("class CubeItem {\n");
>
>         sb.append("    upperCubeHalf: 
> ").append(toIndentedString(upperCubeHalf)).append("\n");
>         sb.append("    lowerCubeHalf: 
> ").append(toIndentedString(lowerCubeHalf)).append("\n");
>         sb.append("    inlay: ").append(toIndentedString(inlay)).append("\n");
>         sb.append("}");
>         return sb.toString();
>     }
>
>     /**
>      * Convert the given object to string with each line indented by 4 spaces
>      * (except the first line).
>      */
>     private String toIndentedString(Object o) {
>         if (o == null) {
>             return "null";
>         }
>         return o.toString().replace("\n", "\n    ");
>     }
> }
>
> I use the following line of code to convert my object into the java class:
>
> ObjectMapper mapper = new ObjectMapper();
> mapper.configure(DeserializationFeature.FAIL_ON_UNKNOWN_PROPERTIES, false);

Ok, first things first: this ^^^^

should not be used when diagnosing problems. It basically disables
sanity checking that would likely tell you what is the problem. So,
leave that enabled.

Also:

>         mapper.enable(READ_ENUMS_USING_TO_STRING);

since you are using explicit `@JsonValue` on `toString()`, you don't
need it (or, conversely, could perhaps leave out annotation). Can
leave that disabled.

>         Parameters parameters = mapper.convertValue(operationParameters, 
> Parameters.class);
>
> Now when I use the above code , I can read the string data properly, but the 
> cubeitem is not properly converted. The cubeItem is always produced NULL: IS 
> there any correct way of de-serializing the CubeItem Class?

Once you get exception from mis-matching property or Enum value it is
probably easier to see where the problem lies.

-+ Tatu +-

-- 
You received this message because you are subscribed to the Google Groups 
"jackson-user" group.
To unsubscribe from this group and stop receiving emails from it, send an email 
to [email protected].
To post to this group, send email to [email protected].
For more options, visit https://groups.google.com/d/optout.

Reply via email to