使用不同的查询参数加入不同的方法:REST

时间:2018-04-14 11:47:09

标签: java api get

任何替代方案,因为我认为我无法重载REST请求。 但我也希望按年或其他任何方式进行搜索。 我已经读过我应该使用RequestMapping或类似的东西,但我不会使用spring。我想将查询字符串(包含所有过滤器)传递给该方法。关于我如何才能拥有唯一方法的任何想法都可以吗?

@GET
@Path("/")
@Produces(MediaType.APPLICATION_JSON)
public Response getMovieByName(@Context HttpServletRequest req, @QueryParam("text") String name) {
    if (!checkLoggedIn(req)) {
        throw new WebApplicationException("User not logged.");
    }
    return buildResponse(movieService.getMovieByName(name));
}


@GET
@Path("/")
@Produces(MediaType.APPLICATION_JSON)
public Response getNumberOfMovies(@Context HttpServletRequest req, @QueryParam("n_movies") Integer total) {

    if (!checkLoggedIn(req)) {
    throw new WebApplicationException("User not logged.");
}
    return buildResponse(movieService.getNumberOfMovies(total));
}

1 个答案:

答案 0 :(得分:0)

你能考虑使用Map<>如下所示:

@RequestMapping(value = "/get-with-params",
                method = RequestMethod.GET)
public String sampleQueryParam(@RequestParam Map<String, String> params) {
    return "You've hit the '/sample/get-with-params' request handler with the following query params:\n" + params;
}

更多信息请点击此处:http://reypader.github.io/2015/12/21/spring-request-mapping.html

https://docs.spring.io/spring-restdocs/docs/2.0.1.RELEASE/reference/html5/#introduction