Sameervb
Sameervb

Reputation: 421

RESTFul WebService - How to represent class variable to a different json name

I'm just starting to learn Restful webservices and working on an assignment where I need to return the Product class object details when a particular GET method is invoked.

I want to send back - {"product_id" : 123,"price" : 35.50,"currency" : "SGD","total_items" : 1000,"items_left" : 450,"time_left" : 100000}

However I'm getting the response as (Notice the duplicates) -{"availableQuantity":45,"currency":"SGD","items_left":45,"price":35.5,"product_id":1,"productId":1,"time_left":10000,"timeAvailable":10000,"total_items":100,"totalItems":100}

My Product Class is -

public class Product {
    @XmlAttribute(name = "product_id")
    private int productId;
    private double price;
    private String currency;
    @XmlAttribute(name = "total_items")
    private int totalItems;

    public int getTotalItems() {
        return totalItems;
    }

    public void setTotalItems(int totalItems) {
        this.totalItems = totalItems;
    }

    @XmlAttribute(name = "items_left")
    private int availableQuantity;
    @XmlAttribute(name = "time_left")
    private long timeAvailable;

    public int getProductId() {
        return productId;
    }

    public void setProductId(int productId) {
        this.productId = productId;
    }

    public double getPrice() {
        return price;
    }

    public void setPrice(double price) {
        this.price = price;
    }

    public String getCurrency() {
        return currency;
    }

    public void setCurrency(String currency) {
        this.currency = currency;
    }

    public int getAvailableQuantity() {
        return availableQuantity;
    }

    public void setAvailableQuantity(int availableQuantity) {
        this.availableQuantity = availableQuantity;
    }

    public long getTimeAvailable() {
        return timeAvailable;
    }

    public void setTimeAvailable(long timeAvailable) {
        this.timeAvailable = timeAvailable;
    }

    public Product(int id, double price, String currency, int totalItems, int itemsLeft, long timeLeft) {
        this.productId = id;
        this.price = price;
        this.currency = currency;
        this.totalItems = totalItems;
        this.availableQuantity = itemsLeft;
        this.timeAvailable = timeLeft;
    }
}

How do I remove the duplicates and return the expected values (even though my class variables have a different name) ? Apologies for any incorrect/missing information !

Upvotes: 0

Views: 848

Answers (2)

abhi3232
abhi3232

Reputation: 391

You may use below jackson annotations.

For the order of elements in response use @JsonPropertyOrder and to change the names in the response simply use @JsonProperty(value="NEW_NAME") annotation.

In your example it would be like

@JsonPropertyOrder({"productId","price","currency","totalItems"})
public class Product {

     @JsonProperty(value="product-id")
     private int productId;

     private double price;

     @JsonProperty(value="total_items")
     private int totalItems;

     private String currency;

      ................
      ...............

See this link for more information on Jackson Annotations

Upvotes: 0

LunaticJape
LunaticJape

Reputation: 1584

It seems that you have another dependency that serializes automatically your object.

If it's Jackson (default mapping dependency in Spring Boot), you can replace for example @XmlAttribute(name = "product_id") to @JsonProperty("product_id").

Upvotes: 1

Related Questions