English 中文(简体)
In Java Spring, 2. 如何确定消费动态参数的终点
原标题:In Java Spring, How to create an endpoint that consumes a dynamic types of parameter

我想制造一个使用有活力的参数的APIC端点。

收到的JSON参数可考虑以下任一因素:

{
  entityType: "A",
  createList:[
      {row: 1, name: "sted", department:"drulp"}, 
      {row: 2, name: "mary", department:"kko"}, 
  ],
   updateList:[
      {row: 3, name: "sddf", department:"fff"}, 
      {row: 4, name: "few", department:"kko"}, 
  ]
}
{
  entityType: "B",
  createList:[
      {firstName: "John", phone: "0412345678", country:"USA"}, 
      {firstName: "Mary", phone: "0412247474", country:"CANADA"}, 
  ],
   updateList:[
      {firstName: "Tim", phone: "0412345678", country:"Australia"}, 
      {firstName: "Luke", phone: "042728282", country:"CANADA"}, 
  ]
}

综上所述,我们可以看到,不同的<代码>entityType/code>可能具有不同类型的createListupdateList

现在,我正在制造一个小端点,接收这些Json数据。 我的法典如下:

主计长

@RequestMapping(method = RequestMethod.POST, value = "/import")
public void executeImport(@RequestBody ImportRequest<? extends ImportModel> request) {
    // some codes...
}

<代码>ImportRequest 班级:

public class ImportRequest<T extends ImportModel>  {
    private String entityType;
    private List<T> createList;
    private List<T> updateList;

The ImportModel class:

public abstract class ImportModel {
}

<代码>ImportTypeAModel 班级:

public class ImportTypeAModel extends ImportModel {
    private Integer row;
    private String name;
    private String department;
    // Getters and Setters
}

The ImportTypeBModel class:

public class ImportTypeBModel extends ImportModel {
    private String firstName;
    private String phone;
    private String country;
    // Getters and Setters
}

然而,当我打电话给APIC时,它显示了以下信息:

Cannot construct instance of `com.core.domain.ImportModel` (no Creators, like default constructor, exist): abstract types either need to be mapped to concrete types, have custom deserializer, or contain additional type information
 at [Source: (PushbackInputStream); line: 1, column: 44] (through reference chain: com.core.domain.ImportRequest["createList"]->java.util.ArrayList[0])

我怎么能够创造最终点来消耗这些动态的类型参数?

问题回答

其中一些评论包括每种类型的习惯控制器、定制序列器/蒸汽器等。 不需要。 如果你用地图模仿你的类别,春天会支持这个盒子。 创造 清单等只是<代码>。 ArrayList<LinkedHashMap<String, String>>。

然后,你会把一切作为通用的关键/价值。

如果您不希望转换成<代码>St,你可使用<代码>String, Object。

这里需要做的是:

@Data
@JsonTypeInfo(use = JsonTypeInfo.Id.NAME, include = JsonTypeInfo.As.PROPERTY, property = "type")
@JsonSubTypes({
        @JsonSubTypes.Type(value = ImportTypeAModel.class, name = "A"),
        @JsonSubTypes.Type(value = ImportTypeBModel.class, name = "B")
})
public abstract class ImportModel {
}

@Data
@ToString
public class ImportTypeAModel extends ImportModel {
    private Integer row;
    private String name;
    private String department;
}

@Data
@ToString
public class ImportTypeBModel extends ImportModel {
    private String firstName;
    private String phone;
    private String country;
}

@Data
@ToString
public class ImportRequest<T extends ImportModel> {
    private String entityType;
    private List<T> createList;
    private List<T> updateList;
}

@RestController
@RequestMapping("/")
public class Controller {

    @RequestMapping(method = RequestMethod.POST, value = "/import")
    public void executeImport(@RequestBody ImportRequest<? extends ImportModel> request) {
        System.out.println(request.toString());
    }
}

1. 得出一个终点:

###
POST http://localhost:8080/import
Content-Type: application/json

{
  "entityType": "A",
  "createList": [
    {
      "type": "A",
      "row": 1,
      "name": "sted",
      "department": "drulp"
    },
    {
      "type": "A",
      "row": 2,
      "name": "mary",
      "department": "kko"
    }
  ],
  "updateList": [
    {
      "type": "A",
      "row": 3,
      "name": "sddf",
      "department": "fff"
    },
    {
      "type": "A",
      "row": 4,
      "name": "few",
      "department": "kko"
    }
  ]
}
# ImportRequest(entityType=A, createList=[ImportTypeAModel(row=1, name=sted, department=drulp), ImportTypeAModel(row=2, name=mary, department=kko)], updateList=[ImportTypeAModel(row=3, name=sddf, department=fff), ImportTypeAModel(row=4, name=few, department=kko)])

###
POST http://localhost:8080/import
Content-Type: application/json

{
  "entityType": "B",
  "createList": [
    {
      "type": "B",
      "firstName": "John",
      "phone": "0412345678",
      "country": "USA"
    },
    {
      "type": "B",
      "firstName": "Mary",
      "phone": "0412247474",
      "country": "CANADA"
    }
  ],
  "updateList": [
    {
      "type": "B",
      "firstName": "Tim",
      "phone": "0412345678",
      "country": "Australia"
    },
    {
      "type": "B",
      "firstName": "Luke",
      "phone": "042728282",
      "country": "CANADA"
    }
  ]
}

# ImportRequest(entityType=B, createList=[ImportTypeBModel(firstName=John, phone=0412345678, country=USA), ImportTypeBModel(firstName=Mary, phone=0412247474, country=CANADA)], updateList=[ImportTypeBModel(firstName=Tim, phone=0412345678, country=Australia), ImportTypeBModel(firstName=Luke, phone=042728282, country=CANADA)])

我对此进行了测试,并做了工作!





相关问题
Spring Properties File

Hi have this j2ee web application developed using spring framework. I have a problem with rendering mnessages in nihongo characters from the properties file. I tried converting the file to ascii using ...

Logging a global ID in multiple components

I have a system which contains multiple applications connected together using JMS and Spring Integration. Messages get sent along a chain of applications. [App A] -> [App B] -> [App C] We set a ...

Java Library Size

If I m given two Java Libraries in Jar format, 1 having no bells and whistles, and the other having lots of them that will mostly go unused.... my question is: How will the larger, mostly unused ...

How to get the Array Class for a given Class in Java?

I have a Class variable that holds a certain type and I need to get a variable that holds the corresponding array class. The best I could come up with is this: Class arrayOfFooClass = java.lang....

SQLite , Derby vs file system

I m working on a Java desktop application that reads and writes from/to different files. I think a better solution would be to replace the file system by a SQLite database. How hard is it to migrate ...

热门标签