Could not write JSON: No serializer found for class FileDescriptor and no properties discovered to create BeanSerializer returning PDF from rest

Viewed 903

I have a java rest service that should return a PDF.

This is my code:

@RequestMapping(value = "/pdf", method = RequestMethod.GET)
@Produces("application/pdf")
public javax.ws.rs.core.Response getPdf() throws Exception {
    try {
        File file = new File("xxx");
        FileInputStream fileInputStream = new FileInputStream(file);
        javax.ws.rs.core.Response.ResponseBuilder responseBuilder = javax.ws.rs.core.Response.ok((Object) fileInputStream);
        responseBuilder.type("application/pdf");
        responseBuilder.header("Content-Disposition", "attachment; filename=" + file.getName());
        return responseBuilder.build();
        
    } catch(Exception ex) {
        return javax.ws.rs.core.Response.serverError().build();
    }
}

When I test it with POSTMAN I always get this error:

org.springframework.http.converter.HttpMessageNotWritableException: Could not write JSON: No serializer found for class java.io.FileDescriptor and no properties discovered to create BeanSerializer (to avoid exception, disable SerializationFeature.FAIL_ON_EMPTY_BEANS) (through reference chain: org.glassfish.jersey.message.internal.OutboundJaxrsResponse["context"]->org.glassfish.jersey.message.internal.OutboundMessageContext["entity"]->java.io.FileInputStream["fd"]); nested exception is com.fasterxml.jackson.databind.exc.InvalidDefinitionException: No serializer found for class java.io.FileDescriptor and no properties discovered to create BeanSerializer (to avoid exception, disable SerializationFeature.FAIL_ON_EMPTY_BEANS) (through reference chain: org.glassfish.jersey.message.internal.OutboundJaxrsResponse["context"]->org.glassfish.jersey.message.internal.OutboundMessageContext["entity"]->java.io.FileInputStream["fd"])

I don't understand why it talks about JSON if I'm trying to return a PDF

What's wrong with this code?

Thanks

UPDATE: After changing code to the following, I don't get the error:

File file = orderLabelService.getLabelsSummary(supplierId, date);

return javax.ws.rs.core.Response.ok(file, MediaType.APPLICATION_OCTET_STREAM)
          .header("Content-Disposition", "attachment; filename=\"" + file.getName() + "\"" )
          .build();

But the result is always a json:

{
    "context": {
        "headers": {
            "Content-Type": [
                {
                    "type": "application",
                    "subtype": "octet-stream",
                    "parameters": {},
                    "wildcardType": false,
                    "wildcardSubtype": false
                }
            ],
            "Content-Disposition": [
                "attachment; filename=\"12-20200313.pdf\""
            ]
        },
        "configuration": null,
        "entity": "e:\\content\\12-20200313.pdf",
        "entityType": "java.io.File",
        "entityAnnotations": [],
        "entityStream": {
            "closed": false,
            "committed": false
        },
        "length": -1,
        "location": null,
        "language": null,
        "lastModified": null,
        "date": null,
        "acceptableMediaTypes": [
            {
                "type": "*",
                "subtype": "*",
                "parameters": {},
                "quality": 1000,
                "wildcardType": true,
                "wildcardSubtype": true
            }
        ],
        "acceptableLanguages": [
            "*"
        ],
        "committed": false,
        "lengthLong": -1,
        "mediaType": {
            "type": "application",
            "subtype": "octet-stream",
            "parameters": {},
            "wildcardType": false,
            "wildcardSubtype": false
        },
        "allowedMethods": [],
        "requestCookies": {},
        "entityClass": "java.io.File",
        "responseCookies": {},
        "entityTag": null,
        "links": [],
        "stringHeaders": {
            "Content-Type": [
                "application/octet-stream"
            ],
            "Content-Disposition": [
                "attachment; filename=\"12-20200313.pdf\""
            ]
        }
    },
    "status": 200,
    "length": -1,
    "location": null,
    "language": null,
    "lastModified": null,
    "date": null,
    "entity": "e:\\content\\12-20200313.pdf",
    "cookies": {},
    "mediaType": {
        "type": "application",
        "subtype": "octet-stream",
        "parameters": {},
        "wildcardType": false,
        "wildcardSubtype": false
    },
    "allowedMethods": [],
    "metadata": {
        "Content-Type": [
            {
                "type": "application",
                "subtype": "octet-stream",
                "parameters": {},
                "wildcardType": false,
                "wildcardSubtype": false
            }
        ],
        "Content-Disposition": [
            "attachment; filename=\"12-20200313.pdf\""
        ]
    },
    "entityTag": null,
    "links": [],
    "stringHeaders": {
        "Content-Type": [
            "application/octet-stream"
        ],
        "Content-Disposition": [
            "attachment; filename=\"12-20200313.pdf\""
        ]
    },
    "statusInfo": "OK",
    "headers": {
        "Content-Type": [
            {
                "type": "application",
                "subtype": "octet-stream",
                "parameters": {},
                "wildcardType": false,
                "wildcardSubtype": false
            }
        ],
        "Content-Disposition": [
            "attachment; filename=\"12-20200313.pdf\""
        ]
    }
}

I need a pdf content to save or open on client

Thanks

0 Answers
Related