spring-projects / spring-framework

Spring Framework
https://spring.io/projects/spring-framework
Apache License 2.0
55.66k stars 37.77k forks source link

Support JSON Pointer in @HttpExchange interface #28576

Open quaff opened 2 years ago

quaff commented 2 years ago

Sometimes we need extract data from child node instead of root document. For example get index mappings from ElasticSearch:

@HttpExchange("http://localhost:9200")
public interface ElasticSearchIndexOperations {

    @GetExchange("/{index}/_mapping")
    @JsonPointer("/${index}/mappings")
    Mappings getMapping(@PathVariable String index);

 }

NOTE: placeholders in @JsonPointer("/${index}/mappings") need to be resolved base on method parameters.

rstoyanchev commented 1 year ago

This is something we could do, but will need to isolate use of APIs specific to JSON libraries.

Jackson supports setting JsonPointer on ObjectReader, so we could start with that in AbstractJackson2Decoder if it's passed as a hint. The next question is how to pass the hint in. We could use HttpRequestValues to pass it to WebClientAdapter, but no good way from there to pass a hint. The closest is calling ClientResponse#body(BodyExtractor) with a decorated BodyExtractor, but we could also add a more first-class option. Perhaps an overloaded method on ClientResponse similar to ServerRequest.

quaff commented 7 months ago

It seems JSONPath is more popular, It would be nice both are supported.

sdeleuze commented 7 months ago

A few open questions and remarks after discussing with @simonbasle:

poutsma commented 7 months ago
  • On functional API side, I think RestClient does not provide a way to pass hints unlike WebClient, could you please confirm @poutsma?

Indeed, because RestClient is based on the existing HttpMessageConverters, which do not support hints.

sdeleuze commented 7 months ago

Ah good point, we use MappingJacksonValue to pass JSON views, so JSON filters support could probably be added the same way.

rstoyanchev commented 7 months ago

Jackson does not provide a @JsonPointer annotation and I am not sure Spring should introduce that as too Jackson/JSON specific (@JsonView is a Jackson annotation that we reuse, but I am not sure a @JsonPointer annotation makes sense on Jackson side).

Jackson has a JsonPointer type and we could take that into account if it is passed in as an argument. It should be clear it applies to the response since you shouldn't need that for the request body.

sdeleuze commented 6 months ago

Feedback after a related discussion during our team meeting: