users@jersey.java.net

[Jersey] API versioning through a custom header

From: Raúl Guerrero Deschamps <raul_at_jimi.com.mx>
Date: Tue, 5 Apr 2016 20:29:35 -0500

Hi all,

After researching API versioning with Jersey a bit, the only way I see this
happening is through modifying the Accept header and use a special MIME
type, and then if the MIME type ends with +xml or +json it's smart enough
to understand what serializer to use.

Now, what if, because of client restrictions, I can't handle custom MIME
types and such, and instead I'd like to use another recommended versioning
pattern which would be adding a header to the HTTP request called version,
and place the version number there.

That way I could do something like

@GET("/myObj")
@Consumes(MediaType.APPLICATION_JSON)
@Header("version", "1.0")
public JsonResponse myV1method() {}

@GET("myObj")
@Consumes(MediaType.APPLICATION_JSON)
@Header("version", "2.0")
public JsonResponse myV2method() {}


Currently, how could I choose which jersey annotated java method to use
depending on what the version header value is?

Thanks,

Raul