1

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 of com.tess4j.rest.model.Image out of START_ARRAY token\n at [Source: (PushbackInputStream); line: 1, column: 1]", "path": "/ocr/v1/upload"

1 Answers1

0

Well, I really think that your issue is in conversion. try to use annotations like that to ignore the fields that you aren't passing in JSON:

@JsonIgnore
@JsonProperty(value = "user_password")
public String userPassword;

References: Ignore fields from Java object dynamically while sending as JSON from Spring MVC.


EDIT

Your Image class could have a "private string imageReceived"(Base64String) and a "private byte[] image". You could receive a string and decrypt to a byte[]. HTTP Transactions can only pass string values by the way you're already converting, you only need to receive this string value in the Web API

Jose Leles
  • 178
  • 1
  • 10
  • Yes, tried with that one also but same error repeat – Vijay Alangaram Samran Aug 22 '19 at 04:17
  • how does your back-end code work? I guess that can also be a problem in json conversion to an object – Jose Leles Aug 22 '19 at 12:48
  • Well, I really thing that your issue is in conversion. Try to use annotations like that to ignore the fields that you arent passing in json: @JsonIgnore @JsonProperty(value = "user_password") public String userPassword; References: https://stackoverflow.com/questions/23101260/ignore-fields-from-java-object-dynamically-while-sending-as-json-from-spring-mvc – Jose Leles Aug 23 '19 at 12:15
  • I was applied this @Jsonignore and JsonProperty for the field which is not used in json conversion[postman] but the error still exist . bad request and internal server error. I couldn't found that exact way to pass field in postman. I was added image and it's encode details in postman but it's not accept the json format. I was applied many stack overflow suggestion on this topic but won't works. SOURCE URL: https://github.com/arun0009/ocr-tess4j-rest – Vijay Alangaram Samran Sep 11 '19 at 05:16
  • So since the problem is not some unmapped field, you can use a base64StringImage field for example, because I think error could be related to the field type, it must be trying to parse an array and found a string. – Jose Leles Sep 11 '19 at 12:54
  • base64StringImage field means , shall I add additional string type image filed in my image class or add base64String in postman request ?. Because already i have "image" byte field name in my image class. So additional can't add with same name it must be a "private String image1" only. Same filed name in same class not possible. I was tried out with Map interface in this case but won't solve. https://stackoverflow.com/questions/16467035/spring-mvc-doesnt-deserialize-json-request-body/16467400 – Vijay Alangaram Samran Oct 25 '19 at 05:22
  • Your Image class could have a "private string imageReceived"(Base64String) and a "private byte[] image". You could receive a string and decrypt to a byte[]. HTTP Transactions can only pass string values by the way you're already converting, you only need to receive this string value in the Web API – Jose Leles Oct 25 '19 at 20:29