1

I am Java web developer, usually develop Spring MVC.

I have been using @RequestMapping or @RequestParam for mapping to hashMap at Controller.

It is a terrible way. I should always cast type when using value.

But nowadays I try to use @ModelAttribute to write clean code at Controller.

However, there are some problem.

case 1) make DTO for each EndPoint.

We can make DTO for each EndPoint, but DTO will have many duplicated property.

@Getter
@Setter
@ToString
class GetUserInfoDTO {
   private String id;
   private String name;
}


@Getter
@Setter
@ToString
class PostUserInfoDTO {
   private String name;
   private Integer age;
   private String address;
   private String gender;
   private String email;
   private Date joinDate;
}

in controller,

@GetMapping("/user")
public ResultDTO getUserInfo (@ModelAttribute GetUserInfoDTO){
    ...
     return ResultDTO;
}

@PostMapping("/user")
public ResultDTO postUserInfo (@ModelAttribute PostUserInfoDTO){
    ...
     return ResultDTO;
}

In this case, we can apply independent validation strategy for each End-Point.

for example..

@Getter
@Setter
@ToString
class GetUserInfoDTO {
   @NotNull
   private String id;
   private String name;
}


@Getter
@Setter
@ToString
class PostUserInfoDTO {

   @NotNull
   private String name;
   @NotNull
   private Integer age;
   @NotEmpty
   private String address;
   private String gender;
   private String email;
   private Date joinDate;
}

like this.

But so many model classes made, and so many duplicated property exists.

case 2. make common DTO for each Controller.

We can make DTO for each Controller, and reuse them.

@Getter
@Setter
@ToString
class UserInfoDTO {
   private String id;
   private String name;
   private Integer age;
   private String address;
   private String gender;
   private String email;
   private Date joinDate;
}


@GetMapping("/user")
public ResultDTO getUserInfo (@ModelAttribute UserInfoDTO){
    //I want only id, name
    ...
     return ResultDTO;
}

@PostMapping("/user")
public ResultDTO postUserInfo (@ModelAttribute UserInfoDTO){
    ...
     return ResultDTO;
}

But In this case, we can only pass specific properties.

If someone send other parameter than id and name, we can't notice. ( 400 error not occur )

Code assistance can't recommend us specific properties that use at single end-point.

I don't like these cases.

In first case, I should make so many models and It's management will be so hard.

Second case, unnecessary properties exists and it hard to validate for each end-point.

Which way is the best?

Or Can you recommend another way for mapping request parameter to model object?

Jalil.Jarjanazy
  • 839
  • 1
  • 9
  • 22
  • We can get all the parameters and values as a map instance. Please check this [answer](https://stackoverflow.com/a/60304941/4214241) . This method would require explicit code to populate the model object – R.G May 17 '20 at 11:38
  • https://stackoverflow.com/questions/37386758/is-it-bad-to-create-different-classes-for-rest-request-and-response – meobeo173 May 18 '20 at 03:12

0 Answers0