当前位置: 首页 > 知识库问答 >
问题:

Java对象属性在Spring Boot Jackson的API Json响应中没有显示

濮阳旭东
2023-03-14

我有一个javadto模型类,它有一些属性。在API Controller类中,我试图使用objectMapper.redValue(..)将java对象转换为json,但它没有转换一个字段(_atType),这就是为什么在响应体中我无法看到@type json键数据的原因。

型号/D至类别:

@JsonInclude(JsonInclude.Include.NON_EMPTY)
@JsonIgnoreProperties(ignoreUnknown = true)
@Schema(description = "Abc...")
public class SomeDto {
    @JsonProperty("id")
    private String id = null;

    @JsonProperty("description")
    private String description = null;

    @JsonProperty("state")
    private TaskStateType state = null;

    @JsonProperty("@type")
    private String _atType = null;
    
    public CheckServiceQualification _atType(String _atType) {
        this._atType = _atType;
        return this;
    }
    
    public String getAtType() {
        return _atType;
    }

    public void setAtType(String _atType) {
        this._atType = _atType;
    }
    
    .... other getter setters for other attributes
}

响应json如下:

{
    "id": "55",
    "description": "Query Service Qualification POST Illustration",
    "state": "accepted"
}

响应 JSON 预期如下所示:

{
    "id": "55",
    "description": "Query Service Qualification POST Illustration",
    "state": "accepted",
    "@type": "Type1"
}

API控制器方法:

public ResponseEntity<SomeDto> createQualification(@Parameter(in = ParameterIn.DEFAULT, description = "The QueryServiceQualification to be created", required = true, schema = @Schema()) @Valid @RequestBody QueryServiceQualificationCreate body) {
        logger.info("Received create QueryServiceQualification request.");
        String responseJson = "{ \"id\": \"55\", \"description\": \"Query Service Qualification POST Illustration\", \"state\": \"accepted\", \"_atType\": \"Type1\"}";
        SomeDto someDto = null;
        try {
            someDto = objectMapper.readValue(responseJson, SomeDto.class);
            return ResponseEntity.status(HttpStatus.CREATED).body(someDto);
        } catch (JsonProcessingException e) {
            logger.error("Could not able to convert json string to object", e);
        }
        return new ResponseEntity<SomeDto>(HttpStatus.NOT_IMPLEMENTED);
    }

因此,尽管API控制器方法(上面一个)中的响应Json变量有“_atType”:“Type1”,但在Postman中的API json响应中,我看不到属性“@type”:“Type1”。请帮助。

共有1个答案

厍书
2023-03-14

在您提供的示例中,字段名以json格式提供为“_atType”:

"_atType" : "Type1"

但是,它在模型对象上被指定为“@type”:

@JsonProperty("@type")
private String _atType = null;

您需要在json数据中包含“@type”字段或在模型对象上删除此部分:@Json属性(“@type”)

 类似资料:
  • 这是我的密码: 这条线给了我错误 "属性错误:'浮点'对象没有属性'exp'"。X,t是Numpy ndarray。

  • 问题内容: 我正在开发Django应用程序,并且出现以下错误 我的模型是这样构造的 我应该做什么? 问题答案: 首先,您必须非常小心地重写以具有非可选参数。记住,每次从一个查询集中获取一个对象时,它将被调用! 这是您想要的正确代码: 如果您只使用该对象的子类,我强烈建议在Animal上设置abstract选项。这样可以确保不为动物创建表,而仅为绵羊(等)创建表。如果未设置abstract,则将创建

  • 我创建了这个简单的GUI: 我让用户界面启动并运行。当我点击按钮时,我在控制台上得到以下错误: 为什么设置为?

  • 问题内容: 我在python 3.3.4中遇到“解码”方法的问题。这是我的代码: 但是我无法解码此问题的代码: 你有什么想法?谢谢 问题答案: 一种 编码 字符串,另一种 解码 字节。 您应该从文件中读取字节并对其进行解码: 幸运的是,有一个编码参数使操作变得简单:

  • 问题内容: 我将Selenium webdriver(chrome)与Python结合使用,试图从网页上的所有链接中获取 href 。当我尝试以下操作时: 它设法获取所有链接,但是在get_attribute上出现错误: “ WebElement”对象没有属性“ Get_Attribute” 尽管到处都看起来很正常。 问题答案: “ Get_Attribute”属性不存在,但是“ get_attr

  • 问题内容: 我创建了这个简单的GUI: 我启动并运行了UI。当我单击Grab按钮时,在控制台上出现以下错误: 为什么entryBox设置为None? 问题答案: 并且place在功能Entry对象和所有其他部件的回报None。在python中,执行此操作时.,表达式的结果为返回的值,因此。 你应该将其分成两行,如下所示: 这样,你就可以将参考存储在其中,并且可以按照你的期望进行布局。如果你以块的形

  • 问题内容: 它显示了运行时错误: 我只是python的初学者,即使在网上搜索后也无法纠正此问题。 问题答案: 错误: 表示您正在尝试将索引运算符应用于int而不是列表。所以即使在应该的时候也没有清单吗?让我们从那开始。 看这里: 在内部使用其他变量名称,看起来列表理解会在迭代过程中覆盖该变量。(不是在设置时 进行 迭代,而是在以下过程中进行。)

  • 问题内容: 我在这里遇到一些问题,在我的python包中,我已经安装了numpy,但是我仍然 遇到 此错误 ‘DataFrame’对象没有属性’sort’ 任何人都可以给我一些想法。 这是我的代码: 问题答案: 不推荐使用DataFrames,而采用以下两种方法之一: 到 由列排序(S) 要 通过索引排序 在Pandas中已弃用(但仍可用)版本0.17(2015-10-09),并引入和。它已从0.