while using Postman to test the @POST method of RESTEasy, I got the error MalformedJsonException
My @POST method
@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();
}
My 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 + ',' + "}";
}
}
This is what I want to add: https://i.sstatic.net/vaBSe.png
The data is in json format, {"id":"band1","name":"Mi Band 4","image":"https://i.imgur.com/7MLMnhW.jpg","price":30.0,"catalogId":"abc1"} for examle
The error: https://i.sstatic.net/eiFSj.png
Earlier I got the error Expected BEGIN_OBJECT but was STRING at line 1 column 1 path $ but then I realized toString() method in Product class was the problem, I fixed it and it produced the error in the question.
Please help me to fix this error.
toStringto convert your product to JSON (Gsonhas atoJsonmethod) and then usefromJsonto convert it back again to pass it to theData.addProductmethod? Couldn't you simply pass theproductparameter to yourData.addProductmethod?