作者热门文章
- html - 出于某种原因,IE8 对我的 Sass 文件中继承的 html5 CSS 不友好?
- JMeter 在响应断言中使用 span 标签的问题
- html - 在 :hover and :active? 上具有不同效果的 CSS 动画
- html - 相对于居中的 html 内容固定的 CSS 重复背景?
我刚刚开始学习 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/
我是一名优秀的程序员,十分优秀!