我通过扩展为您的问题找到了可能的解决方案MappingJackson2HttpMessageConverter
public class MyMappingJackson2HttpMessageConverter extends MappingJackson2HttpMessageConverter {
@Override
protected void writeInternal(Object object, HttpOutputMessage outputMessage) throws IOException, HttpMessageNotWritableException {
//outputMessage.getHeaders().add("code", DigestUtils.md5Hex(object.toString()));
Map<String, Object> map = new HashMap<String, Object>();
map.put("status", 200);
map.put("data", object);
super.writeInternal(map, outputMessage); //To change body of overridden methods use File | Settings | File Templates.
}
}
然后将你自己的 conventer bean 添加到 spring-mvc 配置文件中
<mvc:annotation-driven>
<mvc:message-converters register-defaults="true">
<bean class="com.xxx.utils.MyMappingJackson2HttpMessageConverter">
</bean>
</mvc:message-converters>
</mvc:annotation-driven>
<mvc:default-servlet-handler/>
此方法适用于每个@ResponseBody
处理程序方法。
请注意,它会更改默认的 Spring 动作,您应该考虑一下。