当使用Spring的HATEOAS支持时,我真的很喜欢AnnotationMappingDiscoverer
,它有助于避免在测试中对REST资源路径进行硬编码。有了它,我可以做类似的事情
discoverer = new AnnotationMappingDiscoverer(RequestMapping.class);
Method method = MyController.class.getMethod("myResourceMethod", params);
String path = discoverer.getMapping(method);
然后使用该
path
作为测试中的资源路径。比必须与控制器类和方法注释保持同步的测试中的硬编码路径好得多。RESTEasy有类似的东西吗?
最佳答案
您可以使用UriBuilder:
假设以下课程:
@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