PropertyNamingStrategy.KEBAB_CASE
package com.llq;
import com.fasterxml.jackson.core.JsonProcessingException;
import com.fasterxml.jackson.databind.ObjectMapper;
import com.fasterxml.jackson.databind.PropertyNamingStrategy;
import java.util.HashMap;
class TestClass {
public static void main(String[] args) throws JsonProcessingException {
ObjectMapper mapper = new ObjectMapper();
mapper.setPropertyNamingStrategy(PropertyNamingStrategy.KEBAB_CASE);
HashMap<Object, Object> map = new HashMap<>();
map.put("data-key", "123");
String value = mapper.writerWithDefaultPrettyPrinter().writeValueAsString(map);
A a = mapper.readValue(value, A.class);
System.out.println(a);
}
}
class A {
private String dataKey;
public String getDataKey() {
return dataKey;
}
public void setDataKey(String dataKey) {
this.dataKey = dataKey;
}
@Override
public String toString() {
return "A{" +
"dataKey='" + dataKey + '\'' +
'}';
}
}
运行结果:
PropertyNamingStrategy.SNAKE_CASE
package com.llq;
import com.fasterxml.jackson.core.JsonProcessingException;
import com.fasterxml.jackson.databind.ObjectMapper;
import com.fasterxml.jackson.databind.PropertyNamingStrategy;
import java.util.HashMap;
class TestClass {
public static void main(String[] args) throws JsonProcessingException {
ObjectMapper mapper = new ObjectMapper();
mapper.setPropertyNamingStrategy(PropertyNamingStrategy.SNAKE_CASE);
HashMap<Object, Object> map = new HashMap<>();
map.put("data_key", "123");
String value = mapper.writerWithDefaultPrettyPrinter().writeValueAsString(map);
A a = mapper.readValue(value, A.class);
System.out.println(a);
}
}
class A {
private String dataKey;
public String getDataKey() {
return dataKey;
}
public void setDataKey(String dataKey) {
this.dataKey = dataKey;
}
@Override
public String toString() {
return "A{" +
"dataKey='" + dataKey + '\'' +
'}';
}
}
运行结果: