如何使用Spring Boot在键中使用连字符序列化JSON



我有以下JSON。。。

{
"navigation-items":[
{
"title":"Title",
"description":"This is the desc",
"link":"http://google.com"
}
]
}

我有以下Java

public class ExternalConfig {
@SerializedName("navigation-items")
private List<NavigationItem> items;
public NavigationItem getNavItem(Integer index){
return items.get(index);
}
public void putNavItem(NavigationItem item){
items.add(item);
}
public Integer navItemSize(){
return items == null ? 0 : items.size();
}
}
...
@PostMapping("config")
@ResponseBody
public void setConfig(@RequestBody ExternalConfig config){
System.out.println(config.navItemSize() > 0);
}

但当我向以下机构发送请求时。。。。

{
"navigation-items":[
{
"title":"Title",
"description":"This is the desc",
"link":"http://google.com"
},{
"title":"Test 2",
"description":"asdasdsadsadas",
"link":"http://drudgereport.com"
}
]
}

但当我插入端点时,项为null。如何将带有连字符键的JSON传递给Spring Boot?

如果使用弹簧引导,请使用@JsonProperty。

@JsonProperty("navigation-items")
private List<NavigationItem> items;

这里真正的问题是我没有像这样包含jackson数据绑定。。。

compile 'com.fasterxml.jackson.core:jackson-databind:latest.release'

不确定是否有办法使用GSON,因为我已经有了,但现在我只会使用杰克逊。

最新更新