JSON解析错误:无法从START_OBJECT令牌中反序列化"byte []"实例 [英] JSON parse error: Cannot deserialize instance of `byte[]` out of START_OBJECT token

查看:387
本文介绍了JSON解析错误:无法从START_OBJECT令牌中反序列化"byte []"实例的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

如何以邮递员json格式发送base64图像.我以邮递员形式的数据添加了图像文件,并且以json格式对详细信息进行了编码,但是仅在尝试时我收到了一个错误的请求. [JSON解析错误:无法从START_OBJECT令牌中反序列化byte[]的实例].发现源中没有错误,但邮递员中json格式的实际问题.消息来源: https://github.com/arun0009/ocr-tess4j-rest

How to send a base64 image in postman json format. I was added image file in postman- body form data and it's encode details in json format but i got a bad request only in my attempts. [JSON parse error: Cannot deserialize instance of byte[] out of START_OBJECT token]. found that there is no error in source but the actual issue in json format in postman. SOURCE: https://github.com/arun0009/ocr-tess4j-rest

我应用了堆栈溢出解决方案,但它重复了同样的错误.我从堆栈溢出建议类型中以json格式添加了[]{}.

I applied a stack overflow solution but it's repeating the same error. I was adding [], {} in my json formats from stack overflow suggestion types.

CLASS:

public class Image  {
    private String id;
    private String userId;
    private byte[] image;
    private String extension;
    private String text;  }

控制器:

@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();     }

测试案例:

@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=lassLoader.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);  }

JSON:

    { "image": {  
               "userId": "arun0009", 
                "extension": ".png",    
                "text": "iVBORw0KGgoA"  
   }

JSON解析错误:

无法从START_OBJECT令牌中反序列化byte[]的实例;嵌套的异常是com.fasterxml.jackson.databind.exc.MismatchedInputException:无法在[Source:(PushbackInputStream);

Cannot deserialize instance of byte[] out of START_OBJECT token; nested exception is com.fasterxml.jackson.databind.exc.MismatchedInputException: Cannot deserialize instance of byte[] out of START_OBJECT token\n at [Source: (PushbackInputStream);

推荐答案

请求json的具体布局取决于JAX-RS框架的配置,但默认情况下,它是不带包装的普通JSON对象.在您的情况下,我将发送以下JSON:

The concrete layout of the request json depends on the configuration of the JAX-RS framework, but by default it is a plain JSON object without a wrapper. In your case, I would send this JSON:

{
   "userId": "arun0009", 
   "extension": ".png",    
   "text": null,
   "image": "BASE_64_ENCODED_IMAGE"
}

java端已经知道"期望的对象,因此添加的包装器不是必需的,并且(如您所注意到的)添加无效.

The java side already "knows" what object to expect, so the wrapper you added is not necessary and (as you noticed) not valid to add.

这篇关于JSON解析错误:无法从START_OBJECT令牌中反序列化"byte []"实例的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆