[英]MalformedJsonException while converting JSON data to Java Object
在使用 Postman 测试 RESTEasy 的 @POST 方法时,出现错误MalformedJsonException
我的@POST 方法
@POST
@Path("service/product")
@Consumes("application/json")
public Object setProductData(Product product) {
String result = product.toString().trim();
Gson gson = new Gson();
Data.addProduct(gson.fromJson(result, Product.class));
return Response.status(200).entity(result).build();
}
我的 model
public class Product {
private String id;
private String name;
private String image;
private double price;
private String catalogId;
public Product(String id, String name, String image, double price, String catalogId) {
this.id = id;
this.name = name;
this.image = image;
this.price = price;
this.catalogId = catalogId;
}
public Product() {
}
public String getId() {
return id;
}
public void setId(String id) {
this.id = id;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public String getImage() {
return image;
}
public void setImage(String image) {
this.image = image;
}
public double getPrice() {
return price;
}
public void setPrice(double price) {
this.price = price;
}
public String getCatalogId() {
return catalogId;
}
public void setCatalogId(String catalogId) {
this.catalogId = catalogId;
}
@Override
public String toString() {
return "{" +
"id='" + id + ',' +
"name='" + name + ',' +
"image='" + image + ',' +
"price='" + price + ',' +
"catalogId='" + catalogId + ',' + "}";
}
}
这就是我要添加的内容: https://i.imgur.com/XACBopY.png
数据为json格式, {"id":"band1","name":"Mi Band 4","image":"https://i.imgur.com/7MLMnhW.jpg","price":30.0,"catalogId":"abc1"}
例如
错误: https://i.imgur.com/8suya35.png
早些时候我收到错误Expected BEGIN_OBJECT but was STRING at line 1 column 1 path $
但后来我意识到产品 class 中的toString()
方法是问题所在,我修复了它并在问题中产生了错误。
请帮我解决这个错误。
您的 toString() 一开始就有问题 - Json 公式不正确。 如果您想使用 toString() 将 POJO 转换为 JSON,请在 toString 中使用 apache commons lang3 的 JSON 样式。
import com.google.gson.Gson;
import org.apache.commons.lang3.builder.ToStringBuilder;
import org.apache.commons.lang3.builder.ToStringStyle;
public class Test1 {
public static void main(String[] args) {
Product product = new Product("1", "someone", "https://url", 1.23, "11");
System.out.println(product);
Gson gson = new Gson();
Product product1 = gson.fromJson(product.toString().trim(), Product.class);
System.out.println(product1);
}
private static class Product {
private String id;
private String name;
private String image;
private double price;
private String catalogId;
public Product() {
}
public Product(String id, String name, String image, double price, String catalogId) {
this.id = id;
this.name = name;
this.image = image;
this.price = price;
this.catalogId = catalogId;
}
@Override
public String toString() {
return new ToStringBuilder(this, ToStringStyle.JSON_STYLE)
.append("id", id)
.append("name", name)
.append("image", image)
.append("price", price)
.append("catalogId", catalogId)
.toString();
}
public String getId() {
return id;
}
public Product setId(String id) {
this.id = id;
return this;
}
public String getName() {
return name;
}
public Product setName(String name) {
this.name = name;
return this;
}
public String getImage() {
return image;
}
public Product setImage(String image) {
this.image = image;
return this;
}
public double getPrice() {
return price;
}
public Product setPrice(double price) {
this.price = price;
return this;
}
public String getCatalogId() {
return catalogId;
}
public Product setCatalogId(String catalogId) {
this.catalogId = catalogId;
return this;
}
}
}
output如下:-
{"id":"1","name":"someone","image":"https://url","price":1.23,"catalogId":"11"} {"id":"1 ","name":"someone","image":"https://url","price":1.23,"catalogId":"11"}
现在,开始使用。 如果您将 object 作为输入本身作为 POST 请求正文,那么为什么不简单地使用“Data.addProduct(product);”呢?
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.