我有一个 Controller ,它接受 json 格式的输入请求
{
"default": true
}
默认是java关键字,所以我将其设置为
@JsonProperty(“default”)
Private boolean isDefault;
我有一个客户,我需要调用仅接受 default
的电话这是 boolean 值。
当我按照上述方式发送请求时,它不会发送为 default: true
它发送为 isDefault:true
对我来说,给客户的电话失败了。说isDefault
不存在。
有人可以帮忙吗?
同样,我再次收到客户端的默认响应,其值与我发送的值相同。我如何接受系统中的响应?我尝试使用 JsonProperty 但没有成功。还有 gson 的@SerializedName。有人可以帮忙吗?
最佳答案
我已经创建了示例项目。这是我得到的。
Pojo 类:
public class Pojo {
@JsonProperty("default")
private boolean isDefault;
public boolean isDefault() {
return isDefault;
}
public void setDefault(boolean aDefault) {
isDefault = aDefault;
}
}
Controller :
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RestController;
@RestController
public class TestController {
@GetMapping("/default")
public Pojo test(){
Pojo p = new Pojo();
p.setDefault(true);
return p;
}
}
pom 依赖项:
<dependencies>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-test</artifactId>
<scope>test</scope>
<exclusions>
<exclusion>
<groupId>org.junit.vintage</groupId>
<artifactId>junit-vintage-engine</artifactId>
</exclusion>
</exclusions>
</dependency>
</dependencies>
输出:
{"default":true}
关于java - @@JsonProperty 注释不适用于 java 属性,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/59778485/