Java Jersey - Same method with different parameter on same @path?

不羁岁月 提交于 2020-01-03 02:31:10

问题


I'm newbie on creating web services with Jersey and I'm facing with this problem:

@GET
@Path("/logoutUser")
@Produces(MediaType.APPLICATION_JSON + ";charset=utf-8")
public Response logoutUser(@QueryParam("userName") String userName) {

    if (userName.equalsIgnoreCase("jmart") || userName.equalsIgnoreCase("jromero")) {
        return Response.status(Response.Status.OK).entity("Logout realizado").type(MediaType.APPLICATION_JSON).build();
    }else {
        throw new CustomNotFoundException("No se ha podido realizar el logout del usuario " + userName);
    }
}

@GET
@Path("/logoutUser")
@Produces(MediaType.APPLICATION_JSON + ";charset=utf-8")
public Response logoutUser(@QueryParam("idUsuario") int idUsuario) {

    if (idUsuario == 1 || idUsuario == 2) {
        return Response.status(Response.Status.OK).entity("Logout realizado").type(MediaType.APPLICATION_JSON).build();
    }else {
        throw new CustomNotFoundException("No se ha podido realizar el logout del usuario " + idUsuario);
    }
}

Obviously when I'm trying to call any of the two methods my server throws an exception.

Is there any solution of implementing the same method with different parameter on the same path?


回答1:


I don't think you can do what you are trying to do with Jersey. Even if you were to switch to an @PathParam it isn't going to give you the control that you are looking for. If I were you though, I wouldn't try to do the conditional logic in Jersey anyway, it is honestly a bit confusing. Instead I would get all the @QueryParams in a list and then look to see if what you want is in that list. See the example here. The other option is to just get both @QueryParams and see which one is null. Since @QueryParams are optional you should check for null anyway. Your new code might look like:

@GET
@Path("/logoutUser")
@Produces(MediaType.APPLICATION_JSON + ";charset=utf-8")
public Response logoutUser(@QueryParam("idUsuario") int idUsuario, @QueryParam("userName") String userName) {

    if(userName != null) {
        //do what you want with the username
    }
    else if (idUsuario == 1 || idUsuario == 2) {
        //do what you want with the id
    }
}



回答2:


If you want to apply conditional logic to the same path an alternative solution is to utilize sub-resource locators created from classes (also very useful for API versioning).

@Path("/users")
public class UsersResource {

    @Path("logout")
    @GET
    @Produces(MediaType.APPLICATION_JSON + ";charset=utf-8")
    public Class<?> logout(@QueryParam("condition") String condition) {
        if (check(condition)) {
            return ConditionalLogoutA.class;
        } else {
            return ConditionalLogoutB.class;
        }
    }
}

@Singleton
public class ConditionalLogoutA {
    @Path("/")
    public void logout(@QueryParam("userName") String userName) {};
}

@Singleton
public class ConditionalLogoutB {
    @Path("/")
    public void logout(@QueryParam("userName") String userName) {};
}


来源:https://stackoverflow.com/questions/19249186/java-jersey-same-method-with-different-parameter-on-same-path

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