我们正试图从SOAP转移到REST,我们在这里偶然发现了这个问题,聚会可以是个人或组织的类型。
示例XMLs
<customer>
<details>
<party xsi:type="Individual">
<externalID>ABC123</externalID>
<firstname>John</firstname>
<lastname>Smith</lastname>
</party>
</details>
</customer>
<customer>
<details>
<party xsi:type="Organization">
<externalID>APPLE</externalID>
<organizationName>Apple Inc</organizationName>
<listingName>APPLE</listingName>
</party>
</details>
</customer>但是,当我们转移到JSON表示时,我们会遇到继承信息丢失的问题
JSON示例
{
"customer": {
"details": {
"party": {
"externalID": "ABC123",
"firstname": "John",
"lastname": "Smith"
}
}
}
}
{
"customer": {
"details": {
"party": {
"externalID": "APPLE",
"organizationName": "Apple Inc",
"listingName": "APPLE"
}
}
}
}因此,当我们使用像Gson这样的库将JSON转换回Java对象时,我们丢失了个人或组织的定义。
虽然解决方法之一是构建额外的服务来检索返回具体类型(个人或组织)的“详细信息”,但在JSON中是否有其他方法来处理此问题?
发布于 2017-10-20 05:55:41
https://spacetelescope.github.io/understanding-json-schema/reference/combining.html
在OpenAPI 3.0中,通过添加oneOf关键字增强了这种支持。
您的继承可以使用oneOf (用于选择一个子项)和allOf (用于组合父项和子项)的组合进行建模。
paths:
/customers:
post:
requestBody:
content:
application/json:
schema:
oneOf:
- $ref: '#/components/schemas/Individual
- $ref: '#/components/schemas/Organization
discriminator:
propertyName: customer_type
responses:
'201':
description: Created
components:
schemas:
Customer:
type: object
required:
- customer_type
- externalID
properties:
customer_type:
type: string
externalID:
type: integer
discriminator:
property_name: customer_type
Individual:
allOf:
- $ref: "#/components/schemas/Customer"
- type: object
- properties:
firtName
type: string
lastName
type: string
Organisation:
allOf:
- $ref: "#/components/schemas/Customer"
- type: object
- properties:
organisationName
type: string
listingName
type: stringhttps://github.com/OAI/OpenAPI-Specification/blob/master/versions/3.0.0.md#schemaComposition
18/02/2020编辑:
为了使用Jackson生成Java代码,PR #5120似乎已经修复了OpenAPITools
发布于 2016-07-28 12:13:12
您可以编写一个自定义的Gson反序列化程序,并基于organisationName字段的存在,在此反序列化程序中创建组织或个人类的实例。类似于这里指定的内容:Gson - deserialization to specific object type based on field value,您可以检查您的属性是否存在,而不是“类型”。
发布于 2016-07-28 12:24:30
我发现了IBM提交的资源,我觉得它可能会有一些帮助。它被链接在下面。
在pg上。27他们转换成:
<year xsi:type="xs:positiveInteger">1989</year>至:
{ "xsi:type" : "xs:positiveInteger", value : 1989 }因此,我认为您的代码应该转换为:
{
"customer": {
"details": {
"party": {
"xsi:type": "Individual",
"externalID": "ABC123",
"firstname": "John",
"lastname": "Smith"
}
}
}
}
{
"customer": {
"details": {
"party": {
"xsi:type": "Organization",
"externalID": "APPLE",
"organizationName": "Apple Inc",
"listingName": "APPLE"
}
}
}
}参考资源:https://www.w3.org/2011/10/integration-workshop/s/ExperienceswithJSONandXMLTransformations.v08.pdf
https://stackoverflow.com/questions/38626716
复制相似问题