简体   繁体   English

泽西制作媒体类型冲突

[英]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: 但是,设计上几点:

  1. It is a very bad idea to put passwords in the URI. 将密码放在URI中是一个非常糟糕的主意。 Never ever do it! 从来没有做过! Look at some existing proven authentication schemes - don't try to reinvent the wheel. 看看一些现有的经过验证的认证方案 - 不要试图重新发明轮子。
  2. Consider using query params for specifying ranges - eg if you have some collection resource (like "myapp.com/calendar/events") you can model ranges using query parameters - eg "myapp.com/calendar/events?from=xxx&to=yyy. 考虑使用查询参数指定范围 - 例如,如果您有一些收集资源(如“myapp.com/calendar/events”),您可以使用查询参数对范围进行建模 - 例如“myapp.com/calendar/events?from=xxx&to=yyy 。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM