您可以使用Variant
JAX-RS 的机制。
@GET
@Path("/locations/{id}")
@Produces(value = {"application/vnd.mycompany-v2+json", // current version
"application/vnd.mycompany-v1+json", // old version
MediaType.APPLICATION_JSON}) // fallback
public Response getLocation(@PathParam("id") Integer id,
@Context Request request) {
MediaType vndTypeV1 = new MediaType("application", "vnd.mycompany-v1+json");
MediaType vndTypeV2 = new MediaType("application", "vnd.mycompany-v2+json");
Variant variant1 = new Variant(vndTypeV1, null, null);
Variant variant2 = new Variant(vndTypeV2, null, null);
Variant variantJson = new Variant(MediaType.APPLICATION_JSON_TYPE, null, null);
List<Variant> variants = new ArrayList<Variant>();
variants.add(variant1);
variants.add(variant2);
variants.add(variantJson);
Variant selectedVariant = request.selectVariant(variants);
Location location = someBackendService.getLocation(id);
// Manipulate location according to which variant is the selectedVariant.
// ...
return Response.ok("{}")
.header(HttpHeaders.CONTENT_TYPE, selectedVariant.getMediaType())
.build();
}
另请参阅Java EE 6 教程。
编辑
没有根据所选变体自动编组实体的方法。这需要一些手工工作。例如:
String version = extractVersionFromVariant(selectedVariant);
if ("v1".equals(version)) {
location.setSomeV1Propery("only in v1);
} else if ("v2".equals(version)) {
location.setSomeV2Propery("only in v2);
}
return Response.ok(location)
.header(HttpHeaders.CONTENT_TYPE, selectVariant.getMediaType())
.build();
如果版本足够不同,我会为每个版本使用 JAXB 注释类。然后,每个这样的类将只包含对这个版本有效的那些属性。JAX-RS 负责将它们编组为 JSON。