【问题标题】:How to avoid DTO to domain conversion and vice versa in Spring boot?如何在 Spring Boot 中避免 DTO 到域的转换,反之亦然?
【发布时间】:2020-09-14 19:30:46
【问题描述】:

在我的项目中,我从 UI 获取值,传递给控制器​​和服务层。然后我做了DTO到域的转换,我觉得这是不必要的,也不是一个好的设计。

例如这是我的 JSP:

<form:form id="parcelCondemnationCaseDetailsForm"
modelAttribute="parcelCondemnationCaseDetailsDTO">
<form:hidden path="prclSeqNum" />
<form:hidden path="condemnationStatusCode" />
<form:hidden id="isMDOTVerifiedMoveDateAvailable"
    path="mDOTVerifiedMoveDateAvailable" />
<div class="form-group">
    <div class="row">
        <div class="col-md-3">
            <label for="finalDisposition">Final Disposition</label>
            <form:select class="form-control dirty _enableSaveDisable"
                id="finalDisposition" path="finalDisposition"
                disabled="${agSecurityFinalDisabled}"
                onchange="finalDispositionChange()">
                <form:option value="" select="select">Select</form:option>
                <form:option value="J">Judgement</form:option>
                <form:option value="V">Jury Verdict</form:option>
                <form:option value="S">Settlement</form:option>
                <form:option value="O">Other</form:option>
            </form:select>
        </div>
        <div class="col-md-3" id="finalDispositionDescriptionDiv"
            style="display: none;">
            <label for="finalDispositionDescription" class="req-label">Final
                Disposition Description</label>
            <form:input type='text' id="finalDispositionDescription"
                class="form-control dirty _enableSaveDisable" aria-required="true"
                path="finalDispositionDescription" maxlength="50"
                disabled="${agSecurityFinalDisabled}" />
        </div>
        <div class="col-md-3">
            <label for="finalDispositionDate">Final Disposition Date</label>
            <form:input type='text' id="finalDispositionDate"
                class="form-control lamdaDate dirty _addDate _pastPresent _enableSaveDisable"
                path="finalDispositionDate" disabled="${agSecurityFinalDisabled}" />

        </div>
        <div class="col-md-3">
            <label for="closedDate">Closed Date </label>
            <form:input type='text' id="closedDate"
                class="form-control lamdaDate dirty _addDate _pastPresent  _enableSaveDisable"
                path="closedDate" disabled="${agSecurityFinalDisabled}" />

        </div>
    </div></form:form>

这是我的控制器

    @PostMapping("/saveCaseDetails.htm")
public @ResponseBody String saveCaseDetails(ParcelCondemnationCaseDetailsDTO parcelCondemnationCaseDetailsDTO,
        ModelMap model) {
    String responseMessage = "";
    try {
        parcelCondemnationCaseDetailsDTO = parcelCondemnationCaseDetailsService
                .saveCaseDetails(parcelCondemnationCaseDetailsDTO);
        responseMessage = "success";
    } catch (Exception e) {
        log.error(
                "Error occured while saving Condemnation > Valuation at CondemnationValuationController.saveValuation",
                e);
        responseMessage = "Error occured while saving Case Details.";
    }
    return responseMessage;
}

和服务层。

    public ParcelCondemnationCaseDetailsDTO saveCaseDetails(
            ParcelCondemnationCaseDetailsDTO parcelCondemnationCaseDetailsDTO) {
        ParcelCondemnation parcelCondemnation = ParcelCondemnationCaseDetailsMapper.INSTANCE
                .getCaseDetails(parcelCondemnationCaseDetailsDTO);
parcelCondemnationRepository.save(parcelCondemnation);

在进行 DTO 到域的转换时,我没有看到我在添加任何价值。 那么,我的问题是有什么方法可以避免使用 DTO、映射器和额外的代码行?

感谢收看。

【问题讨论】:

    标签: java spring spring-boot controller dto


    【解决方案1】:

    由于您的实体正是您的 DTO,您可以在控制器中直接在实体类中使用 @JsonView 注释,它可以过滤您希望作为响应显示给 UI 的字段。

    @PostMapping("/saveCaseDetails.htm")
    @JsonView(View.Public.class)
    public @ResponseBody String saveCaseDetail
    ...
    
    
    @Entity
    public ParcelCondemnation {
        
    @JsonView(View.Public.class)
    private String anyFieldtoShow;
    
    private String anyFieldToNotShow;
    ...
    
    public class Views {
        public static class Public {}
    }
     
    

    在这种情况下,您的响应将仅显示 anyFieldToShow 作为对带有相关 JsonView 注释的该端点的任何请求的回答。

    这里有一个更好的解释: Baeldung tutorial

    而且我知道你也许可以使用 GraphQL,不幸的是我没有足够的知识来谈论它,但如果你想深入了解,这里有链接:GraphQL doc

    【讨论】:

    • 感谢您的 cmets,但是,如何处理我的日期是 UI 和货币格式表示的金额的情况? UI 可能会以字符串形式发送日期,并以字符串形式再次发送当前日期。
    • 不客气。我不明白你的意思,你能用更多细节更新你的问题或创建另一个问题吗?
    猜你喜欢
    • 2015-04-26
    • 1970-01-01
    • 1970-01-01
    • 2014-02-26
    • 2017-06-11
    • 1970-01-01
    • 2016-12-05
    • 2022-06-11
    • 2011-09-30
    相关资源
    最近更新 更多