How in Java (REST api) you can specify 2 resources located in the same path, so the execution depends on the version client had provided?

StackOverflow https://stackoverflow.com/questions/22153511

  •  19-10-2022
  •  | 
  •  

Frage

Example:

@POST
@Path("/commonPath")
@Consumes(MediaType.APPLICATION_XML)
@Produces(MediaType.APPLICATION_XML)
public Response getMethoV1(RequestDto1 reqDto) {
   //logic
}

@POST
@Path("/commonPath")
@Consumes(MediaType.APPLICATION_XML)
@Produces(MediaType.APPLICATION_XML)
public Response getMethoV2(RequestDto2 reqDto) {
   //logic
}

How can this code be adjusted, so the clients who use app version 1 could go to getMethod1() and those who have switched to version 2 could use resource getMethod2()? And RequestDtos may or may not be the same class objects.

Keine korrekte Lösung

Andere Tipps

Easiest way to version the API is including version number as part of the URI during the development.

/commonPath/v1.0
/commonPath/v1.1

Your code would then become like this

        @GET
        @Path("/commonPath/v1.0")
        @Consumes(MediaType.APPLICATION_XML)
        @Produces(MediaType.APPLICATION_XML)
        public Response getMethoV1(RequestDto1 reqDto) {
           //logic
        }

        @GET
        @Path("/commonPath/v1.1")
        @Consumes(MediaType.APPLICATION_XML)
        @Produces(MediaType.APPLICATION_XML)
        public Response getMethoV2(RequestDto2 reqDto) {
           //logic
        }
Lizenziert unter: CC-BY-SA mit Zuschreibung
Nicht verbunden mit StackOverflow
scroll top