jax-rs路由优先级冲突

时间:2015-02-25 08:12:42

标签: java routing jax-rs

我在Spring中使用jax-rs。我有2个PUT API,它们有冲突,我想优先考虑其中一个。

@PUT
@Path("/allQuestions")
public Response method1() {}

@PUT
@Path("/{qustionId}")
public Response method2(@PathParam("qustionId") long qustionId) {}

当我致电/allQuestions时,应用程序始终会尝试插入' allQuestions'字符串加入qustionId的{​​{1}},因此我想优先考虑method2

我该怎么做?

1 个答案:

答案 0 :(得分:0)

问题是在method1之前缺少@Consumes({ MediaType.APPLICATION_JSON })行。完整的代码是:

@PUT
@Path("/availableQuestions")
@Produces({ MediaType.APPLICATION_JSON })
public Response method1() {}

@PUT
@Path("/{questionId}")
@Produces({ MediaType.APPLICATION_JSON })
@Consumes({ MediaType.APPLICATION_JSON })
public Response method2(@PathParam("questionId") long questionId) {}

我不确定原因(编辑:请参阅@peeskillet评论以获得解释),但现在我添加了@Consumes({ MediaType.APPLICATION_JSON })

@PUT
@Path("/availableQuestions")
@Produces({ MediaType.APPLICATION_JSON })
@Consumes({ MediaType.APPLICATION_JSON })
public Response method1() {}

它只是有效。