-1

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();
GuitarStrum
  • 713
  • 8
  • 24
  • What is `ErrorResponder`? – Andreas Jul 19 '17 at 18:58
  • It's just a helper class that makes a json object for errors. ex: {"errors":["A","B","C"]}. What I can do is send that back as a string and let the client side deal with it, but it looks weird. – GuitarStrum Jul 19 '17 at 18:59
  • 1
    Does the `sendErrorObject()` method set media type to JSON, e.g. like this? `Response.status(Response.Status.BAD_REQUEST).type(MediaType.APPLICATION_JSON).entity(jsonText).build()` --- But in general, without seeing what `ErrorResponder` does, how do you expect us to tell you why it doesn't work? – Andreas Jul 19 '17 at 19:03
  • No, it does not do that. I will try that. I didn't put it there because I thought it was just abstracted enough ("it sends JSON") and the container handled it, but obviously I was wrong. – GuitarStrum Jul 19 '17 at 19:11
  • How would it know that you're sending JSON unless you tell it? – Andreas Jul 19 '17 at 19:12
  • I don't know, it's always just worked like that in our office. "Magic". – GuitarStrum Jul 19 '17 at 19:13
  • 1
    If `@Produces` lists multiple types, it'll choose a type using content negotiation rules, and if you give an object to the `entity()` call, it will try to format that object according to the negotiated type. Since you want `text/plain` for the normal response, you should only list that in the `@Produces`, then override it if you decide to send other media type. – Andreas Jul 19 '17 at 19:15
  • The .type() function made it work. Much appreciated. – GuitarStrum Jul 19 '17 at 19:33

1 Answers1

-1

More than one MIME type can be configured as below

@Produces({"text/plain", "application/json"})
Akash
  • 587
  • 5
  • 12
  • The commented line (line 2) says that that doesn't work. – Andreas Jul 19 '17 at 18:55
  • @Haramoz Please revisit and check the content type , FYI https://www.w3.org/Protocols/rfc1341/7_1_Text.html Please check shared link. – Akash Jun 21 '19 at 12:23