-
Notifications
You must be signed in to change notification settings - Fork 38.5k
Read RestTemplate Accept header from entity annotation [SPR-12096] #16712
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Comments
Rossen Stoyanchev commented What about setting the supportedMediaTypes property on the converter? |
Christopher Smith commented That approach is cumbersome and, without completely hand-configuring the template, unreliable. In my case, the standard Jackson 2 mapper is perfectly sufficient for the actual conversion, and at a minimum I'd have to create a template without default converters (because the standard converter would add |
Rossen Stoyanchev commented I guess what I was getting at is do you need to use different versions on different entities in the same app? Configuring converters on the RestTemplate overrides defaults. So you'll need to list the converters you want, which is really straight forward and only done once. The ability to apply different versions on different entities IMO would be a stronger requirement for having some such annotation. Hence my question. |
Christopher Smith commented I'm not sure I understand your question. Each entity class has its own (versioned) media type, but besides that, the out-of-the-box converter's behavior handles everything perfectly; listing converters requires writing, instantiating, and updating converter classes that do nothing except narrow the I would expect to use the same |
Rossen Stoyanchev commented Okay I see your point. We can consider ways to improve working with versioned media types. One concern with the annotation is the ability to use it with more than one version but I can see that a single version is the 80% (common) case. When changing versions you would have to make sure the versions in all entity annotations are updated accordingly, which seems like a chore although I don't have any better ideas. Note that there is also the option to use the RestTemplate's exchange methods, essentially passing a RequestEntity with the Accept header set, but considering that this is required for every operation with a response entity, it amounts to bypassing the more convenient RestTemplate methods by HTTP method wholesale. |
Christopher Smith commented Generally speaking, the Java entity class will correspond to a specific API version, since the server side will need to be able to distinguish between representation classes for different versions. I'm actually using a |
Rossen Stoyanchev commented After some internal discussion the prevailing opinion is that such an annotation is not a good fit for use with the RestTemplate and would be too narrowly defined. It would be a better candidate for a higher-level REST programming model for example such as the one proposed here #16747. Note also that it would be trivial for you to add such support by overriding the acceptHeaderRequestCallback method. |
Christopher Smith opened SPR-12096 and commented
Current best practice is to version REST APIs by media type (
application/vnd.example.person-v1+json
), but the RestTemplate does not provide a simple mechanism for indicating theAccept
header for an entity type when using a general-purpose converter (such as Jackson). Instead, theAcceptHeaderRequestCallback
will add all potentially matching media types to theAccept
header.It should be possible to annotate an entity class to specify the media type that RestTemplate will request from the server so that older clients will not send an unversioned request that they can't understand:
Affects: 4.0.6
Issue Links:
0 votes, 5 watchers
The text was updated successfully, but these errors were encountered: