Bonjour,

Je souhaiterais savoir si il est possible de récupérer l'URI path template dans la méthode qui a été associé à l'URI path effective de façon native avec l'API JAX-RS (càd sans faire de bidouille).
Je vous donne un exemple pour être plus clair (où il y a de la bidouille) :
avec comme URI path effective : /api/v1/objects/1234
et comme URI path template : /api/v1/objects/{objectId}

Code : Sélectionner tout - Visualiser dans une fenêtre à part
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
 
@Path("/api/v1")
public class MyApiResource {
 
    @Context
    private UriInfo uriInfo;
 
    @Autowired
    private MyMetrics metrics;
 
    @Autowired
    private MyObjectService service;
 
    @GET
    @Path("/objects/{objectId}")
    @Produces({ MediaType.APPLICATION_JSON })
    Object getObject(@PathParam("objectId") String objectId) {
        metrics.log(getPathTemplate());
        return service.get(objectId);
    }
 
    /**
     * Cette methode fait de la bidouille pour recuperer l'URI path template
     */
    private String getPathTemplate() {
        String path = uriInfo.getPath(); // path = /api/v1/objects/1234
        for (String paramKey : uriInfo.getPathParameters().keySet()) { // paramKey = objectId
            for (String paramValue : uriInfo.getPathParameters().get(paramKey)) { // paramValue = 1234
                path = path.replace(paramValue, "{" + paramKey + "}");
            }
        }
        return path; // path = /api/v1/objects/{objectId}
    }
}
Merci