6

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

4
  • Your JSON request looks like another model class wraps the FreightCalculationRequest properties. Unwrap it, and it should work. Commented Dec 18, 2018 at 22:00
  • @TheHeadRush I tried this, and now instead of getting a response, i get an json parsing error. Commented Dec 18, 2018 at 22:17
  • This is a case of not following naming conventions. If you follow proper conventions, this will work fine. e.g. shipToZip not ShipToZip. It's odd anyway, since you're mixing naming conventions in your payloads. Commented Dec 18, 2018 at 22:23
  • I see the error I was getting was actually a different issue. Commented Dec 18, 2018 at 22:26

1 Answer 1

3

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;

    .
    .
    .
}
Sign up to request clarification or add additional context in comments.

5 Comments

Do I need to do this for the objects within the Request object
@boo You need to erap it because your JSON has FreightCalculationRequest at the root and not Products.
Okay I got it started and I added the annotation to Products and I now get a response of { "isCommercial": false, "shipToZip": null, "products": null, "Products": null } and when I add the annotations to all the variables, it leaves the lowercase names in the json response.
So I'm getting everything working except one last issue, now the products is showing whats in the json as a response, but it also duplicates it as a "Products" object to and so I have two products arrays in my response, trying to tweak it around, but I have my mapping set.
@boo Have a look at @JsonSetter and @JsonGetter annotations. You'll want them on your Getter-Setters.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.