DTO中的相似领域

Similar fields in DTO

我想创建一个 DTO。 DTO 将用于将 HTTP 请求映射到 spring 中的 REST 网络服务。我的控制器看起来像这样:

RequestMapping( value = DmsRestSvcApi.DOCUMENT_SEARCH_PATH, method = RequestMethod.POST, produces = { MediaType.APPLICATION_JSON_VALUE } )
public List<DocSearchResponse> getDocumentInfoJson( @Validated @RequestBody DocSearchRequest oDocSearchRequest ) throws Exception {
    // do something
}

在上面的签名中,DocSearchRequest 是我要创建的 DTO。 DTO 有一些字段,例如:

private String searchCriteria1;
private String searchCriteria2;
/*
  .
  .
  .
 */
private String searchCriteria20;

// setters and getters.

我们是否有更好的方法来实现 DTO?要记住的一件事是 Spring 使用反射来设置从请求到 DTO 的值。

Do we have a better way to implement the DTO.

DTO 中没有业务逻辑。看来你也没有。它应该代表对象的状态。它应该有私有实例字段及其 getters/setters。基本上应该遵循encapsulation/abstraction.

自助餐厅列表解决了这个问题。我必须将请求作为逗号分隔值发送,ans Spring 负责映射。响应也可以这样做。