Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Json request body to java object with spring-boot

Tags:

java

spring

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

like image 899
boo Avatar asked Dec 18 '18 21:12

boo


People also ask

How pass JSON object in POST request in Java Spring boot?

Send JSON Data in POST Spring provides a straightforward way to send JSON data via POST requests. The built-in @RequestBody annotation can automatically deserialize the JSON data encapsulated in the request body into a particular model object. In general, we don't have to parse the request body ourselves.

How do I request body in spring boot?

Simply put, the @RequestBody annotation maps the HttpRequest body to a transfer or domain object, enabling automatic deserialization of the inbound HttpRequest body onto a Java object. Spring automatically deserializes the JSON into a Java type, assuming an appropriate one is specified.

Can we pass request body in get method in spring boot?

No, We cannot send Body with GET Method. The Purpose of GET is to retrieve data. If i take example of database then the select query is a kind of GET. But if you want to pass some values through GET method then you can do that by passing the input as a query param.

What is @RequestBody and @ResponseBody annotation in Spring?

By using @RequestBody annotation you will get your values mapped with the model you created in your system for handling any specific call. While by using @ResponseBody you can send anything back to the place from where the request was generated. Both things will be mapped easily without writing any custom parser etc.


1 Answers

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;

    .
    .
    .
}
like image 133
user2004685 Avatar answered Oct 02 '22 16:10

user2004685