我有一个班级:
package my.package;
public class TreeItem{
...
}
当我想序列化像TreeItem.class
这样的类文字时,杰克逊将其序列化为:
"class my.package.TreeItem"
我想将其序列化为
"my.package.TreeItem"
编辑:
public class TreeConfigMap extends HashMap<Class<? extends TreeItem>, Map<String, Object>>{
//empty
}
TreeController :(休息控制器)
@RequestMapping("/config")
public TreeConfigMap config(...){
TreeConfigMap config= new TreeConfigMap();
Map<String,Object> m = new HashMap<>();
m.put("ali","gholi");
config.put(TreeItem.class,m);
return config;
}
输出结果为:
{"class my.package.TreeItem":{"ali":"gholi"}}
答案 0 :(得分:1)
您可以使用自定义密钥序列化程序。
注释TreeConfigMap以使用自定义密钥序列化程序
public static class ClassNameSerializer extends JsonSerializer<Class> {
@Override
public void serialize(Class value, JsonGenerator jgen, SerializerProvider provider) throws IOException, JsonProcessingException {
jgen.writeFieldName(value.getCanonicalName());
}
}
或者,您可以使用ObjectMapper注册序列化程序
public static String readin()
{
Boolean gettingNumber = true;
String stringin = null;
while (gettingNumber)
{
stringin = JOptionPane.showInputDialog(null, "Please enter a number");
try
{
Integer number = Integer.parseInt(stringin);
gettingNumber = false;
}
catch (NumberFormatException exception)
{
// no need to do anything
}
}
System.out.println("returning [" + stringin + "]");
return(stringin);
}
这是序列化代码
bind_param('s', $string)