Created
May 16, 2012 05:05
-
-
Save ryankennedy/2707604 to your computer and use it in GitHub Desktop.
Vendor JSON Provider for Jersey
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
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.