簡體   English   中英

澤西島上的嵌套內部類/ JAX-RS

[英]Nested inner class in Jersey / JAX-RS

我有位於完美的此資源:

@Path("/adoptable")
public class AdoptableAnimalsResource {

  @GET
  @Produces(MediaType.TEXT_PLAIN)
  public String get() 
  {
    return "dogs";
  }
}

現在,如何將此類轉換為嵌套內部類? 例如,

public class Grouper
{
  @Path("/adoptable")
  public class AdoptableAnimalsResource {

    @GET
    @Produces(MediaType.TEXT_PLAIN)
    public String get() 
    {
      return "dogs";
    }
  }
}

嘗試時,出現404 Not Found錯誤,提示Jersey並未將內部類視為資源。

您需要使用子資源定位符 基本上,您將在Grouper類中有一個方法,該方法將實例化AdoptableAnimalsResource類。 AdoptableAnimalsResource不應具有@Path批注。 可以,但是將被忽略。 它的方法可以具有子資源@Path Grouper類中的方法應具有@Path ,以標識AdoptableAnimalsResource子資源。

所以看起來可能像

@Path("/groups")
public class Grouper {

    @Path("/adoptable")
    public AdoptableAnimalsResource animalSubResource() {
        return new AdoptableAnimalsResource();
    }

    public class AdoptableAnimalsResource {

        @GET
        @Produces(MediaType.TEXT_PLAIN)
        public String get() {
            return "dogs";
        }
    }
}

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM