Skip to content

Provide a mechanism to serve hypermedia responses to requests that accept application/json #1074

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

Open
wilkinsona opened this issue Sep 5, 2019 · 7 comments
Assignees
Labels
in: mediatypes Media type related functionality

Comments

@wilkinsona
Copy link
Member

wilkinsona commented Sep 5, 2019

In Spring Boot 1.2 (I think) we added support for serving HAL in response to requests that accept application/json. This was originally mentioned in this comment but I think it ought to be tracked as a separate issue.

The way that we achieve the behaviour described above has changed a bit over time. The current implementation relies on the mutability of TypeConstrainedMappingJackson2HttpMessageConverter to add application/json to its supported media types. We need to be able to do something similar for HATEOAS on top of WebFlux and I don't think it's possible without resorting to reflection at the moment.

Taking a step back, perhaps it would be better to revisit the approach that we took for MVC at the same time and for Spring HATEOAS to provide a first-class configuration setting for this?

This is a similar issue to #1073 but is about allowing requests for application/json to receive a HAL (or other format) response.

@wilkinsona wilkinsona changed the title Provide a mechanism to server hypermedia responses to requests that accept application/json Provide a mechanism to serve hypermedia responses to requests that accept application/json Sep 5, 2019
@gregturn
Copy link
Contributor

On this commit I coded support where a given media type could also register for application/json.

674ea29

It feels kind of clunky. Not sure what a proper API would be that says “when asking for plain JSON, serve HAL”. Maybe it is this?

Or perhaps this but just for HAL.

@gregturn
Copy link
Contributor

We have a dedicated HalConfiguration bean to alter rendering of HAL. Perhaps that could be the place to “register” additional media types for the same serializer.

@gregturn
Copy link
Contributor

In fact putting this setting inside HalConfiguration sounds more appealing than encoding such decision into the media type handler itself. It would also grant Boot the ability to inject this through any means desired.

@wilkinsona
Copy link
Member Author

wilkinsona commented Sep 16, 2019

Thanks for taking a look, Greg. FWIW, 674ea29 doesn't look too clunky to me. One thing I did notice is that I think the default is now that HAL would be served in response to a request that accepts application/json. I wouldn't change the default in HATEOAS, just provide an option to enable that behaviour.

It would also grant Boot the ability to inject this through any means desired.

That's what we really need from a Boot perspective. If you have time to code something up in that direction then I'd be happy to take a look at how it would integrate into Boot. It may allow us to get spring-projects/spring-boot#16020 into Boot 2.2.

gregturn added a commit that referenced this issue Sep 16, 2019
…-FORMS.

Inside HalConfiguration and HalFormsConfiguration, add a "wither" to support registering additional mediatypes. This makes it possible for Spring Boot to create a bean that will yield HAL when clients ask for application/json.

NOTE: This is NOT implemented in Collection+JSON, UBER, or any other mediatypes, and has not been captured as an interface that all custom mediatypes would pick up. So far, it's custom for HAL and HAL-FORMS. If there is a big draw, we can look at extracting some type of interface. But for now, community feedback would be nice before going down that road.
@gregturn
Copy link
Contributor

wdyt @wilkinsona?

10% of this is the code to register extra mediatypes. 90% is the copying of test cases for HAL and HAL-FORMS against both WebFlux and WebMVC.

@gregturn gregturn self-assigned this Sep 16, 2019
@gregturn gregturn added the in: mediatypes Media type related functionality label Sep 16, 2019
@gregturn gregturn modified the milestones: 1.0.0.RC3, 1.0.0.RELEASE Sep 16, 2019
@odrotbohm
Copy link
Member

odrotbohm commented Sep 23, 2019

I don't think this should be solved on the HMC level but rather in content negotiation with the user explicitly defining which media type she want's to see for a request for application/json. The HMC defines which format it can produce and shouldn't have to deal with "Well, I want to say application/json but actually mean application/prs.foobar+json". That's content negotiation and the HMC are means consulted in the decision not the abstraction driving it.

Content negotiation for requests is done in MVC. If media type aliasing is supposed to be supported, it should live there. application/json is just one example, custom, user defined media types are another. Of course we can wire all of these things down into the HMC / Encoder implementations but there's just no need for that kind of knowledge so deep down the call stack.

The approach is also at odds with us allowing to register custom media type implementations that would have to know about this, use a dedicated base class so that Boot can tweak their setups as well. All of this has much bigger impact and complexity than MVC aliasing the media type in its content negotiation process.

@gregturn gregturn modified the milestones: 1.0.0.RELEASE, 0.25.3.RELEASE, 1.1.0.M1 Sep 26, 2019
@odrotbohm odrotbohm modified the milestones: 1.1.0.M1, 1.1.0.M2 Jan 14, 2020
@odrotbohm odrotbohm modified the milestones: 1.1.0.M2, 1.1.0 M3 Feb 11, 2020
@gregturn gregturn modified the milestones: 1.1.0.M3, 1.1.0.M4 Mar 31, 2020
@gregturn gregturn modified the milestones: 1.1.0.RC1, 1.2.0-M1 Apr 21, 2020
@odrotbohm odrotbohm modified the milestones: 1.2 M1, 1.2 RC1 Aug 11, 2020
@gregturn gregturn modified the milestones: 1.2 RC1, 1.2 GA Sep 15, 2020
@odrotbohm odrotbohm modified the milestones: 1.2 GA, 1.3 M1 Oct 27, 2020
@odrotbohm odrotbohm modified the milestones: 1.3 M1, 1.3 M2 Jan 13, 2021
@odrotbohm odrotbohm modified the milestones: 1.3 M2, 1.3 RC1 Feb 16, 2021
@odrotbohm odrotbohm modified the milestones: 1.3 M3, 1.4 M1 Mar 15, 2021
@odrotbohm odrotbohm modified the milestones: 1.4 M1, 1.4 M2 Jul 14, 2021
@odrotbohm odrotbohm modified the milestones: 1.4 M2, 1.4 M3 Aug 12, 2021
@odrotbohm odrotbohm modified the milestones: 1.4 M3, 1.4 candidates Sep 16, 2021
@kdebski85
Copy link

Is there any solution/workaround for this issue? I just want to use application/json instead of application/hal+json for all responses with WebFlux + HATEOAS.

@odrotbohm odrotbohm removed this from the 1.4 candidates milestone Nov 13, 2023
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
in: mediatypes Media type related functionality
Projects
None yet
Development

No branches or pull requests

4 participants