Created
April 16, 2013 06:25
-
-
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.
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); | |
} | |
} |
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 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