Releasing Two Lightweight Libraries for Server-Driven Content Negotiation

In Why I Consistently Reach for Server-Driven Content Negotiation (For Versioning), I spoke about how useful server-driven content-negotiation can be for versioning or for multiple representations of data for your RESTful APIs.

But if you're building serverless projects, or projects without a heavy web layer, it may be quite difficult for you to do this, as you need to hand-roll it.

(As an aside, this is more for when you have a larger serverless project, which handles multiple routes / representations, which isn't always going to be true, depending on how far you stray from functions-as-a-service)

I've just released two libraries to Maven Central to make this easer - media-type and content-negotiation, which provide the ability to perform this negotiation in a straighforward way.

It's been a bit annoying that there's not a lightweight alternative for at least the MediaType class, as many libraries/frameworks have a nice implementation, but it's not available standalone - I've taken the best parts of lots of designs to get to my implementation.

Although they're at a 0.1.0 release for now, they fulfill a number of requirements for my own usage, and after a bit of use in production, I'll look to stabilise the API with a v1 release.

This allows you, as a consumer, to write the following code to parse the request and resolve your MediaType, if applicable.

// setup
ContentTypeNegotiator negotiator = new ContentTypeNegotiator(
    Arrays.asList(MediaType.APPLICATION_JSON, MediaType.TEXT_PLAIN));
AcceptHeaderParser parser = new AcceptHeaderParser();

// each request
try {
  MediaType resolved = negotiator.negotiate(parser.parse("application/*"));
  // do something with the resolved `MediaType`, i.e. present your response as that format
} catch (NotAcceptableException e) {
  // handle, i.e. returning a 406 Not Acceptable
}

This handles quality value negotiation, allowing a consumer to say that they don't necessarily want a given version.

Written by Jamie Tanna's profile image Jamie Tanna on , and last updated on .

Content for this article is shared under the terms of the Creative Commons Attribution Non Commercial Share Alike 4.0 International, and code is shared under the Apache License 2.0.

#java #media-type #content-negotiation.

This blog post is 100% human-written, and has nothing derived from LLMs (be it code, editorial support or prose).

This post was filed under articles.

Interactions with this post

Interactions with this post

Below you can find the interactions that this page has had using WebMention.

Have you written a response to this post? Let me know the URL:

Do you not have a website set up with WebMention capabilities? You can use Comment Parade.