[英]How do I reuse multiple DTOs (spring boot)
我是一個想用spring實現api server的學生。
目前,我對每個 api 使用請求和響應。 (如果 api 存在用於登錄,我已經創建了 AccountSignInDto.Request、AccountSignInResponse。)但是,我覺得有越來越多的重復代碼。
我想知道如何重用多個 dto 來減少重復代碼。
詳情如下。
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;
}
}
我很抱歉我的英語不好。 提前致謝!
嘗試將Request
聲明為抽象類,所有其他 dto 都擴展Request
。 像下面的代碼顯示:
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 {
}
這個例子忽略了 lombok 的影響。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.