Json request body to java object with spring-boot

boo :

I have the following JSON Request

{
  "FreightCalculationRequest": {
    "Products": [
      {
        "sku": "123",
        "size": "S",
        "quantity": "1",
        "shipAlone": "True",
        "itemType": "Shoe"
      },
      {
        "sku": "123",
        "size": "S",
        "quantity": "1",
        "shipAlone": "True",
        "itemType": "Shoe"
      }
    ],
    "ShipToZip": "54452",
    "IsCommercial": "True"
  }
}

I am trying to send this request to the API controller method as a custom java object, and then return that same object as a json formatted string. I am getting a response through postman however, for products, and shiptoZip i get a null, and for isCommercial I get false, but i don't even have false as a value for isCommercial in the request. What's going on? I don't know how to debug very well in Java as i basically am checking my app every time by typing mvn spring-boot:start

here is my object that I am returning and using as a parameter into the controller method.

public class FreightCalculationRequest {

    private Product[] Products;
    private String ShipToZip;
    private boolean IsCommercial;

    public Product[] getProducts() { return this.Products; }
    public void setProducts(Product[] itemsRequest) { this.Products = itemsRequest; }

    public String getShipToZip() { return this.ShipToZip; }
    public void setShipToZip(String ShipToZip) { this.ShipToZip = ShipToZip; }

    public boolean getIsCommercial() { return this.IsCommercial; }
    public void setIsCommercial(boolean IsCommercial) { this.IsCommercial = IsCommercial; }

}

and here is the controller method im calling

@RequestMapping(value = "/test", produces = MediaType.APPLICATION_JSON_VALUE,  method = RequestMethod.POST)
FreightCalculationRequest TestCall(@RequestBody FreightCalculationRequest calculationRequest) {
    return calculationRequest;

}

Why is my response not showing the same as the request coming in.

update: I added @JsonProperty to my variables, and now the response looks like such

{
    "isCommercial": false,
    "shipToZip": null,
    "products": null,
    "Products": null,
    "ShipToZip": null,
    "IsCommercial": false
}

Kind of lost a bit, also realized I can save my changes while mvn is running and it will auto compile the changes

Update: So the itemType in my json was actually throwing an error when I initally remove the wrapping of "FreightCalculationRequest" in the json response, so i thought that was the issue, however itemType is actually an object in the code so it was due to me not putting in a valid property and reading the json parsing error thoroughly, There were two solutions for me, wrapping the response in another class, or remove the FreightCalculationWrapping.

I also learned that I need to add @JsonProperty to map the json

Thanks SO

user2004685 :

I am getting a response through postman however, for products, and shiptoZip i get a null, and for isCommercial I get false, but i don't even have false as a value for isCommercial in the request. What's going on?

You'll have to wrap the FreightCalculationRequest in a new model class.

Make a new Wrapper class,

public class FreightCalculationRequestWrapper {
    @JsonProperty("FreightCalculationRequest")
    private FreightCalculationRequest freightCalculationRequest;

    ...
}

Use this new Wrapper class to handle your requests:

@RequestMapping(value = "/test", produces = MediaType.APPLICATION_JSON_VALUE,  method = RequestMethod.POST)
FreightCalculationResponse TestCall(@RequestBody FreightCalculationRequestWrapper calculationRequest) {
    return calculationRequest;

}

Also, the property names in your JSON start with a capital letter.

If you are using Jackson then you can use @JsonProperty(...) annotation on your model fields to map them properly.

For Example:

public class FreightCalculationRequest {
    @JsonProperty("Products")
    private Product[] Products;

    .
    .
    .
}

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=70753&siteId=1