Java – Generates application/octet-stream and application/json

Generates application/octet-stream and application/json… here is a solution to the problem.

Generates application/octet-stream and application/json

I have an endpoint that usually returns documents like PDF, Word, etc. There is user input accompanying this endpoint, but so I want to validate it.

The problem is that when I validate the input and want to return some kind of bad JSON response, I end up with an error similar to


Execution of GET/foo/generateBar failed: NoMessageBodyWriterFoundFailure: Response object of type MessageBodyWriter not found: FooResponse: application/octet-stream of media type

I’m using @Produces({"application/octet-stream", "application/json"}) on the endpoint

Is there a way to avoid this error by returning JSON and a different file format? I got the impression that the @Produces I was using was doing, but I might not be at all confused.

Also, I have to note that I use window.open on the frontend to call this endpoint.

Solution

In Java and javax.ws.rs.core.Response, you should try something like this in header:

               Response.status(response.getStatus())
                       .header("Accept", "application/json")
                       .header("Content-type", "application/json")
                       .entity(entity)
                       .build();

Related Problems and Solutions