Spring Boot - 不同的模型表示/多个 API [英] Spring Boot - Different model representations / Multiple APIs

查看:26
本文介绍了Spring Boot - 不同的模型表示/多个 API的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我的后端必须提供两种不同的 API - 对相同模型的不同访问,分别是相同的实现和相同的数据库映射.模型作为 JSON 发送,后端以相同的方式使用它们.

my backend must offer two different APIs - different access to the same models, respectively, same implementation and same mappings to the database. Models are send as JSONs and they are consumed by the backend in the same way.

但是每个 API 都需要不同的 JSON 表示.F.e.我想以不同的方式命名一些字段(w/@JsonProperty f.e.)或想省略一些.

But different JSON representations are necessary on each API. F.e. I'd like to name some fields differently (w/ @JsonProperty f.e.) or want to omit some.

如前所述,控制器应以与生产它们相同的方式使用它们.

As mentioned, they should be consumed by the controllers in the same way they are produced.

因为只有表示不同:是否有一种简单且符合 DRY 的方法来实现这一点?

Since only the representation differs: is there a simple and DRY compliant way to accomplish this?

例如:

打电话

ProductsController.java

    sym/products/1

应该返回

{
  "id": 1,
  "title": "stuff",
  "label": "junk"
}

并打电话

ProductsController.java

    frontend/products/1

应该返回

{
  "id": 1,
  "label": "junk",
  "description": "oxmox",
  "even-more": "text"
}

非常感谢!

蒂姆

推荐答案

单独的 DTO 可能是最好的解决方案.

Separate DTOs may be the best solution.

另一种方法(假设您使用 Jackson)是让一个 DTO 包含所有不同的字段,然后使用 MixIns 来控制 DTO 的序列化方式.

An alternate (assuming you are using Jackson) is to have one DTO with the all the different fields, and then use MixIns to control how the DTO is serialized.

import com.fasterxml.jackson.annotation.JsonIgnore;
import com.fasterxml.jackson.databind.ObjectMapper;

public class Main {

    public static void main(String[] args) throws Exception  {

        ObjectMapper objectMapper = new ObjectMapper();
        objectMapper.addMixIn(SomeDTOWithLabel.class, IgnoreLabelMixin.class);

        SomeDTOWithLabel dto = new SomeDTOWithLabel();
        dto.setLabel("Hello World");
        dto.setOtherProperty("Other property");
        String json = objectMapper.writeValueAsString(dto);
        System.out.println("json = " + json);
    }

    public static class SomeDTOWithLabel {
        private String label;
        private String otherProperty;

        public String getOtherProperty() {
            return otherProperty;
        }

        public void setOtherProperty(String otherProperty) {
            this.otherProperty = otherProperty;
        }

        public String getLabel() {
            return label;
        }
        public void setLabel(String label) {
            this.label = label;
        }
    }

    public abstract class IgnoreLabelMixin {
        @JsonIgnore
        public abstract String getLabel();

    }
}

例如,我们的 DTO 具有旧客户端可能仍然依赖的已弃用属性,但我们不想将它们发送给新客户端,因此我们使用 MixIns 来抑制它们.

For instance we have DTOs with deprecated properties that old clients may still depend on, but we don't want to send them to newer client, so we use MixIns to supress them.

这篇关于Spring Boot - 不同的模型表示/多个 API的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆