[英]Anonymous JAX-RS Resources
我正在搜索在运行时(对于实体类)创建JAX-RS资源的任何可能性。 通常,这些类使用@Path("/<resource>")
进行注释,以将其标识为资源类。 是否可以在运行时创建这些类,例如作为具有自定义路径的匿名类?
这里是一个想法,它是什么样的,而抽象服务是一个基本实现:
AnyType service = new AbstractService() {
@Path("/<CustomResourceName>")
public Collection<Resource> getAll() {
return dao.getAll(Resource.class);
}
}
在那之后,当然必须以某种方式注册该服务。
谢谢!!
该解决方案非常简单,如下所示:
@Path("/dynamic-classes")
public class DynamicRestResource {
private static final Map<String, Class<?>> CLASS_NAME_MAP = Collections.unmodifiableMap(new HashMap<String, Class<?>>(){
private static final long serialVersionUID = 1L;
{
put(MyClass1.class.getSimpleName().toLowerCase(), MyClass1.class);
put(MyClass2.class.getSimpleName().toLowerCase(), MyClass2.class);
//... put here your other classes.
}}
);
private Class<?> getMappedClass(String mappedPath) {
return CLASS_NAME_MAP.get(mappedPath);
}
@GET
@Path("/{className}")
public Collection<?> getAll(@PathParam("className") String className) {
//do some checks that there is such a path
return dao.getAll(this.getMappedClass(className));
}
@DELETE
@Path("/{className}/{entityId}")
public Collection<?> deleteEntity(@PathParam("className") String className, @PathParam("entityId") Long entityId) {
//do some checks that there is such a path
return dao.delete(this.getMappedClass(className), entityId);
}
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.