Unirest POST发送无法识别的字段(io.vertx.core.json.DecodeException错误)

时间:2019-01-18 05:09:38

标签: java json vaadin vert.x unirest

我试图使用Unirest单击按钮将Java对象从Vaadin前端发送到Vertx后端。在我的POST函数中,我有一个ObjectMapper,可以在执行POST到Vertx之前将Java对象映射到JSON字符串。但是,一旦在Vertx中收到此字符串,我就会得到Vaadin中没有的其他字段。如下面的错误所示,还有一个额外的“宽度”和“高度”,而我原来的是boxWidth和boxHeight。以前,在另一次运行中,我有一个额外的“名称”,而我原来的名称是boxName。另外,如UserService.java中的代码所示,实际上可以将json字符串成功保存到新文件中。

错误:

SEVERE: Unhandled exception
io.vertx.core.json.DecodeException: Failed to decode: Unrecognized field "width" (class io.vertx.starter.components.BoundingBox), not marked as ignorable (12 known properties: "boxHeight", "picID", "boxname", "boxcolour", "boxcategory", "boxWidth", "xcoordi", "ycoordi", "endY", "boxName", "bbID", "endX"])
 at [Source: (String)"{
  "xcoordi" : 356.0,
  "ycoordi" : 247.0,
  "boxWidth" : 216.0,
  "boxHeight" : 178.0,
  "endX" : 572.0,
  "endY" : 425.0,
  "picID" : "https://i.ebayimg.com/images/g/k7MAAOSwPfZZyfJD/s-l300.png",
  "boxname" : "lamppost1",
  "boxcategory" : "lights",
  "boxcolour" : "Aqua",
  "boxName" : "lamppost1",
  "width" : 216.0,
  "height" : 178.0
}"; line: 13, column: 18] (through reference chain: io.vertx.starter.components.BoundingBox["width"])

Vaadin UserService.java:

package com.vaadin.starter.beveragebuddy.service;

import com.fasterxml.jackson.core.JsonGenerationException;
import com.fasterxml.jackson.databind.JsonMappingException;
import com.fasterxml.jackson.databind.ObjectMapper;
import com.mashape.unirest.http.HttpResponse;
import com.mashape.unirest.http.JsonNode;
import com.mashape.unirest.http.Unirest;
import com.mashape.unirest.http.exceptions.UnirestException;
import com.vaadin.starter.beveragebuddy.ui.components.BoundingBox;
import com.vaadin.starter.beveragebuddy.ui.components.Canvas;
import org.json.JSONArray;
import org.json.JSONObject;

import java.io.File;
import java.io.IOException;
import java.util.ArrayList;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;


public class UserService {

    public JSONArray bbJsonArray;
    public JSONObject bbJsonObject;
    private static final Logger LOGGER = LoggerFactory.getLogger(UserService.class);
    public static ArrayList<BoundingBox> bb = Canvas.getArrayBoxes();
    public String jsonInString;

public void postAnnotations() {

        HttpResponse<JsonNode> response = null;

        try {
            response = Unirest.post("http://localhost:9080/api/annotations")
                    .header("accept", "application/json")
                    .header("content-type", "application/json") 
                    .body(jsonInString)
                    .asJson();
        } catch (UnirestException e) {
            e.printStackTrace();
        }

    }

    public void run() {
        ObjectMapper mapper = new ObjectMapper();

        BoundingBox dummyBB = createDummyObject();

        try {
            // Convert object to JSON string and save into a file directly
            mapper.writeValue(new File("sample/file/path"), dummyBB);

//            // Convert object to JSON string
//            jsonInString = mapper.writeValueAsString(dummyBB);
//            System.out.println(jsonInString);

            // Convert object to JSON string and pretty print
            jsonInString = mapper.writerWithDefaultPrettyPrinter().writeValueAsString(dummyBB);
            System.out.println(jsonInString);

        } catch (JsonGenerationException e) {
            e.printStackTrace();
        } catch (JsonMappingException e) {
            e.printStackTrace();
        } catch (IOException e) {
            e.printStackTrace();
        }

        System.out.println(dummyBB);

        postAnnotations();

    }

    private BoundingBox createDummyObject() {

        BoundingBox dummyBB = new BoundingBox("", "", "", "", 0.0, 0.0, 0.0, 0.0, 0.0, 0.0);

        for (int i = 0; i < bb.size(); i++) {

            dummyBB = new BoundingBox(bb.get(i).picID, bb.get(i).boxname, bb.get(i).boxcategory, bb.get(i).boxcolour, bb.get(i).xcoordi, bb.get(i).ycoordi, bb.get(i).boxWidth, bb.get(i).boxHeight, bb.get(i).endX, bb.get(i).endY);

        }

        return dummyBB;

    }
}

Vaadin BoundingBox.java:

package com.vaadin.starter.beveragebuddy.ui.components;

import org.json.JSONObject;

import java.util.concurrent.atomic.AtomicInteger;

public class BoundingBox {

    public double xcoordi = 0;
    public double ycoordi = 0;
    public double boxWidth = 0;
    public double boxHeight = 0;
    public double endX = 0;
    public double endY = 0;
//    public final int bbID;
    public String picID;
    public String boxname;
    public String boxcategory;
    public String boxcolour;
    private static final AtomicInteger COUNTER = new AtomicInteger();

    public BoundingBox(String picID, String boxname, String boxcategory, String boxcolour, double xcoordi, double ycoordi, double boxWidth, double boxHeight, double endX, double endY) {
//        this.bbID = COUNTER.getAndIncrement();
        this.picID = picID;
        this.boxname = boxname;
        this.boxcategory = boxcategory;
        this.boxcolour = boxcolour;
        this.xcoordi = xcoordi;
        this.ycoordi = ycoordi;
        this.boxWidth = boxWidth;
        this.boxHeight = boxHeight;
        this.endX = endX;
        this.endY = endY;
    }
//
//    public int getBbID() {
//        return bbID;
//    }

    public String getPicID() {
        return picID;
    }

    public void setPicID(String picID) {
        this.picID = picID;
    }

    public String getBoxName() {
        return boxname;
    }

    public void setName(String boxname) {
        this.boxname = boxname;
    }

    public String getBoxcategory() {
        return boxcategory;
    }

    public void setBoxcategory(String boxcategory) {
        this.boxcategory = boxcategory;
    }

    public String getBoxcolour() {
        return boxcolour;
    }

    public void setBoxcolour(String boxcolour) {
        this.boxcolour = boxcolour;
    }

    public double getXcoordi() {
        return xcoordi;
    }

    public void setXcoordi(double xcoordi) {
        this.xcoordi = xcoordi;
    }

    public double getYcoordi() {
        return ycoordi;
    }

    public void setYcoordi(double ycoordi) {
        this.ycoordi = ycoordi;
    }

    public double getWidth() {
        return boxWidth;
    }

    public void setWidth(double endX, double xcoordi) {
        boxWidth = endX - xcoordi;
    }

    public double getHeight() {
        return boxHeight;
    }

    public void setHeight(double endY, double ycoordi) {
        boxHeight = endY - ycoordi;
    }

    public double getEndX() {  // Bottom-right X coordinate of box
        return endX;
    }

    public void setEndX(double endX) {
        this.endX = endX;
    }

    public double getEndY() {  // // Bottom-right Y coordinate of box
        return endY;
    }

    public void setEndY(double endY) {
        this.endY = endY;
    }

    public JSONObject toJSON() {

        JSONObject bbJsonObject = new JSONObject();
        bbJsonObject.put("boxname", boxname);
        bbJsonObject.put("boxcategory", boxcategory);
        bbJsonObject.put("boxcolour", boxcolour);
        bbJsonObject.put("xcoordi", xcoordi);
        bbJsonObject.put("ycoordi", ycoordi);
        bbJsonObject.put("boxwidth", boxWidth);
        bbJsonObject.put("boxheight", boxHeight);

        return bbJsonObject;
    }

    @Override
    public String toString() {
        return "{" +
                "Name=" + boxname +
                ", Class=" + boxcategory +
                ", Colour=" + boxcolour +
                ", X=" + xcoordi +
                ", Y=" + ycoordi +
                ", Width=" + boxWidth +
                ", Height=" + boxHeight +
                '}';
    }
}

非常感谢您的帮助,谢谢!

1 个答案:

答案 0 :(得分:0)

这与Vert.x无关,这是纯杰克逊: 如果您不想解码其他属性,请使用:

@JsonIgnoreProperties(ignoreUnknown = true)