API Versioning Service in header for Restful

all!

I'm check about service of the Version API in Rest Header, I working in Java language and Jersey framework. No URI only header in request all (POST, GET, PUT and DELETE). See a example below.

Example: GET /Employee Header - Version: 1.0 <- this it's header that I need implement in version service.

Somebody has tips or pass me the articles and tutorial about it.

Thank a lot.

Upvotes: 1

Views: 2322

Answers (1)

Russ Jackson
Russ Jackson

Reputation: 2112

Under the Resource Oriented Architectural style for REST you would version the resources themselves and not the service. You would implement this by use of a custom, versioned media type. For example, using Java and JAX-RS it might look something like this:

@Path("/resource/account/{id}")
@GET
@Produces("application/vnd.mycompany.account-1.0+json")
public Response getAccount(@PathParam("id") String id) {
    ...

Notice the version number in the media type string in the @Produces annotation.

Then, in your client when you issue the request you use the Accept header to specify that you want the given media type:

Accept: application/vnd.mycompany.account-1.0+json

In other words, don't do this:

@Path("/resource/account/v1/{id}")

If you want to support multiple versions your service class might look like this:

@Path("/resource/account")
public class AccountWebService {

    @GET
    @Path("/{id}")
    @Produces("application/vnd.mycompany.account-1.0+json")
    public Response getAccount(@PathParam("id") String id) {
    ...

    @GET
    @Path("/{id}")
    @Produces("application/vnd.mycompany.account-2.0+json")
    public Response getAccountV2(@PathParam("id") String id) {
    ...

We usually pull the media type out into a public static so we can access and use them from our test clients. Be sure to use JerseyTest to test your web services as part of your unit testing.

Upvotes: 3

Related Questions