Skip to content

Instantly share code, notes, and snippets.

@warmuuh
Last active September 10, 2015 13:27
Show Gist options
  • Save warmuuh/67797f5910a64954c7df to your computer and use it in GitHub Desktop.
Save warmuuh/67797f5910a64954c7df to your computer and use it in GitHub Desktop.
blogspot-snippets-2
@RestController
@RequestMapping("/v1")
ProfileV1Controller {
@RequestMapping("/profile")
ProfileV1 getProfile(){
return new ProfileV1();
}
}
@Controller
@RequestMapping("/v2")
ProfileV2Controller {
@RequestMapping("/profile")
ProfileV2 getProfile(){
return new ProfileV2();
}
}
@RestController
ProfileController {
@RequestMapping("/profile")
@VersionRange(Version.V1)
ProfileV1 getProfileV1(){
return new ProfileV1();
}
@RequestMapping("/profile")
@VersionRange(Version.V2)
ProfileV2 getProfileV2(){
return new ProfileV2();
}
}
@RestController
ProfileController {
@RequestMapping("/profile")
@VersionRange(@Version.V1) @JsonView(Views.V1)
Profile getProfileV1(){
return getProfileV2();
}
@RequestMapping("/profile")
@VersionRange(@Version.V2) @JsonView(Views.V2)
Profile getProfileV2(){
return new Profile();
}
}
@ControllerAdvice
public class VersionRangeResponseBodyAdvice extends AbstractMappingJacksonResponseBodyAdvice {
@Override
public boolean supports(MethodParameter returnType, Class<? extends HttpMessageConverter<?>> converterType) {
return (AbstractJackson2HttpMessageConverter.class.isAssignableFrom(converterType) && returnType.getMethodAnnotation(VersionRange.class) != null);
}
@Override
protected void beforeBodyWriteInternal(MappingJacksonValue bodyContainer, MediaType contentType,
MethodParameter returnType, ServerHttpRequest request, ServerHttpResponse response) {
Version selectedVersion = //retrieve requested version from request object;
bodyContainer.setSerializationView(selectedVersion.getDefaultView());
}
}
@RestController
ProfileController {
@RequestMapping("/profile")
@VersionRange({@Version.V1, @Version.V2})
Profile getProfile(){
return new Profile();
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment