Skip to content

Instantly share code, notes, and snippets.

@ryankennedy
Created April 16, 2013 06:25
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/5393789 to your computer and use it in GitHub Desktop.
Save ryankennedy/5393789 to your computer and use it in GitHub Desktop.
Brief exploration into what it takes to do vendor extensions in JAX-RS.
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);
}
}
package com.yammer.testvendor;
import javax.ws.rs.GET;
import javax.ws.rs.Path;
import javax.ws.rs.Produces;
@Path("/vendor")
@Produces("application/vnd.myvendor+json")
public class VendorResource {
@GET
public Vendor get() {
return new Vendor(1, "yammer");
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment