如何在我的其余API的不同模块中使用相同的模型类

时间:2018-04-15 02:07:31

标签: spring rest

背景

我有一个用于REST API的Spring启动应用程序,我必须创建基于休息的微服务。 对于不同的休息,我必须将请求体传递为json数据。 请求json数据例如:

1

{ "query":"", "variables": { "input": { "name": "", "description": "", "gitUrl": "", "repoName": "" } } }

  1. { "query":"", "variables": { "input": { "name": "" } } }

    3。 { "query":"", "variables": { "search": "" } }

    我不想在我的API中将所有参数作为请求体传递,有些只有Query,Variables,Input有些只有Query和变量如上面的json数据 我想创建相同的模型,可以在所有其他apis中使用。现在我已经为每个API创建了不同的模型。

    1. 创建请求:

      public class CreatetRequest {

      private String query;
      private Variables variables;
      

      }

    2. public class Variables {
      
          private Input input;
      }
      

      public class Input {
      
          private String name;
          private String description;
          private String gitUrl;
          private String repoName;
      }
      

      这里我在所有API中复制我的模型,所以我想创建三个模型类,其中包含所有必需的变量,这些变量在我的启动应用程序中很常见,但同时我必须避免在请求中发送所有数据我的休息时间。

      春季应用中最好的方法是避免这种锅炉板代码

1 个答案:

答案 0 :(得分:0)

使用带@JsonDeserialize注释的界面

import com.fasterxml.jackson.annotation.JsonSubTypes;
import com.fasterxml.jackson.annotation.JsonSubTypes.Type;
import com.fasterxml.jackson.databind.annotation.JsonDeserialize;

@JsonDeserialize(using = InputDeserializer.class)
public interface InputInterface {

}

实现此接口的输入对象类型

import com.fasterxml.jackson.databind.annotation.JsonDeserialize;

@JsonDeserialize(as = Type1.class)
public class Type1 implements InputInterface {

    private String name;
    private String description;
    private String gitUrl;
    private String repoName;

    // setters, getters, constructor
}

实现此接口的输入对象类型

import com.fasterxml.jackson.databind.annotation.JsonDeserialize;

@JsonDeserialize(as = Type2.class)
public class Type2 implements InputInterface{

    String name;

    // setters, getters, constructor
}

为接口

实现反序列化器

在deserialize方法中,您可以通过请求正文中的参数来检查输入的类型。

为简化起见,我们只使用两种类型。

import com.fasterxml.jackson.core.JsonParser;
import com.fasterxml.jackson.databind.DeserializationContext;
import com.fasterxml.jackson.databind.JsonDeserializer;
import com.fasterxml.jackson.databind.ObjectMapper;
import com.fasterxml.jackson.databind.node.ObjectNode;

public class InputDeserializer extends JsonDeserializer<InputInterface> {

       @Override
        public InputInterface deserialize(JsonParser jp, DeserializationContext context) throws IOException {
            ObjectMapper mapper = (ObjectMapper) jp.getCodec();
            ObjectNode root = mapper.readTree(jp);

            /* 
                  Write your conditions here 
                  to check what type of input is

             */
            if (root.has("name") && root.has("description")) {
                return mapper.readValue(root.toString(), Type1.class);
            } else {
                return mapper.readValue(root.toString(), Type2.class);
            }

        }

}

使用控制器中的界面

@PostMapping()
public void save(@RequestBody InputInterface input) throws Exception {
    // save the customer using service
    System.out.println(input);

}

这只是为了展示它的工作方式,使其适应您的需求: - )