Java http调用期间特定类的对象映射器映射

Java http调用期间特定类的对象映射器映射,java,spring,http,spring-mvc,jackson,Java,Spring,Http,Spring Mvc,Jackson,是否有一种方法可以通过注释为特定类添加ObjectMapper @JsonRootName("employee") public class Sample implements Serializable{ private String name; private String id; // Getters and setters } 在RestController中,我有RequestMapping和如下方法:- @ResponseBody public Samp

是否有一种方法可以通过注释为特定类添加ObjectMapper

@JsonRootName("employee")
public class Sample implements Serializable{
    private String name;
    private String id;
    // Getters and setters 
}
在RestController中,我有RequestMapping和如下方法:-

 @ResponseBody
 public Sample (@RequestBody Sample sample){
 //some logic
 return sample;
}
我的输入有效载荷如下

    {
      "employee":{
            "name":"abcd",
            "id":"1234"
        }
    }
我期望的输出是

{
    "name":"abcd",
    "id":"1234"
}
1) 是否有一种方法可以使用同一个类来完成输入和输出

2) 我在类的顶部添加了@JsonRootName,它需要ObjectMapper的序列化功能来包装_ROOT_值,如下所示:-

ObjectMapper mapper = new ObjectMapper();
mapper.enable(SerializationFeature.WRAP_ROOT_VALUE);
mapper.enable(DeserializationFeature.UNWRAP_ROOT_VALUE); 

可以将其添加为仅反映在此类中。

可能只保留默认的序列化行为?然后,在反序列化时,您仍然会拉出“employee”包装,但在序列化时,您将在不使用包装的情况下编写它

ObjectMapper mapper = new ObjectMapper();
//mapper.enable(SerializationFeature.WRAP_ROOT_VALUE);
mapper.enable(DeserializationFeature.UNWRAP_ROOT_VALUE); 
通过您的输入,我获得了所需的序列化输出:

{"name":"abcd","id":"1234"}
编辑

至于将这段代码放在哪里,我建议使用一个带有静态方法的单例或类来处理(反)序列化。您可以使用两个不同的映射器来执行“正常”或“包装”行为。下面是静态方法方法的概述:

public class SerializationUtil {
    private static ObjectMapper normalObjectMapper;
    private static ObjectMapper wrappedObjectMapper;

    static {
        /* configure different (de)serialization strategies */
        normalObjectMapper = new ObjectMapper();
        wrappedObjectMapper = new ObjectMapper();
        wrappedObjectMapper.enable(SerializationFeature.WRAP_ROOT_VALUE);
        wrappedObjectMapper.enable(DeserializationFeature.UNWRAP_ROOT_VALUE);
    }

    public static <T> T normalDeserialize(String json, Class<T> clazz) throws Exception {
        return normalObjectMapper.readValue(json, clazz);
    }

    public static String normalSerialize(Object bean) throws Exception {
        return normalObjectMapper.writeValueAsString(bean);
    }

    public static <T> T deserializeWrappedObject(String json, Class<T> clazz) throws Exception {
        return wrappedObjectMapper.readValue(json, clazz);
    }

    public static String serializeWrappedObject(Object bean) throws Exception {
        return wrappedObjectMapper.writeValueAsString(bean);
    }
}
如果您对此不感兴趣,您也可以检测特殊情况,并在相同的方法调用中处理它:

public static <T> T deserialize(String json, Class<T> clazz) throws Exception {
    if (clazz instanceof Bean) {
        return wrappedObjectMapper.readValue(json, clazz);
    } else {
        return normalObjectMapper.readValue(json, clazz);
    }
}
publicstatict反序列化(字符串json,类clazz)引发异常{
if(Bean的clazz实例){
返回wrappedObjectMapper.readValue(json,clazz);
}否则{
返回normalObjectMapper.readValue(json,clazz);
}
}

我的问题还询问将这个对象映射器代码放在哪里,因为如果我创建一个bean,它将应用于整个项目。但我只希望在这个示例类中反序列化,而不希望在任何其他类中反序列化。@在我更新答案之后。这就是你要找的吗?
public static <T> T deserialize(String json, Class<T> clazz) throws Exception {
    if (clazz instanceof Bean) {
        return wrappedObjectMapper.readValue(json, clazz);
    } else {
        return normalObjectMapper.readValue(json, clazz);
    }
}