java Jackson无法序列化属性

qmelpv7a  于 2023-04-04  发布在  Java
关注(0)|答案(1)|浏览(167)

在我的SpringBoot应用程序中,我用以下方式定义了一个Controller:

@RestController
@RequestMapping("/api/book")
@Slf4j
public class BookController {

    @PostMapping("/create")
    public ResponseEntity<String> createBook(@RequestBody BookCreationPayload payload) {

        log.info(payload.getName());
        log.info(payload.getAuthor());
        log.info(payload.getEBookReference());

        return ResponseEntity.ok().body("Created successfully!");
    }
}

相应的有效载荷定义如下:

@Data
public class BookCreationPayload {

    private String name;
    private String author;
    private String eBookReference;
}

当我用下面的requestbody访问端点时,eBookReference属性为null。

{
    "name":"Kafka on the Shore",
    "author": "Haruki Murakami",
    "eBookReference": "https://www.pdfdrive.com/kafka-on-the-shore-e191718472.html"
}

现在,如果我将属性名更改为electronicBookReference,它工作得很好。我认为Jackson由于一些命名约定问题而无法序列化它,但我不知道到底发生了什么。
这是我的pom.xml。

<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
         xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 https://maven.apache.org/xsd/maven-4.0.0.xsd">
    <modelVersion>4.0.0</modelVersion>
    <parent>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-parent</artifactId>
        <version>3.0.5</version>
        <relativePath/> <!-- lookup parent from repository -->
    </parent>
    <groupId>com.example</groupId>
    <artifactId>sandbox</artifactId>
    <version>0.0.1-SNAPSHOT</version>
    <name>sandbox</name>
    <description>sandbox</description>
    <properties>
        <java.version>17</java.version>
    </properties>
    <dependencies>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-web</artifactId>
        </dependency>

        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-devtools</artifactId>
            <scope>runtime</scope>
            <optional>true</optional>
        </dependency>
        <dependency>
            <groupId>org.projectlombok</groupId>
            <artifactId>lombok</artifactId>
            <optional>true</optional>
        </dependency>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-test</artifactId>
            <scope>test</scope>
        </dependency>
    </dependencies>

    <build>
        <plugins>
            <plugin>
                <groupId>org.springframework.boot</groupId>
                <artifactId>spring-boot-maven-plugin</artifactId>
                <configuration>
                    <excludes>
                        <exclude>
                            <groupId>org.projectlombok</groupId>
                            <artifactId>lombok</artifactId>
                        </exclude>
                    </excludes>
                </configuration>
            </plugin>
        </plugins>
    </build>

</project>
jei2mxaa

jei2mxaa1#

正如@xerx593在评论中提到的,问题是关于命名约定和由Lombok生成的Getter Setter方法,如果你不想使用@JsonProperty @JsonCreator,解决方案是手动创建Getter & Setter方法,如下所示。

public String geteBookReference() {
    return eBookReference;
}

public void seteBookReference(String eBookReference) {
    this.eBookReference = eBookReference;
}

这与上面提到的请求有效负载一起工作,因为Jackson使用类getters和setters方法进行序列化和反序列化。这些方法中的“get”,“is”和“set”后面的内容将用作JSON字段的键。

相关问题