[英]Generic Crud Restfull Service
I am senior Java developer (also new in stackoverflow) working on a client-server project: 我是负责客户端服务器项目的高级Java开发人员(也是stackoverflow中的新手):
For every entity clients just have to pass Class type of the entity and it will has desired functions. 对于每个实体,客户只需传递实体的类类型,它将具有所需的功能。
So I tried to have a Generic Session bean which has Restful method findAll like this: 所以我试图有一个通用会话bean,它具有Restful方法findAll,如下所示:
@Stateless
@Path("/DAOFacad/")
public class DAOFacade<T> {
@PersistenceContext EntityManager entityManager;
@PUT
@Consumes("application/json")
@Produces("application/json")
public List findAll(Class type) {
CriteriaQuery cq = entityManager.getCriteriaBuilder().createQuery();
cq.select(cq.from(type));
return entityManager.createQuery(cq).getResultList();
}
} }
In the Client web project, i have something like this: 在客户端Web项目中,我有类似以下内容:
public class Consumer {
private WebResource webResource;
private Client client;
private static final String BASE_URI = "http://localhost:8181/ServerPRS/webresources";
public Consumer() {
com.sun.jersey.api.client.config.ClientConfig config = new com.sun.jersey.api.client.config.DefaultClientConfig();
client = Client.create(config);
webResource = client.resource(BASE_URI).path("DAOFacad");
}
public <T> T findAll(Class<T> responseType, Object requestEntity) throws UniformInterfaceException {
return webResource.type(javax.ws.rs.core.MediaType.APPLICATION_JSON).put(responseType, requestEntity);
}
public void close() {
client.destroy();
}
}
A managed been: 受过管理:
public class AdminContollerMB {
Consumer consumer;
/**
* Creates a new instance of AdminContollerMB
*/
public AdminContollerMB() {
consumer = new Consumer();
}
public List<Bank> getAllBanks(){
GenericType<List<Bank>> respT = new GenericType<List<Bank>>(){};
return (List<Bank>) consumer.findAll(respT.getClass(),Bank.class);
}
but after running i get error : 但是运行后出现错误:
com.sun.jersey.api.client.ClientHandlerException: A message body writer for Java type, class java.lang.Class, and MIME media type, application/json, was not found
I have searched alot. 我搜索了很多。 i tried GenericEntity and GenericType in jersey api and also gson api of google but they didnt work out.
我在Jersey api和Google的gson api中尝试了GenericEntity和GenericType,但是他们没有解决。
any help will be appreciated. 任何帮助将不胜感激。
Your method is called findAll
but accepts PUT
. 您的方法称为
findAll
但接受PUT
。 That is not how RESTful services are build. 这不是建立RESTful服务的方式。
I guess you want to GET
all instances of some type of Resource. 我猜您想
GET
某种资源类型的所有实例。 In a RESTful service, Resources have no relation to Java Classes. 在RESTful服务中,资源与Java类无关。 So if you want a generic
findAll
GET
method, do something like this: 因此,如果要使用通用的
findAll
GET
方法,请执行以下操作:
@GET
public Response findAll(@QueryParam("class") String clazz) {
List<Object> instances = findAllInstancesOfClazz(clazz);
return Response.ok(instances).build();
}
Call this method: 调用此方法:
GET http://example.com/DAOFacade/?class=SomeClassName
Remember: REST is not RPC! 切记: REST不是RPC!
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.