gpt4 book ai didi

java - RESTFul WebService - 如何将类变量表示为不同的 json 名称

转载 作者:行者123 更新时间:2023-12-02 10:39:54 25 4
gpt4 key购买 nike

我刚刚开始学习 Restful Web 服务,并正在完成一项作业,在调用特定 GET 方法时,我需要返回 Product 类对象详细信息。

我想退回 - {"product_id": 123,"price": 35.50,"currency": "SGD","total_items": 1000,"items_left": 450,"time_left": 100000}

但是我得到的响应为(注意重复项)-{"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}

我的产品类别是 -

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;
}
}

如何删除重复项并返回预期值(即使我的类变量具有不同的名称)?对于任何不正确/缺失的信息,我们深表歉意!

最佳答案

您似乎有另一个依赖项可以自动序列化您的对象。

如果是Jackson (Spring Boot 中的默认映射依赖项),您可以将 @XmlAttribute(name = "product_id") 替换为 @JsonProperty("product_id")

关于java - RESTFul WebService - 如何将类变量表示为不同的 json 名称,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/52993485/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com