项目中目前用到SpringBoot,且控制层用到的是@RestController,
我们知道@RestController 即就是@Controller和@ResponseBody。
@ResponseBody注解会告诉Spring,我们要将返回的对象作为资源发送给客户端,并将其转换成客户端可接受的表述方式,
假设我们客户端的Accept头部信息表明它接受“application/json”,Spring会选择合适的httpMethodConverter(一般常见的是Jackson JSON)将对象转换为JSON推送到前台!!!
一般我会直接将实体对象返回到前台,但是我发现当我在实体类中自定义了一个getAbc()方法后,返回到前台的json串里面竟然包含这abc这个字段,但是我实体类并没有这个abc这个字段。
所以我就怀疑啊只有定义了getXXX方法后 Jackson才能转换成json串,我把get换成GET,geT,Get发现前台并没有这个字段,所以就更加肯定是只对get方法进行了处理,那么具体是如何处理的呢?
废话不多说:直接看代码
com.fasterxml.jackson.databind.introspect.AnnotatedMethod.BeanUtil
public static String okNameForRegularGetter(AnnotatedMethod am, String name,
boolean stdNaming)
{
if (name.startsWith("get")) {
/* 16-Feb-2009, tatu: To handle [JACKSON-53], need to block
* CGLib-provided method "getCallbacks". Not sure of exact
* safe criteria to get decent coverage without false matches;
* but for now let's assume there's no reason to use any
* such getter from CGLib.
* But let's try this approach...
*/
if ("getCallbacks".equals(name)) {
if (isCglibGetCallbacks(am)) {
return null;
}
} else if ("getMetaClass".equals(name)) {
// 30-Apr-2009, tatu: Need to suppress serialization of a cyclic reference
if (isGroovyMetaClassGetter(am)) {
return null;
}
}
return stdNaming
? stdManglePropertyName(name, 3)
: legacyManglePropertyName(name, 3);
}
return null;
}
大家直接debug跟踪即可