[英]Jersey Producing media type conflict
I'm trying out Jersey at the moment, followed this link to set up a web service in netbeans. 我现在正在尝试Jersey,按照此链接在netbeans中设置一个Web服务。 I have my entities classes and my REST classes. 我有我的实体类和我的REST类。 It works to add, edit, delete, request objects (in this case Users objects) from a javafx2 client. 它适用于从javafx2客户端添加,编辑,删除,请求对象(在本例中为Users对象)。
However, now I try to add a new method to my webservice for some simple authentication. 但是,现在我尝试向我的webservice添加一个新方法,以进行一些简单的身份验证。 First I added a new named query (Users.login) in the Users.java file: 首先,我在Users.java文件中添加了一个新的命名查询(Users.login):
@NamedQueries({
@NamedQuery(name = "Users.findAll", query = "SELECT u FROM Users u"),
@NamedQuery(name = "Users.login", query = "SELECT u FROM Users u WHERE u.username = :username AND u.password = :password"),
@NamedQuery(name = "Users.findById", query = "SELECT u FROM Users u WHERE u.id = :id"),
@NamedQuery(name = "Users.findByUserlevel", query = "SELECT u FROM Users u WHERE u.userlevel = :userlevel"),
@NamedQuery(name = "Users.findByDisabled", query = "SELECT u FROM Users u WHERE u.disabled = :disabled")
})
Afterwards I added following code to my UsersFacadeREST.java file (generated by Netbeans 7.2): 然后我将以下代码添加到我的UsersFacadeREST.java文件(由Netbeans 7.2生成):
@GET
@Path("{username}/{password}")
@Produces({"application/xml", "application/json"})
public Users login(@PathParam("username") String username, @PathParam("password") String password) {
return em.createNamedQuery("login", Users.class)
.setParameter("username", username)
.setParameter("password", password)
.getSingleResult();
}
However, I receive following error when trying to deploy the web service: 但是,在尝试部署Web服务时收到以下错误:
SEVERE: Producing media type conflict. The resource methods public entities.Users service.UsersFacadeREST.login(java.lang.String,java.lang.String) and public java.util.List service.UsersFacadeREST.findRange(java.lang.Integer,java.lang.Integer) can produce the same media type
As I'm new to this, I have no clue why the login() method is giving a conflict with findRange()? 由于我是新手,我不知道为什么login()方法与findRange()发生冲突? The first has 2 String parameters and give a Users object, the second has 2 integer parameters and returns a List object? 第一个有2个String参数并给出一个Users对象,第二个有2个整数参数并返回一个List对象? Is there any way to fix this problem as I'm gonna need some custom queries added to my webservice... 有没有办法解决这个问题,因为我需要一些自定义查询添加到我的webservice ...
For completion: 完成:
@GET
@Path("{from}/{to}")
@Produces({"application/xml", "application/json"})
public List<Users> findRange(@PathParam("from") Integer from, @PathParam("to") Integer to) {
return super.findRange(new int[]{from, to});
}
Code in super class (AbstractFacade.java) 超类中的代码(AbstractFacade.java)
public List<T> findRange(int[] range) {
javax.persistence.criteria.CriteriaQuery cq = getEntityManager().getCriteriaBuilder().createQuery();
cq.select(cq.from(entityClass));
javax.persistence.Query q = getEntityManager().createQuery(cq);
q.setMaxResults(range[1] - range[0]);
q.setFirstResult(range[0]);
return q.getResultList();
}
The problem is, both methods are using path templates that match the same URIs. 问题是,两种方法都使用匹配相同URI的路径模板。 "{a}/{b}"
is equivalent to "{c}/{d}"
- in the same way "{username}/{password}"
is equivalent to "{from}/{to}"
. "{a}/{b}"
相当于"{c}/{d}"
- 就像"{username}/{password}"
相当于"{from}/{to}"
。 And because both methods also use the same media type, there is an ambiguity. 并且因为这两种方法也使用相同的媒体类型,所以存在歧义。 You can fix this by using a regular expression in the path template to make it more specific. 您可以通过在路径模板中使用正则表达式来解决此问题,以使其更具体。 Ie since "{from}/{to}"
should always be numbers, you can disambiguate it by changing it like follows: "{from: [0-9]+}/{to: [0-9]+}"
. 即,因为"{from}/{to}"
应始终为数字,您可以通过更改它来消除歧义: "{from: [0-9]+}/{to: [0-9]+}"
。
Anyway, are you sure no user will pick plain numbers from username and password? 无论如何,你确定没有用户会从用户名和密码中选择普通数字吗? Looks like in your case it would be much better to use different URI "sub-space" for each of the two resources. 看起来在你的情况下,为两个资源中的每一个使用不同的URI“子空间”会好得多。 Eg: login/{username}/{password}
and ranges/{from}/{to}
. 例如: login/{username}/{password}
和ranges/{from}/{to}
。
BUT, few points on the design: 但是,设计上几点:
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.