How do I reuse multiple DTOs (spring boot)

Viewed 336

I am a student who wants to implement api server using spring.

Currently, I use request and response to per api. (If api exists for signin, I have created AccountSignInDto.Request, AccountSignInResponse.) but, I felt that there were more and more duplicate codes.

I wonder how I can reuse multiple dto to reduce duplicate code.

The details are as follows.

  • AccountCreateDto.java
  • AccountFindIdByEmailDto.java
  • AccountFindPasswordDto.java
  • AccountReadDto.java
  • AccountSignInDto.java
  • AccountUpdateDto.java
  • QuestionReadDto.java
package com.se.apiserver.v1.account.application.dto;

import com.se.apiserver.v1.account.domain.entity.AccountType;

import javax.validation.constraints.Email;
import javax.validation.constraints.Size;

import io.swagger.annotations.ApiModel;
import io.swagger.annotations.ApiModelProperty;
import lombok.*;

public class AccountCreateDto {

  @Data
  @NoArgsConstructor
  @AllArgsConstructor
  @ApiModel("signIn request")
  @Builder
  static public class Request {

    @ApiModelProperty(example = "account", notes = "id")
    @Size(min = 4, max = 20)
    private String id;

    @ApiModelProperty(example = "password", notes = "password")
    @Size(min = 8, max = 20)
    private String password;

    @ApiModelProperty(example = "name", notes = "name")
    @Size(min = 2, max = 20)
    private String name;

    @ApiModelProperty(example = "nickname", notes = "nick-name")
    @Size(min = 2, max = 20)
    private String nickname;

    @ApiModelProperty(example = "11110000", notes = "stu-num")
    @Size(min = 8, max = 20)
    private String studentId;

    @ApiModelProperty(example = "STUDENT", notes = "account type")
    private AccountType type;

    @ApiModelProperty(example = "01012345678", notes = "phone-number, 00011112222")
    @Size(min = 10, max = 20)
    private String phoneNumber;

    @ApiModelProperty(example = "abc@def.com", notes = "email")
    @Email
    private String email;

    @ApiModelProperty(example = "1", notes = "question number")
    private Long questionId;

    @ApiModelProperty(example = "region", notes = "answer")
    @Size(min = 2, max = 100)
    private String answer;


  }

  @Data
  @AllArgsConstructor
  @ApiModel("signIn response")
  static public class Response {

    @ApiModelProperty(example = "1", notes = "account pk")
    private Long id;
  }

}

I'm sorry for my poor English. Thanks in advance!

1 Answers

Try declare Request as a abstract class, and all other dtos extend Request. Like code shows below:

public abstract class Request {

    @ApiModelProperty(example = "account", notes = "id")
    @Size(min = 4, max = 20)
    private String id;

    @ApiModelProperty(example = "password", notes = "password")
    @Size(min = 8, max = 20)
    private String password;
     
    ...
}

public class AccountCreateDto extends Request {

}

public class AccountUpdateDto extends Request {

}

This example ignore the influence of lombok.

Related