Does Jersey provide any way to list all of the resources it exposes? That is, given the resource class:
泽西岛是否提供任何方式来列出它所暴露的所有资源?也就是说,给定资源类:
package com.zoo.resource
@Path("/animals")
public class AnimalResource {
@GET
@Produces(MediaType.APPLICATION_JSON)
@Path("dog")
public Dog getDog(){
...
}
@GET
@Produces(MediaType.APPLICATION_JSON)
@Path("cat")
public Cat getCat(){
...
}
}
Does Jersey provide any way for me to get the information:
泽西岛是否为我提供了获取信息的方式:
-
GET
at the path/animals/dog
returns typeDog
-
GET
at the path/animals/cat
returns typeCat
GET在路径/动物/狗返回类型狗
获取路径/ animals / cat返回类型Cat
(And furthermore, does it provide a way for me to know that AnimalResource is a resource?)
(此外,它是否为我提供了一种了解AnimalResource是资源的方法?)
I would like to have this information available to me in a unit test so that I can check that every resource I expose conforms to what an external system expects. I know that there is automagic that exposes the application.wadl, but I don't see that showing me return types and I don't know how to access it from within my tests.
我希望在单元测试中可以获得这些信息,以便我可以检查我公开的每个资源是否符合外部系统的预期。我知道有一些自动化暴露了application.wadl,但是我没有看到向我显示返回类型,我不知道如何从我的测试中访问它。
1 个解决方案
#1
7
[update - example is the same but I have reworded my caveats]
[更新 - 示例是相同的但我已经重写了我的警告]
It can be done. Try the following:
可以办到。请尝试以下方法:
import com.sun.jersey.api.model.AbstractResource;
import com.sun.jersey.api.model.AbstractSubResourceMethod;
import com.sun.jersey.server.impl.modelapi.annotation.IntrospectionModeller;
import javax.ws.rs.GET;
import javax.ws.rs.Path;
import javax.ws.rs.Produces;
import javax.ws.rs.core.MediaType;
public class AnimalsTest
{
public static void main(String [] args)
{
AbstractResource resource = IntrospectionModeller.createResource(AnimalResource.class);
System.out.println("Path is " + resource.getPath().getValue());
String uriPrefix = resource.getPath().getValue();
for (AbstractSubResourceMethod srm :resource.getSubResourceMethods())
{
String uri = uriPrefix + "/" + srm.getPath().getValue();
System.out.println(srm.getHttpMethod() + " at the path " + uri + " return " + srm.getReturnType().getName());
}
}
}
class Dog {}
class Cat {}
@Path("/animals")
class AnimalResource {
@GET
@Produces(MediaType.APPLICATION_JSON)
@Path("dog")
public Dog getDog(){
return null;
}
@GET
@Produces(MediaType.APPLICATION_JSON)
@Path("cat")
public Cat getCat(){
return null;
}
}
These introspection classes are in jersey-server.
这些内省类在泽西服务器中。
Note that the example above uses some Jersey classes that have "impl" in the package name which suggests that these Jersey classes are not intended for public consumption and may very well have breaking changes in the future. I am just speculating here - I am not a Jersey committer. Just a random user.
请注意,上面的示例使用了一些在包名称中具有“impl”的Jersey类,这些类表明这些Jersey类不用于公共消费,并且可能在将来很有可能发生重大变化。我只是在这里推测 - 我不是泽西岛的提交者。只是一个随机的用户。
Also everything above I figured out by perusing the source code. I have never seen any documentation of an approved way to introspect JAX-RS annotated classes. I agree that an officially supported API to do this kind of thing would be very helpful.
此外,我通过仔细阅读源代码找到了所有内容。我从未见过任何关于内省JAX-RS注释类的批准方法的文档。我同意官方支持的API来做这种事情会非常有帮助。
#1
7
[update - example is the same but I have reworded my caveats]
[更新 - 示例是相同的但我已经重写了我的警告]
It can be done. Try the following:
可以办到。请尝试以下方法:
import com.sun.jersey.api.model.AbstractResource;
import com.sun.jersey.api.model.AbstractSubResourceMethod;
import com.sun.jersey.server.impl.modelapi.annotation.IntrospectionModeller;
import javax.ws.rs.GET;
import javax.ws.rs.Path;
import javax.ws.rs.Produces;
import javax.ws.rs.core.MediaType;
public class AnimalsTest
{
public static void main(String [] args)
{
AbstractResource resource = IntrospectionModeller.createResource(AnimalResource.class);
System.out.println("Path is " + resource.getPath().getValue());
String uriPrefix = resource.getPath().getValue();
for (AbstractSubResourceMethod srm :resource.getSubResourceMethods())
{
String uri = uriPrefix + "/" + srm.getPath().getValue();
System.out.println(srm.getHttpMethod() + " at the path " + uri + " return " + srm.getReturnType().getName());
}
}
}
class Dog {}
class Cat {}
@Path("/animals")
class AnimalResource {
@GET
@Produces(MediaType.APPLICATION_JSON)
@Path("dog")
public Dog getDog(){
return null;
}
@GET
@Produces(MediaType.APPLICATION_JSON)
@Path("cat")
public Cat getCat(){
return null;
}
}
These introspection classes are in jersey-server.
这些内省类在泽西服务器中。
Note that the example above uses some Jersey classes that have "impl" in the package name which suggests that these Jersey classes are not intended for public consumption and may very well have breaking changes in the future. I am just speculating here - I am not a Jersey committer. Just a random user.
请注意,上面的示例使用了一些在包名称中具有“impl”的Jersey类,这些类表明这些Jersey类不用于公共消费,并且可能在将来很有可能发生重大变化。我只是在这里推测 - 我不是泽西岛的提交者。只是一个随机的用户。
Also everything above I figured out by perusing the source code. I have never seen any documentation of an approved way to introspect JAX-RS annotated classes. I agree that an officially supported API to do this kind of thing would be very helpful.
此外,我通过仔细阅读源代码找到了所有内容。我从未见过任何关于内省JAX-RS注释类的批准方法的文档。我同意官方支持的API来做这种事情会非常有帮助。