Instantly share code, notes, and snippets.

Embed
What would you like to do?

Let Kikaha render it for you

Defining a content-type per method 1

Response getWishList( ... ){
  ...

  // As I don't know what is the response type, I'm using List<Wish> here...
  List<Wish> wishes = userWishList.getWishList(
      tokenExtractorHelper.getId(),
      paginatorHelper.getPerPage(),
      paginatorHelper.getPage()
  );

  // Let Kikaha serialize it for you! ;)
  return DefaultResponse.ok().entity( wishes ).contentType( Mimes.JSON );
}

Defining a content-type per method 2

@Produces( Mimes.JSON )
Response getWishList( ... ){
  ...

  // As I don't know what is the response type, I'm using List<Wish> here...
  List<Wish> wishes = userWishList.getWishList(
      tokenExtractorHelper.getId(),
      paginatorHelper.getPerPage(),
      paginatorHelper.getPage()
  );

  // Let Kikaha serialize it for you! ;)
  return DefaultResponse.ok().entity( wishes );
}

Defining a content-type per class

@Produces( Mimes.JSON )
class WishListController {

...
  Response getWishList( ... ){
    ...

    // As I don't know what is the response type, I'm using List<Wish> here...
    List<Wish> wishes = userWishList.getWishList(
        tokenExtractorHelper.getId(),
        paginatorHelper.getPerPage(),
        paginatorHelper.getPage()
    );

    // Let Kikaha serialize it for you! ;)
    return DefaultResponse.ok().entity( wishes );
  }

The simplier way

If you intent to have most (or all) of your routes sending responses using the same Content-Type, you can define it on your application.conf and you will never have to worry about it any more.

# application.yml
server:
  urouting:
    default-content-type: "application/json"

Then you don't even have to define the mime on your Java code.

  Response getWishList( ... ){
    ...

    // As I don't know what is the response type, I'm using List<Wish> here...
    List<Wish> wishes = userWishList.getWishList(
        tokenExtractorHelper.getId(),
        paginatorHelper.getPerPage(),
        paginatorHelper.getPage()
    );

    // Let Kikaha serialize it for you! ;)
    return DefaultResponse.ok().entity( wishes );
  }

Or maybe, if you don't need to send any extra details besides of the response object, you can go even simpler like this:

  // As I don't know what is the response type, I'm using List<Wish> here...
  List<Wish> getWishList( ... ){
    ...

    // Let Kikaha serialize it for you! ;)
    return userWishList.getWishList(
        tokenExtractorHelper.getId(),
        paginatorHelper.getPerPage(),
        paginatorHelper.getPage()
    );
  }
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment