JSON parse error: Cannot deserialize instance of `byte[]` out of START_OBJECT token

五迷三道 提交于 2019-12-25 18:04:12

问题


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

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

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

TEST CASE:

@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 parse error:

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);


回答1:


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"
}

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.



来源:https://stackoverflow.com/questions/57907068/json-parse-error-cannot-deserialize-instance-of-byte-out-of-start-object-to

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!