当内容为Application/JSON时,Spring不会反序列化Object



我试图在spring中创建一个端点,在那里我发送了原始JSON,它不会反序列化到对象,但如果我使用表单Data,它可以正常工作。我只是想知道我做错了什么?

这是目标

@Document("Boats")
@NoArgsConstructor
@AllArgsConstructor
@Getter
@Setter
public class Boat{
@Id
private String id;
private Boolean online;
private String boatName;
private String lastOnline;
private String password;
@JsonIgnore
@Transient
private java.net.InetAddress inetAddress;
private String displayAddress;
private String displayStatus;
public Boat(String boatName, InetAddress inetAddress,Boolean online) {
this.online = online;
this.boatName = boatName;
this.inetAddress = inetAddress;
this.displayAddress = inetAddress.getHostAddress();
if(online){
String pattern = "E, dd MMM yyyy HH:mm:ss z";
SimpleDateFormat simpleDateFormat = new SimpleDateFormat(pattern);
simpleDateFormat.setTimeZone(TimeZone.getTimeZone("CST"));
this.lastOnline = simpleDateFormat.format(new Date());
}
updateDisplayStatus();
}
public void updateDisplayStatus(){
if(getOnline()){
setDisplayStatus(BoatStatus.ONLINE.getDisplayText());
}else{
setDisplayStatus(BoatStatus.OFFLINE.getDisplayText());
}
}
}

这是终点

@PostMapping(path= "/boat/update")
public ResponseEntity updateBoat(@RequestBody Boat boat){
boatFacade.updateBoat(boat);
return ResponseEntity.ok().build();
}

这是请求的正文

{
"id":"62ca4f7c5a3e0f1411445065",
"online":"false",
"boatName":"dfsjkdfsd",
"lastOnline":"null",
"password":"null",
"displayAddress":"10.50.50.50",
"displayStatus":"ONLINE"
}

这些是我正在使用的POM依赖项,不确定是否缺少依赖

<dependencies>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-data-mongodb</artifactId>
</dependency>
<dependency>
<groupId>io.springfox</groupId>
<artifactId>springfox-swagger2</artifactId>
<version>3.0.0</version>
</dependency>
<dependency>
<groupId>io.springfox</groupId>
<artifactId>springfox-swagger-ui</artifactId>
<version>2.9.2</version>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web-services</artifactId>
</dependency>
<dependency>
<groupId>com.google.guava</groupId>
<artifactId>guava</artifactId>
<version>31.1-jre</version>
</dependency>
<dependency>
<groupId>org.apache.poi</groupId>
<artifactId>poi-ooxml</artifactId>
<version>5.2.2</version>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-devtools</artifactId>
<scope>runtime</scope>
<optional>true</optional>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-test</artifactId>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.projectlombok</groupId>
<artifactId>lombok</artifactId>
</dependency>
</dependencies>

始终建议指定端点使用/生成的数据的类型/种类。

@PostMapping(value = "/boat/update", consumes = MediaType.APPLICATION_JSON_VALUE, produces = MediaType.APPLICATION_JSON_VALUE)

对象保留-调试点

邮差设置

相关内容

最新更新