簡體   English   中英

JSON 解析錯誤:無法構造`com.tess4j.rest.model.Image`的實例(盡管至少存在一個創建者):

[英]JSON parse error: Cannot construct instance of `com.tess4j.rest.model.Image` (although at least one Creator exists):

我有 JSON 解析錯誤

JSON parse error: Cannot construct instance of `com.tess4j.rest.model.Image` (although at least one Creator exists): no String-argument constructor/factory method to deserialize from String value ('1'); nested exception is com.fasterxml.jackson.databind.exc.MismatchedInputException: Cannot construct instance of `com.tess4j.rest.model.Image` (although at least one Creator exists): no String-argument constructor/factory method to deserialize from String value ('1')\n at [Source: (PushbackInputStream); line: 2, column: 8] (through reference chain: java.util.LinkedHashMap[\"id\"]) 

並且無法解決一次。

CLASS:

public class Image {
    @Id   
    private String id;
    private String userId;
    @JsonProperty("image")
    private  byte[] image;
    private String extension;
    private String text;
//get & setters }

CONTROLLER:

 @RequestMapping(value = "ocr/v1/upload", method = RequestMethod.POST,consumes= MediaType.APPLICATION_JSON_VALUE, produces = MediaType.APPLICATION_JSON_VALUE)
        public Status doOcr( @RequestBody Map<String, Image> image ) throws Exception {
try {
  ByteArrayInputStream bis = new ByteArrayInputStream(Base64.decodeBase64(((Image) image).getImage()));
               Tesseract tesseract = new Tesseract(); // JNA Interface Mapping
                String imageText = tesseract.doOCR(ImageIO.read(bis));
                ((Image) 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");       }

 @RequestMapping(value = "ocr/v1/images/users/{userId}", method = RequestMethod.GET, consumes = MediaType.APPLICATION_JSON_VALUE, produces = MediaType.APPLICATION_JSON_VALUE)
        public List<Image> getUserImages(@PathVariable String userId) throws Exception {
            List<Image> userImages = new ArrayList<>();
            try {
                userImages = repository.findByUserId(userId);
            } catch (Exception e) {
                LOGGER.error("Exception occurred finding image for userId: {} ", userId, e);
                throw new Exception();
            }            return userImages;        }

測試用例:

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

    @Test
    public void testGetUserImages() 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);
        String response = given().contentType("application/json").headers(headers).when()
            .pathParam("userId", "arun0009").get("http://localhost:8080/ocr/v1/images/users/{userId}")
            .asString();
        System.out.println(response);
    }

JSON

{ 
    "id": "1",
    "userId": "arun0009",
    "extension": ".png",
    "text" : null,
    "image" : "ENCODE DETAILS OF IMAGe"
}

嘗試了不同的 json 格式,並在構建 gradle 中添加了 json 依賴項,但相同的錯誤重復。 無法解決。

圖片

只需為Image class 默認創建一個空的構造函數,Jackson 總是要求空的構造函數。

public Image() 
}

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM