0

I want to create a DTO. The DTO will be used to map HTTP request to a REST webservice in spring. My controller looks like this:

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
}

In the above signature the DocSearchRequest is the DTO I want to create. The DTO has some fields like:

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

// setters and getters.

Do we have a better way to implement the DTO? One thing to keep in mind is Spring uses reflection to set the values from the request to the DTO.

alexbt
  • 16,415
  • 6
  • 78
  • 87
Albert Pinto
  • 392
  • 2
  • 6
  • 17
  • 1
    Why don't you use a List for the Criterias? – dunni Oct 05 '15 at 05:56
  • i can try that but not sure if spring will be able to map the request to the body using reflections. – Albert Pinto Oct 05 '15 at 05:58
  • 1
    If you provide the values in the JSON as list, then it will be able to do that. – dunni Oct 05 '15 at 08:19
  • 1
    You can use a `List` as @dunni suggested, or you can create a simple class containing private fields with public getters and setters, or you could also use a `Map` and use `get("searchCriteria1")` etc methods to get the values. – fps Oct 05 '15 at 12:49
  • @dunni and Federico I implement a List and in my request i send my criteria as a coma separated. I all works as charm... thanks for the suggestion. – Albert Pinto Oct 13 '15 at 00:45

2 Answers2

0

A List of cafeterias solved the problem. I have to send the request as a comma separated value ans Spring takes care of mapping. Same can be done for the Response.

Albert Pinto
  • 392
  • 2
  • 6
  • 17
-1

Do we have a better way to implement the DTO.

There is no business logic in DTO. It looks like you don't have too. It should represent the state of object . It should have private instance fields and their getters/setters. Basically should follow the encapsulation/abstraction.

M Sach
  • 33,416
  • 76
  • 221
  • 314