gpt4 book ai didi

java - json postman 关于使用其编码图像发布图像的问题

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

我对 postman json 格式有疑问。我想发布一张带有编码图像的图像,以将其存储在 mongodb 实例中。但我收到错误 400。我已将图像附加到 formdata 中,并且它以 json 格式编码详细信息。但我仍然收到相同的 400 错误重复

模型类

public class Image {

@Id
private String id;

private String userId;

private byte[] image;

private String extension;

private String text;
} /with getters and setter and constructors

Controller

 @RequestMapping(value = "ocr/v1/upload", method = RequestMethod.POST, consumes = MediaType.APPLICATION_JSON_VALUE, produces = MediaType.APPLICATION_JSON_VALUE)
public Status doOcr(@RequestBody Image image) throws Exception {
try {
ByteArrayInputStream bis = new ByteArrayInputStream (Base64.decodeBase64 (image.getImage()));
Tesseract tesseract = new Tesseract(); // JNA Interface Mapping
String imageText = tesseract.doOCR(ImageIO.read(bis));
image.setText(imageText);
repository.save(image);
LOGGER.debug("OCR Result = " + imageText);
} catch (Exception e) {
LOGGER.error("TessearctException while converting/uploading image: ", e);
throw new TesseractException();
}
return new Status("success"); }

JSON:

    {   
"image": {
"userId": "arun0009",
"extension": ".png",
"text": "WlgSmI3XGrnq31Uy6Vfnuo/qnHz1K8Z1+e4flJXk"
}
}

代码:

    @Test
public void testDoOcr() throws IOException
{
Map<String, String> headers = new HashMap<String, String>();
headers.put("Accept", MediaType.APPLICATION_JSON_VALUE);
headers.put("Content-Type", MediaType.APPLICATION_JSON_VALUE);

Image image = new Image();
InputStream inputStream = ClassLoader.getSystemResourceAsStream("eurotext.png");
image.setUserId("arun0009");
image.setExtension(".png");
image.setImage(Base64.encodeBase64(IOUtils.toByteArray(inputStream)));
String response = given().contentType("application/json").headers(headers).body(image).when().post("http://localhost:8080/ocr/v1/upload").then()
.statusCode(200).extract().response().body().asString();
System.out.println(response);
}

"status": 400, "error": "Bad Request", "message": "JSON parse error: Cannot deserialize instance of com.tess4j.rest.model.Image out of START_ARRAY token; nested exception is com.fasterxml.jackson.databind.exc.MismatchedInputException: Cannot deserialize instance of com.tess4j.rest.model.Image out of START_ARRAY token\n at [Source: (PushbackInputStream); line: 1, column: 1]", "path": "/ocr/v1/upload"

最佳答案

嗯,我真的认为你的问题在于转换。尝试使用这样的注释来忽略未在 JSON 中传递的字段:

@JsonIgnore
@JsonProperty(value = "user_password")
public String userPassword;

引用文献:Ignore fields from Java object dynamically while sending as JSON from Spring MVC .

<小时/>

编辑

您的 Image 类可能有一个“私有(private)字符串 imageReceived”(Base64String) 和一个“私有(private) byte[] 图像”。您可以接收一个字符串并解密为 byte[]。 HTTP 事务只能通过您已经转换的方式传递字符串值,您只需要在 Web API 中接收该字符串值

关于java - json postman 关于使用其编码图像发布图像的问题,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/57592681/

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