- android - 多次调用 OnPrimaryClipChangedListener
- android - 无法更新 RecyclerView 中的 TextView 字段
- android.database.CursorIndexOutOfBoundsException : Index 0 requested, 光标大小为 0
- android - 使用 AppCompat 时,我们是否需要明确指定其 UI 组件(Spinner、EditText)颜色
我正在尝试使用 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/
我正在创建一个执行 POST 操作的 Java 应用程序。我需要执行 HttpRequestWithBody 来发送 JSONObject。我正在使用 Unirest 来完成这项任务。调试时,程序会在
嗨,我是新的 Rest,正在开发 API,其中我从客户端获得的请求参数每次都不同。然后我将不得不调用另一个 API。我想编写一种方法来从 3rd 方 API 获取 JSON 数据。我在网上查了一下,所
我们使用 Unirest 1.5.4 for java。我们的服务器以 204 - NO_CONTENT 状态和空响应正文响应 DELETE 调用。当我执行同步删除调用时,以下调用将以 SocketT
我正在使用 Unirest Java 客户端,如下所示连接到外部 API: public static String loginAsAdmin(String authenticationURL ,
您能否告诉我使用 Unirest 作为 REST 客户端禁用证书验证的方法。 我将 Unirest 与 Java Spring 结合使用。以下是我的源代码: try { HttpRespons
我的网络应用程序似乎内存不足,我认为这是由于线程泄漏。线程似乎一直在等待,并且越来越大,直到内存达到堆大小的顶部。在空闲且不使用 Web 应用程序时,Web 应用程序的本地 Tomcat 服务器上的线
我刚开始接触 API(官方和非官方),我正在使用一个名为 JavaSnap 的 API。 。我一直在搞乱示例代码的非常基本的实现,但遇到了错误。这是非常基本的代码: Snapchat snapchat
我们正在调用后端 2 个 REST 服务(A、B)(GET 方法),这两个服务具有不同的凭据(用户名和密码),但我们遇到了奇怪的行为: 1-如果我们先调用A,身份验证成功,但如果我们调用B,我们将收到
所以我正在使用 java 中的 REST API。我的 POST 请求可以正常工作,但我的代码对我来说似乎有点低效,例如: HttpResponse jsonResponse = Unires
使用 Unirest 时,程序不会退出,直到我们通过调用 Unirest.shutdown() 手动关闭每个线程。如果我只需要提出一个请求,那很简单: private static void asyn
我有一个像这样的类,它使用 Unirest API 进行 HTTP POST 调用 - public class MyClass{ public MyObject getData(){
我正在使用 Unirest 库通过以下代码行调用公开可用的 REST 端点: public void callRest() { String url = "https://upstream.c
制作用于连接 QwintryLogistics API ( http://logistics.qwintry.com ) 的服务,在 Java + Unirest 库上编码,但 API 的所有文档都是
我有一个接收字符串响应的 unirest 请求,但在解析其结果时遇到问题。我做了一项研究,但我看到的只是有关如何解析 json 请求的教程,这显然不是我需要的。不管怎样,请求看起来像这样: publi
我的项目是使用 Spring Boot 框架用 Java 编程语言编写的。我使用 Unirest 库来获取对某些 API 的请求,并且我想以表单形式获取响应,如下所示 List下面我写了一些我尝试提出
我正在尝试通过 PHP 获取从 unirest Node 模块发送的数据。这是我的 Node 代码: unirest.post('http://example.com/api') .h
我正在使用Unirest library用于使用 Python 发出异步 Web 请求。我已阅读文档,但无法找到是否可以使用代理。也许我只是瞎了,有办法将它与 Unirest 一起使用吗? 或者还有其
我尝试使用 Unirest.get(...).asObjectAsync(...) 使用计划任务更新资源。要停止使用 Unirest 的程序,您需要调用 Unirest.shutdown(); 以退出
Unirest 关于代理设置的文档提到要使用: Unirest.setProxy(new HttpHost("127.0.0.1", 8000)); 由于我使用的代理需要身份验证,因此我也需要指定凭据
我想使用 jsonWhois api,但它使用 Unirest 发出服务器请求,看起来它不再维护,无论如何我更愿意使用 curl。 如何将此代码转换为使用 Curl?? $response = Uni
我是一名优秀的程序员,十分优秀!