0

I am trying to version my APIs and the approach I am following is by allowing the Accept header.

@GetMapping("/api/version", produces = ["application/vnd.app.v1+json"])
fun versionOne(): Map<String, String>{
  return mapOf("version" to "1")
}

@GetMapping("/api/version", produces = ["application/vnd.app.v2+json"])
fun versionTwo(): Map<String, String>{
  return mapOf("version" to "2")
}

Now above works well. If the API consumer doesn't pass any accept header, then automatically version 2 is picked up based on the sequence of the code.

Now my API is already consumed and a few of them are passing Accept: application/json and here my API fails. It returns an error, is there a way to have an or match? If the Accept header is empty or application/json, return the latest version.

razortech
  • 63
  • 3
  • you broke backward compatibility – VedantK Jul 22 '21 at 07:46
  • I have to agree with @VeKe. Even if you _can_ make this work, you _shouldn't_. Every client is bound to some _version_; regardless of whether it is formally stated. By automatically advancing the mapping, you run the high risk of breaking a client, which you - the server - cannot guarantee. Let's assume you did have it working and you add `application/vnd.app.v3+json`, which also maps to `application/json` or unspecified. If anything in the payload changed, even additive, a `v2` (or `v1`) client matching the same rules _can_ break, but there is no way to know for sure. – Chris Martinez Jul 27 '21 at 21:01

0 Answers0