0

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.

4
  • Why would you use toString to convert your product to JSON (Gson has a toJson method) and then use fromJson to convert it back again to pass it to the Data.addProduct method? Couldn't you simply pass the product parameter to your Data.addProduct method? Commented May 12, 2021 at 2:42
  • Your data is in json format doesn't even have an end curly brace. That alone makes it invalid. Are you sure it's pasted correctly or is that actually your problem? Commented May 12, 2021 at 2:46
  • @HopeyOne i tried though, it still produced the same error Commented May 12, 2021 at 2:52
  • @Atmas thank you, it was a typo, i corrected it Commented May 12, 2021 at 2:52

1 Answer 1

1

Your toString() is faulty to begin with - the Json formulation isn't correct. If you want to use toString() anyways to convert your POJO into JSON, use apache commons lang3's JSON style in the toString().

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

The output is as follows:-

{"id":"1","name":"someone","image":"https://url","price":1.23,"catalogId":"11"} {"id":"1","name":"someone","image":"https://url","price":1.23,"catalogId":"11"}

Now, coming to the usage. If you are taking the object as an input itself as a POST request body, then why not simply use 'Data.addProduct(product);'?

Sign up to request clarification or add additional context in comments.

3 Comments

I'm confused a bit, as I'm new to this. I can call data from mongodb to server as json, but I do not know how to get data input back to mongodb. Can I add data to mongodb with raw json data input? If yes, please tell me, I'm willing to know it
And thank you, I'm learning your code to find a way improving my code.
@MinhNguyênNguyễnĐức, in mongo, everything is in terms of document. You can add direct json as well to it. Sample would be: geeksforgeeks.org/…. The recommended way is to save the POJO in mongo. Follow the official documentation from mongodb site here: docs.mongodb.com/guides/server/insert You are welcome. Am happy that the code makes sense to you.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.