Spring在控制器级别添加Json Root Name

时间:2016-03-07 17:02:00

标签: json spring

有没有办法在@controller级别添加根元素,而不是在应用程序级别。我需要采用以下格式的回复

{"BaseResponse":{"HeaderResponse":{"prop1":"test","prop2":"1"},"DetailResponse":[{"recordDesc":"record1","recordId":1},{"recordDesc":"record1","recordId":1}]}}

但我没有得到" BaseResponse"在根

{"HeaderResponse":{"prop1":"test","prop2":"1"},"DetailResponse":[{"recordDesc":"record1","recordId":1},{"recordDesc":"record1","recordId":1}]}

我不想在应用程序级别配置WRAP_ROOT_VALUE,如下所示。

 public class SpringMVCJacksonObjectMapper extends ObjectMapper  
 {
        public SpringMVCJacksonObjectMapper() 
        {
            super();
            this.configure(DeserializationFeature.UNWRAP_ROOT_VALUE, true);
            this.configure(SerializationFeature.WRAP_ROOT_VALUE, true);
        }
  }

1 个答案:

答案 0 :(得分:0)

在控制器级别创建条件ObjectMapper解决了这个问题。

here is the solution