CLASS:
//@JsonIgnoreProperties(ignoreUnknown = true)
public class Image (with and without this one--->> implements java.io.Serializable)
{
@Id
private String id;
private String userId;
private byte[] image;
private String extension;
private String text;
//getters & setters and //Generate to string()
public Image() { }
public Image( String id, String userId, byte[] image, String extension, String text)
{
this.id = id;
this.userId = userId;
this.image = image;
this.extension = extension;
this.text = text; } }
控制器:
@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);
}
JSON1 [JSON解析错误:无法构造``的实例(尽管至少存在一个创建者)]
{
"userId": "arun0009",
"extension": ".png",
"text" : null,
"image" : "ENCODE DETAILS OF IMAGe"
}
JSON2 [JSON解析错误:无法反序列化START_ARRAY令牌中的“”实例; ]
{ "image" : [ {
"userId": "arun0009",
"extension": ".png",
"text" : null,
"image" : "ENCODE DETAILS OF IMAGe"
}]}
大家好,我无法解决此错误。我试图通过邮递员发布此详细信息,但出现此错误。我被应用了很多堆栈溢出建议,例如添加json依赖项,并尝试使用不同的json格式,实现java.io.Serializable,添加@ JsonDeserialize,objectMapper。和许多解决方案。
没有任何帮助我解决此错误。虽然打json格式1时出现错误“无法构造``(尽管至少存在一个Creator的实例)”,但在使用第二个json格式时却显示为上述错误。谁能帮我解决这个问题??
来源:https://github.com/arun0009/ocr-tess4j-rest
预先感谢朋友。