How can I overload a method with @QueryParam in Jersey/Spring?

为君一笑 提交于 2019-12-12 11:35:12

问题


I would like to overload a method with the @QueryParam but everytime I try to execute this code it throws:

SEVERE: Exception occurred when intialization
com.sun.jersey.spi.inject.Errors$ErrorMessagesException

My code is:

@GET
@Path("/test")
@Produces("text/plain")
public String getText(@QueryParam("PID") String pid)
{
  return pid;
}

@GET
@Path("/test")
@Produces("text/plain")
public String getText(@QueryParam("PID") String pid, @QueryParam("NAME") String name)
{
  return pid + name;
}

回答1:


No can be.

It's ok in java, but the thing about it is - from the servlet side - jersey needs to map each url to some function in your class.

What you can do is, of course, separate it into 2 methods, or build one method that checks the parameters and does the right logic.




回答2:


You can overload the rest endpoint in terms of what request/query parameters are present in the request.

Here's the answer that solved my use case: create two method for same url pattern with different arguments



来源:https://stackoverflow.com/questions/10030045/how-can-i-overload-a-method-with-queryparam-in-jersey-spring

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!