gpt4 book ai didi

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

转载 作者:太空宇宙 更新时间:2023-11-04 09:55:06 27 4
gpt4 key购买 nike

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

错误:

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

非常感谢任何帮助,谢谢!

最佳答案

这与 Vert.x 无关,这是纯粹的 Jackson :如果您不想解码其他属性,请使用以下注释您的类:

@JsonIgnoreProperties(ignoreUnknown = true)

关于java - Unirest POST 发送无法识别的字段(io.vertx.core.json.DecodeException 错误),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/54248040/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com