在我的应用程序中查找所有泽西岛资源方法的列表? [英] Find a list of all Jersey resource methods in my app?

查看:86
本文介绍了在我的应用程序中查找所有泽西岛资源方法的列表?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

Jersey是否提供了列出它所暴露的所有资源的方法?也就是说,给定资源类:

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 在路径 / animals / dog 返回类型 Dog

  • GET 在路径 / animals / cat 返回类型 Cat

  • GET at the path /animals/dog returns type Dog
  • GET at the path /animals/cat returns type Cat

(此外,它是否为我提供了一种了解AnimalResource是资源的方法?)

(And furthermore, does it provide a way for me to know that AnimalResource is a resource?)

我想在单元测试中向我提供这些信息,以便我可以检查每个资源我暴露符合外部系统期望的内容。
我知道有一个自动化版本暴露了 application.wadl ,但是我没有看到显示我的返回类型,我不知道如何从我的测试中访问它。

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.

推荐答案

[更新 - 示例是相同的,但我已重新编写我的警告]

[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;
    }
}

这些内省类位于jersey-server。

These introspection classes are in jersey-server.

请注意,上面的示例使用了一些在包名称中包含impl的Jersey类,这些类表明这些Jersey类不适合公共使用,并且很可能会发生重大变化在将来。我只是在这里推测 - 我不是泽西岛的提交者。只是一个随机的用户。

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.

此外,我通过仔细阅读源代码来解决所有问题。我从未见过任何关于内省JAX-RS注释类的批准方法的文档。我同意官方支持的API来做这种事情会非常有帮助。

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.

这篇关于在我的应用程序中查找所有泽西岛资源方法的列表?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆