DZone
Web Dev Zone
Thanks for visiting DZone today,
Edit Profile
  • Manage Email Subscriptions
  • How to Post to DZone
  • Article Submission Guidelines
Sign Out View Profile
  • Post an Article
  • Manage My Drafts
Over 2 million developers have joined DZone.
Log In / Join
  • Refcardz
  • Trend Reports
  • Webinars
  • Zones
  • |
    • Agile
    • AI
    • Big Data
    • Cloud
    • Database
    • DevOps
    • Integration
    • IoT
    • Java
    • Microservices
    • Open Source
    • Performance
    • Security
    • Web Dev
DZone > Web Dev Zone > Ratpacked: Respond To Custom MIME Types

Ratpacked: Respond To Custom MIME Types

In Ratpack we can use the byContent method on the Context object to send different responses based on the requested MIME type from the client. We can match on a custom MIME type with the method type and specify the MIME type. If the type matches we can create a response.

Hubert Klein Ikkink user avatar by
Hubert Klein Ikkink
·
Jan. 04, 16 · Web Dev Zone · Tutorial
Like (1)
Save
Tweet
3.29K Views

Join the DZone community and get the full member experience.

Join For Free

In Ratpack we can use the byContent method on the Context object to send different responses based on the requested MIME type from the client. There is already support for application/json, application/xml, text/plain, and text/html MIME types with corresponding methods of the ByContentSpec object that is passed as argument to the byContent method. We can match on a custom MIME type with the method type and specify the MIME type. If the type matches we can create a response.

In the following example application we have a custom renderer for a User object:

// File: src/main/groovy/com/mrhaki/ratpack/UserRenderer.groovy
package com.mrhaki.ratpack

import ratpack.handling.ByContentSpec
import ratpack.handling.Context
import ratpack.jackson.Jackson
import ratpack.render.RendererSupport

import static ratpack.groovy.Groovy.markupBuilder

class UserRenderer extends RendererSupport<User> {

    @Override
    void render(Context context, User user) throws Exception {
        context.byContent { ByContentSpec spec ->
            spec
                .type('application/vnd.com.mrhaki.user+json;v=1') {
                    context.render(Jackson.json(user))
                }
                .type('application/vnd.com.mrhaki.user+xml;v=1') {
                    context.render(markupBuilder('application/xml', 'UTF-8') {
                        delegate.user {
                            username(user.username)
                        }
                    })
                }
        }
    }

}

Next we use it in our Ratpack definition:

import com.mrhaki.ratpack.User
import com.mrhaki.ratpack.UserRenderer

import static ratpack.groovy.Groovy.ratpack

ratpack {
    bindings {
        // Register renderer for User objects.
        bind(UserRenderer)

        // Create two sample users.
        bindInstance(['mrhaki', 'hubert'].collect { String name ->
            new User(username: name)
        })        
    }

    handlers {
        get('user/:username') { List<User> users ->
            // Get value for username token.
            final String username = pathTokens.username

            // Find user in list of users.
            final User user = users.find { User user ->
                user.username == username
            }

            // Render user object.
            render(user)
        }
    }
}

When we make a request with different accept MIME types we see different results:

$ http localhost:5050/user/mrhaki Accept:application/vnd.com.mrhaki.user+xml;v=1
HTTP/1.1 200 OK
connection: keep-alive
content-encoding: gzip
content-type: application/xml
transfer-encoding: chunked

<user>
<username>mrhaki</username>
</user>

$ http localhost:5050/user/mrhaki Accept:application/vnd.com.mrhaki.user+json;v=1
HTTP/1.1 200 OK
connection: keep-alive
content-encoding: gzip
content-type: application/vnd.com.mrhaki.user+json;v=1
transfer-encoding: chunked

{
"username": "mrhaki"
}

$

Written with Ratpack 1.1.1.

Object (computer science) application Requests

Published at DZone with permission of Hubert Klein Ikkink, DZone MVB. See the original article here.

Opinions expressed by DZone contributors are their own.

Popular on DZone

  • AI Philosophy and Its Part in Digital Design
  • Everything You Should Know About APIs
  • Distributed Training on Multiple GPUs
  • The Impacts of Blockchain on the Software Development Industry

Comments

Web Dev Partner Resources

X

ABOUT US

  • About DZone
  • Send feedback
  • Careers
  • Sitemap

ADVERTISE

  • Advertise with DZone

CONTRIBUTE ON DZONE

  • Article Submission Guidelines
  • MVB Program
  • Become a Contributor
  • Visit the Writers' Zone

LEGAL

  • Terms of Service
  • Privacy Policy

CONTACT US

  • 600 Park Offices Drive
  • Suite 300
  • Durham, NC 27709
  • support@dzone.com
  • +1 (919) 678-0300

Let's be friends:

DZone.com is powered by 

AnswerHub logo