I have an issue with postman json format. I want to post an image with it's encode image to store it in a mongodb instance. But I get an error 400. I have attached the image in formdata and it's encode details in json format. But still I get the same 400 error repeat
model class
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"
}
}
CODE:
@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 ofcom.tess4j.rest.model.Image
out of START_ARRAY token\n at [Source: (PushbackInputStream); line: 1, column: 1]", "path": "/ocr/v1/upload"