【问题标题】:JOIN different methods with different query parameters : REST使用不同的查询参数加入不同的方法:REST
【发布时间】:2018-04-14 11:47:09
【问题描述】:

任何替代方案,因为我认为我无法重载 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));
}

【问题讨论】:

  • 在一种方法中使用多个 QueryParam 还不够吗?

标签: java api get


【解决方案1】:

您可以考虑使用 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

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2020-11-11
    • 2021-09-18
    • 2018-08-12
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多