java 泽西岛生产媒体类型冲突
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/12883029/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Jersey Producing media type conflict
提问by Perneel
I'm trying out Jersey at the moment, followed thislink to set up a web service in netbeans. I have my entities classes and my REST classes. It works to add, edit, delete, request objects (in this case Users objects) from a javafx2 client.
我目前正在尝试 Jersey,按照此链接在 netbeans 中设置 Web 服务。我有我的实体类和我的 REST 类。它可以从 javafx2 客户端添加、编辑、删除、请求对象(在本例中为用户对象)。
However, now I try to add a new method to my webservice for some simple authentication. 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()? The first has 2 String parameters and give a Users object, the second has 2 integer parameters and returns a List object? Is there any way to fix this problem as I'm gonna need some custom queries added to my webservice...
由于我是新手,我不知道为什么 login() 方法会与 findRange() 发生冲突?第一个有 2 个 String 参数并给出一个 Users 对象,第二个有 2 个整数参数并返回一个 List 对象?有什么办法可以解决这个问题,因为我需要在我的网络服务中添加一些自定义查询...
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();
}
回答by Martin Matula
The problem is, both methods are using path templates that match the same URIs. "{a}/{b}"
is equivalent to "{c}/{d}"
- in the same way "{username}/{password}"
is equivalent to "{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. I.e. since "{from}/{to}"
should always be numbers, you can disambiguate it by changing it like follows: "{from: [0-9]+}/{to: [0-9]+}"
.
问题是,这两种方法都使用匹配相同 URI 的路径模板。"{a}/{b}"
相当于"{c}/{d}"
- 以同样的方式"{username}/{password}"
相当于"{from}/{to}"
. 并且因为这两种方法也使用相同的媒体类型,所以存在歧义。您可以通过在路径模板中使用正则表达式来解决此问题,使其更加具体。即自"{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. E.g.: login/{username}/{password}
and ranges/{from}/{to}
.
无论如何,您确定没有用户会从用户名和密码中选择普通数字吗?看起来在您的情况下,为两个资源中的每一个使用不同的 URI“子空间”会好得多。例如:login/{username}/{password}
和ranges/{from}/{to}
。
BUT, few points on the design:
但是,设计上的几点:
- It is a very badidea to put passwords in the URI. Never ever do it! Look at some existing proven authentication schemes - don't try to reinvent the wheel.
- Consider using query params for specifying ranges - e.g. if you have some collection resource (like "myapp.com/calendar/events") you can model ranges using query parameters - e.g. "myapp.com/calendar/events?from=xxx&to=yyy.
- 将密码放在 URI 中是一个非常糟糕的主意。永远不要这样做!查看一些现有的经过验证的身份验证方案 - 不要试图重新发明轮子。
- 考虑使用查询参数来指定范围 - 例如,如果您有一些集合资源(例如“myapp.com/calendar/events”),您可以使用查询参数对范围进行建模 - 例如“myapp.com/calendar/events?from=xxx&to=yyy .