处理大量的查询参数

huangapple go评论75阅读模式
英文:

Handle large number of query parameters

问题

我是新手学习Spring。我的控制器需要处理大量的查询参数。以下是我目前的代码:

@GET
@Path("myresource")
@Consumes({ MediaType.APPLICATION_JSON })
@Produces({ MediaType.APPLICATION_JSON })
public String getResource
(
  @QueryParam("param1") final String param1,
  @QueryParam("param2") final String param2,
  @QueryParam("param3") final String param3,
  @QueryParam("param4") final String param4,
  @QueryParam("param5") final String param5
) throws Exception
{
  /* 处理查询参数 */
}

这种编码风格看起来对我来说有点不太好看。我想知道是否有更好的方法来处理(相对较多的)查询参数,例如某种注解,可以帮助我将所有查询参数存储到一个POJO中?

英文:

I'm new to Spring. My controller needs to handle a considerable number of query params. Here's what my code looks like at the moment:

@GET
@Path("myresource")
@Consumes({ MediaType.APPLICATION_JSON } )
@Produces({ MediaType.APPLICATION_JSON } )
public String getResource
(
  @QueryParam("param1") final String param1,
  @QueryParam("param2") final String param2,
  @QueryParam("param3") final String param3,
  @QueryParam("param4") final String param4,
  @QueryParam("param5") final String param5
) throws Exception
{
  /* Handle query params */
}

This style of coding looks rather ugly to me. I wonder if there's some better way to handle (relatively) large number of query params, for instance some kind of annotation that would help me store all the query params into a POJO?

答案1

得分: 1

// 创建一个模型以用于参数并将其用于端点。POJO不需要任何额外的注释。它应该有一个字段列表,与将从HTTP请求绑定的请求参数匹配,标准的getter/setter和无参构造函数。

class ParamsModel {
    final String param1;
    final String param2;
    ...
    // getter setter constructor
}

@GET
@Path("myresource")
@Consumes({ MediaType.APPLICATION_JSON })
@Produces({ MediaType.APPLICATION_JSON })
public String getResource(ParamsModel model) throws Exception {
    /* 处理查询参数 */
}
英文:

Create a model for param and use it endpoint. The POJO doesn’t require any additional annotations. It should have a list of fields which match with request parameters that will be bound from the HTTP request, standard getters/setters, and a no-argument constructor.

class ParamsModel {
   final String param1;
   final String param2;
   ...
   //getter setter constructor
}

@GET
@Path("myresource")
@Consumes({ MediaType.APPLICATION_JSON } )
@Produces({ MediaType.APPLICATION_JSON } )
public String getResource(ParamsModel model) throws Exception
{
  /* Handle query params */
}

答案2

得分: -1

是的。您可以将所有的String参数替换为

@RequestBody SomePOJO requestData

实际上,将JSON数据发送到您的端点(包含与您的POJO相对应的数据)。但是,您应该使用@POST端点而不是@GET

参考:Spring的RequestBody和ResponseBody注解

英文:

Yes. You can replace all your String parameters by

@RequestBody SomePOJO requestData

and actually send JSON data to your endpoint (containing data corresponding to your POJO). But you should use a @POST endpoint instead of @GET.

For reference: Spring’s RequestBody and ResponseBody Annotations

huangapple
  • 本文由 发表于 2020年8月3日 17:33:07
  • 转载请务必保留本文链接:https://go.coder-hub.com/63226958.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定