在我的应用中,用户可以注册,用户可以在锻炼上发布锻炼和评论。
我设计了以下资源和URI:
USERS
/users
/users/{id}
WORKOUTS
/workouts
/workouts/{id}
COMMENTS
/workouts{id}/comments
/workouts{id}/comments/{id}
由于/ workouts URI返回时间轴,我试图映射用户的锻炼,例如/ workouts?user = userId
我不知道如何通过@QueryParam区分get all collection和get collection,因为:
@GET
@Produces({MediaType.APPLICATION_XML, MediaType.APPLICATION_JSON})
public Practicas getPracticas() {
List<Practica> listaPracticas = practicaDao.getPracticas();
Practicas practicas = new Practicas();
practicas.setPracticas(listaPracticas);
return practicas;
}
@GET
@Produces({MediaType.APPLICATION_XML, MediaType.APPLICATION_JSON})
public Practicas getPracticasUsuario(@QueryParam("idUsuario") int idUsuario) {
List<Practica> listaPracticas = practicaDao.getPracticasByUser(idUsuario);
Practicas practicas = new Practicas();
practicas.setPracticas(listaPracticas);
return practicas;
}
抛出以下异常:
org.glassfish.jersey.server.model.ModelValidationException: Validation of the application resource model has failed during application initialization.
[[FATAL] A resource model has ambiguous (sub-)resource method for HTTP method GET and input mime-types as defined by @Consumes and @Produces annotations at Java methods public com.sporty.model.Practicas com.sporty.resource.PracticasResource.getPracticas() and public com.sporty.model.Practicas com.sporty.resource.PracticasResource.getPracticasUsuario(int) at matching regular expression /practicas. These two methods produces and consumes exactly the same mime-types and therefore their invocation as a resource methods will always fail.; source='org.glassfish.jersey.server.model.RuntimeResource@1cc465f']
解决方案是否在URI上添加了另一个级别,例如/ workouts / search?user = userId?
是否存在源自糟糕的URI设计的问题?
提前致谢
答案 0 :(得分:1)
我认为你正在寻找类似的东西:
@GET
@Produces({MediaType.APPLICATION_XML, MediaType.APPLICATION_JSON})
public Practicas getPracticasUsuario(@QueryParam("idUsuario") Integer idUsuario) {
final List<Practica> listaPracticas;
if (idUsario != null) {
practicaDao.getPracticasByUser(idUsuario);
} else {
practicaDao.getPracticas();
}
Practicas practicas = new Practicas();
practicas.setPracticas(listaPracticas);
return practicas;
}