I have a webservice that returns the text of a file. If something goes wrong, I want to send back a json object that describes the error. I've read that it's possible to have multiple MediaTypes in the @Produces annotation, but I can't get it to work (It just goes with the first MediaType). I probably need a way to say "produce text here, produce json here".
@GET
//@Produces( {MediaType.TEXT_PLAIN, MediaType.APPLICATION_JSON} ) doesn't work either.
@Produces( MediaType.TEXT_PLAIN )
@Path( "/{filename}" )
public Response get( @PathParam( "filename" ) String filename ) {
String text;
try {
text = service.getFileText( service.getDirectory(), filename );
}
catch ( MyFileNotFoundException ex ) {
//Make a JSON object describing the error.
return ErrorResponder.sendErrorObject(ex.getMessage(), Response.Status.BAD_REQUEST);
}
//Send back plain text.
return Response.ok().entity( text ).build();
}
Thanks.
ErrorResponder.sendErrorObject
returns like so currently; will try the .type() function:
return Response.status( status ).entity( errorObject ).build();