[英]Nested inner class in Jersey / JAX-RS
I have this resource which is located perfectly: 我有位于完美的此资源:
@Path("/adoptable")
public class AdoptableAnimalsResource {
@GET
@Produces(MediaType.TEXT_PLAIN)
public String get()
{
return "dogs";
}
}
Now, how can I turn this class into a nested inner class? 现在,如何将此类转换为嵌套内部类? For example,
例如,
public class Grouper
{
@Path("/adoptable")
public class AdoptableAnimalsResource {
@GET
@Produces(MediaType.TEXT_PLAIN)
public String get()
{
return "dogs";
}
}
}
When I try it, I get a 404 Not Found error, suggesting that Jersey is not considering the inner class as a resource. 尝试时,出现404 Not Found错误,提示Jersey并未将内部类视为资源。
You need to use Sub-Resource Locators . 您需要使用子资源定位符 。 Basically, you will have a method in
Grouper
class, which will instantiate the AdoptableAnimalsResource
class. 基本上,您将在
Grouper
类中有一个方法,该方法将实例化AdoptableAnimalsResource
类。 The AdoptableAnimalsResource
should not have a @Path
annotation. AdoptableAnimalsResource
不应具有@Path
批注。 It could but it will be ignored. 可以,但是将被忽略。 It's methods can have sub-resource
@Path
s. 它的方法可以具有子资源
@Path
。 The method in the Grouper
class should have @Path
that identities the AdoptableAnimalsResource
sub-resource. Grouper
类中的方法应具有@Path
,以标识AdoptableAnimalsResource
子资源。
So it might look something like 所以看起来可能像
@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.