Saat menggunakan Postman untuk menguji metode @POST dari RESTEasy, saya mendapatkan kesalahan MalformedJsonException

Metode @POST saya

@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 saya

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 + ',' + "}";
    }
}

Inilah yang ingin saya tambahkan: https://i.imgur.com/XACBopY.png

Data dalam format json, {"id":"band1","name":"Mi Band 4","image":"https://i.imgur.com/7MLMnhW.jpg","price":30.0,"catalogId":"abc1"} misalnya

Kesalahan: https://i.imgur.com/8suya35.png

Sebelumnya saya mendapatkan kesalahan Expected BEGIN_OBJECT but was STRING at line 1 column 1 path $ tetapi kemudian saya menyadari metode toString() di kelas Produk adalah masalahnya, saya memperbaikinya dan menghasilkan kesalahan dalam pertanyaan.

Tolong bantu saya untuk memperbaiki kesalahan ini.

0
Minh Nguyên Nguyễn Đức 12 Mei 2021, 05:31

1 menjawab

Jawaban Terbaik

ToString() Anda salah untuk memulai - formulasi Json tidak benar. Jika Anda ingin menggunakan toString() untuk mengubah POJO Anda menjadi JSON, gunakan gaya JSON Apache commons lang3 di 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;
        }
    }
}

Outputnya adalah sebagai berikut: -

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

Sekarang, datang ke penggunaan. Jika Anda mengambil objek sebagai input itu sendiri sebagai badan permintaan POST, lalu mengapa tidak menggunakan 'Data.addProduct(product);'?

1
v2k 12 Mei 2021, 02:54