是否有类似 spring-hateoas 的 RESTEasy 的 AnnotationMappingDiscoverer 之类的东西?

Is there anything like spring-hateoas' AnnotationMappingDiscoverer for RESTEasy?

在使用 Spring 的 HATEOAS 支持时,我非常喜欢 AnnotationMappingDiscoverer,它有助于避免在测试中对 REST 资源路径进行硬编码。有了它,我可以做

discoverer = new AnnotationMappingDiscoverer(RequestMapping.class);
Method method = MyController.class.getMethod("myResourceMethod", params);
String path = discoverer.getMapping(method);

然后使用 path 作为测试中的资源路径。比必须与控制器 class 和方法注释保持同步的测试中的硬编码路径要好得多。

RESTEasy 有类似的东西吗?

您可以使用 UriBuilder:

假设以下 class:

@Path("persons")
public class PersonResource {

    @Path("/{id}")
    public Response get(@PathParam("id") String id) {
      //
    }

}

你会得到这样的路径:

URI path = UriBuilder.fromResource(PersonResource.class)
                     .path(PersonResource.class, "get")
                     .build("4711");
// path = /persons/4711