Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8746279
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 13, 20262026-06-13T12:10:12+00:00 2026-06-13T12:10:12+00:00

I read a lot about versioning REST APIs, f.e. in this thread: Best practices

  • 0

I read a lot about versioning REST APIs, f.e. in this thread: Best practices for API versioning?

Because of that I would like to use the HTTP-Accept-Header to indicate which version the client is asking for.
But how can I apply this in my application? Which changes had therefore be made? How does the marshaller know, which version should be used? Do I have to register my type?

What I know is that I have to change the content of the @Produces-Annotation

@GET
@Path("/locations")
@Produces("application/vnd.mycompany-v1+xml")
Location[] getLocations();

But what else has to be changed?

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-13T12:10:12+00:00Added an answer on June 13, 2026 at 12:10 pm

    You can use the Variant mechanisms of 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();
    }
    

    See the Java EE 6 Tutorial, too.

    Edit

    There is no automatic way to marshal an entity according to the selected variant. This requires some manual work. For example:

    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();
    

    If the versions are different enough, I’d use a JAXB annotated class for each version. Each such class would then only contain those properties that are valid for this version. JAX-RS takes care to marshal them to JSON.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

So i read a lot about why this implementation isn't thread-safe. But I didn't
Assume i've read a lot about versioning a restful api, and I decided to
I have read a lot about this issue but mine still seems to be
I have read a lot about jquery and i have a simple webservice that
I've read a lot about building UI that supports multiple screen resolutions on android.
I read a lot about GlassFish application server that it supports the whole Java
(I've read a lot about 64-bit versus 32-bit OS/Apps, but this question is specifically
I'm really confused about this, tried to read a lot about diagrams but I
I have read a lot about this! But I can't make a clear idea!
I read a lot about this problem on Stackoverflow but not of the Helps

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.