Define string return type for 500 responses


#1

I am defining a service in RAML 0.8 that generally consumes/produces JSON. We are using json schemas to define our data types. But, when a 500 error is returned, we want to just set a string as the body of the response . So, I defined the mediaType to be application/json and set the body of the 500 responses to plain/text to overwrite application/json for those responses:

#%RAML 0.8
---
title: book
version: 1.0
mediaType: application/json 
schemas:
  - person: !include Person.json
  - book: !include Book.json
  - author: !include Author.json
  - books: !include Books.json
/books:
  get:
    description: Get all books  
    responses:
      200:
        body:
          schema: books    
  post:
    description: Create a new book  
    body:
      schema: book
    responses:
      200:
        body:
          schema: book
      500:
        body:
          plain/text
  /{isbn}:
    get:
      description: Retrieve a book for ISBN
      responses:
        200:
          body:
            schema: book
        404:
        500:
          body:
            plain/text
    put:
      description: Update an existing book  
      body:
        schema: book
      responses:
        200:
          body:
            schema: book
        500:
          body:
            plain/text

This sets the 500 response entity to a StreamingOutput object and the content-type to “application/json”:

    public static BooksResource.GetBooksByIsbnResponse withJsonInternalServerError(StreamingOutput entity) {
        Response.ResponseBuilder responseBuilder = Response.status(500).header("Content-Type", "application/json");
        responseBuilder.entity(entity);
        return new BooksResource.GetBooksByIsbnResponse(responseBuilder.build());
    }

I tried updating removing the mediaType and setting the content-type for each specific response, but this causes an invalid RAML error from the plugin:

#%RAML 0.8
---
title: book
version: 1.0
schemas:
  - person: !include Person.json
  - book: !include Book.json
  - author: !include Author.json
  - books: !include Books.json
/books:
  get:
    description: Get all books  
    responses:
      200:
        body:
          application/json:
            schema: books    
  post:
    description: Create a new book  
    body:
      schema: book
    responses:
      200:
        body:
          application/json:       
            schema: book
      500:
        body:
          plain/text
  /{isbn}:
    get:
      description: Retrieve a book for ISBN
      responses:
        200:
          body:
            application/json:
              schema: book
        404:
        500:
          body:
            plain/text
    put:
      description: Update an existing book  
      body:
        application/json:
          schema: book
      responses:
        200:
          body:
            application/json:
              schema: book
        500:
          body:
            plain/text

How do I write the RAML so that it is “correct” and the plugin generates classes as expected?


#2

Ughh!! It helps if you use the right content type, text/plain instead of plain/text…


#3

So you answered your own question? :smiley:


#4

Would you mind adding your working solution? I can’t seem to get raml to represent an endpoint that returns byte[] as application/octet-stream, and I wonder if your solution would do the trick :slight_smile:


#5

@heybuddy: you can set the media type however you’d like in RAML. Is the issue you’re having with a specific tool?


#6

Thanks for getting back to me @jstoiko – my issue was with the code generation tool I was using (it’s now fixed). Thanks again!