Skip to content

Instantly share code, notes, and snippets.

@ryankennedy
Created May 16, 2012 05:05
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save ryankennedy/2707604 to your computer and use it in GitHub Desktop.
Save ryankennedy/2707604 to your computer and use it in GitHub Desktop.
Vendor JSON Provider for Jersey
package com.yammer.testvendor;
import com.yammer.dropwizard.jersey.JacksonMessageBodyProvider;
import com.yammer.dropwizard.json.Json;
import javax.ws.rs.Consumes;
import javax.ws.rs.Produces;
import javax.ws.rs.core.MediaType;
import java.lang.annotation.Annotation;
import java.lang.reflect.Type;
import java.util.regex.Pattern;
@Produces("application/*")
@Consumes("application/*")
public class VendorJsonProvider extends JacksonMessageBodyProvider {
private static final String TYPE = "application";
private static final Pattern SUBTYPE_PATTERN = Pattern.compile("vnd\\..*\\+json");
public VendorJsonProvider(Json json) {
super(json);
}
@Override
public boolean isReadable(Class<?> type, Type genericType, Annotation[] annotations, MediaType mediaType) {
return mediaType.getType().equals(TYPE) &&
SUBTYPE_PATTERN.matcher(mediaType.getSubtype()).matches() &&
super.isReadable(type, genericType, annotations, MediaType.APPLICATION_JSON_TYPE);
}
@Override
public boolean isWriteable(Class<?> type, Type genericType, Annotation[] annotations, MediaType mediaType) {
return mediaType.getType().equals(TYPE) &&
SUBTYPE_PATTERN.matcher(mediaType.getSubtype()).matches() &&
super.isWriteable(type, genericType, annotations, MediaType.APPLICATION_JSON_TYPE);
}
}
@akahn
Copy link

akahn commented Nov 19, 2018

How do you register this class with Jersey? In other words, how do you declare to Jersey to use this class for content negotiation? I'm using Dropwizard.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment