Forum Discussion

fjuillet's avatar
fjuillet
New Member
5 years ago

Cannot receive the binary content of Single File Upload whith the codegen spring server stub

Hello, 

 

i wrote this operation openapi: 3.0.0 definition 

 

/cev2ddoc:
    post:
      tags:
        - cev
      summary: encode a cev pattern from model
      operationId: encodeCev2ddoc
      description: generate a datamatrix with content in body
      requestBody:
        content:
          application/json:
            schema:
              $ref: '#/components/schemas/Cev2ddoc'
        description: cev 2d-doc parameters
      responses:
        '200':           # Response
          description: successful operation
          content:       # Response body
            image/*:     # Media type, Can be image/png, image/svg, image/gif, etc.
              schema: 
                type: string
                format: binary

When i generate a spring server stub with codegen option i got the this interface :

 

 

   @ApiOperation(value = "decode a cev pattern", nickname = "decodeCev2ddoc", notes = "", response = Cev2ddoc.class, tags={ "cev", })
    @ApiResponses(value = { 
        @ApiResponse(code = 200, message = "successful operation", response = Cev2ddoc.class) })
    @RequestMapping(value = "/cev2ddoc/decode",
        produces = { "application/json" }, 
        consumes = { "application/octet-stream" },
        method = RequestMethod.POST)
    default ResponseEntity<Cev2ddoc> decodeCev2ddoc(@ApiParam(value = ""  )  @Valid @RequestBody Object body) {
        if(getObjectMapper().isPresent() && getAcceptHeader().isPresent()) {
        } else {
            log.warn("ObjectMapper or HttpServletRequest not configured in default Cev2ddocApi interface so no example is generated");
        }
        return new ResponseEntity<>(HttpStatus.NOT_IMPLEMENTED);
    }

My issue is that i don't know how to get the binary content of the uploaded file ??

 

I try to cast the input Object type whith no result ...

 

try {
            if (body == null) {
                // do nothing
            } else if (body instanceof String) {
                String stringBody = ((String) body);
            } else if (body instanceof File) {

                File fileBody = (File) body;
                image = ImageIO.read(fileBody);

            } else if (body instanceof InputStream) {

                InputStream inputStreamBody = (InputStream) body;
                image = ImageIO.read(inputStreamBody);

            } else if (body instanceof Resource) {

                Resource resourceBody = (Resource) body;
                InputStream imageStream = resourceBody.getInputStream();
                image = ImageIO.read(imageStream);

            } else if (body instanceof Buffer) {
                Buffer bufferBody = (Buffer) body;
            } else if (body instanceof byte[]) {
                byte[] bodyBytes = (byte[]) body;
            } else {
                log.error("Can't detemine body type");
            }
        } catch (IOException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }

 

Do i have to tuse a Multipart Request ?

Is the spring codegen bugged ?

 

Any help would be greatfull :)

 

 

No RepliesBe the first to reply