Java 还使用了如何将空值传递给pojo属性,以便使用Jackson和Spring AOP转换为Json
我试图在春季使用jacksonjar返回一个json。但我在转换相同内容时出错。这个错误与SpringAOP有关,因为当我删除它时,代码工作正常。请查看以下pojo详细信息:-Java 还使用了如何将空值传递给pojo属性,以便使用Jackson和Spring AOP转换为Json,java,spring,jackson,spring-aop,Java,Spring,Jackson,Spring Aop,我试图在春季使用jacksonjar返回一个json。但我在转换相同内容时出错。这个错误与SpringAOP有关,因为当我删除它时,代码工作正常。请查看以下pojo详细信息:- //@JsonInclude(JsonInclude.Include.NON_NULL) @JsonInclude(content=JsonInclude.Include.NON_EMPTY) public class UserBean { private String result; private
//@JsonInclude(JsonInclude.Include.NON_NULL)
@JsonInclude(content=JsonInclude.Include.NON_EMPTY)
public class UserBean {
private String result;
private String tID;
//getters and setters
}
我已经在类级别尝试了上述两种注释
在上面的代码中,我只在TID字段中设置值,结果字段的默认值为null。这个java类中还有一个ToString方法
控制器类:-
@Autowired
public UserBean userBean;
userBean.settID("T123456");
xml->
<dependency>
<groupId>com.fasterxml.jackson.core</groupId>
<artifactId>jackson-core</artifactId>
<version>2.7.3</version>
</dependency>
<dependency>
<groupId>com.fasterxml.jackson.core</groupId>
<artifactId>jackson-databind</artifactId>
<version>2.5.4</version>
</dependency>
<dependency>
<groupId>com.fasterxml.jackson.core</groupId>
<artifactId>jackson-annotations</artifactId>
<version>2.7.3</version>
</dependency>
这就是我得到的错误->
Could not write content:
No serializer found for class org.springframework.aop.TrueClassFilter
and no properties discovered to create BeanSerializer
(to avoid exception, disable SerializationFeature.FAIL_ON_EMPTY_BEANS) )
(through reference chain: com.fca.c2pc.bean.UserBean$$EnhancerBySpringCGLIB$$c56dc6af
["advisors"]->org.springframework.aop.interceptor.["pointcut"]->
org.springframework.aop.TruePointcut["classFilter"]);
nested exception is com.fasterxml.jackson.databind.JsonMappingException:
No serializer found for class org.springframework.aop.TrueClassFilter and
no properties discovered to create BeanSerializer
(to avoid exception, disable SerializationFeature.FAIL_ON_EMPTY_BEANS) )
(through reference chain: com.fca.c2pc.bean.UserBean$$EnhancerBySpringCGLIB$$c56dc6af
["advisors"]->org.springframework.aop.interceptor.["pointcut"]->
org.springframework.aop.TruePointcut["classFilter"])
为什么你的
UserBean
甚至是一个spring管理的bean?那么它应该是什么呢??除了我得到的例外,这又有什么关系呢?只有Springbean应用了AOP,如果它不是Springbean,它就不会用AOP来丰富,因此应该可以消除您的错误。不是所有的东西都必须是春豆…谢谢你的回复。。但例如,我的UserBean类有一个其他类或服务的引用,需要向spring容器注册。那么,为了使上述代码正常工作,应该在代码中进行哪些更改。为什么要将应用程序bean公开为JSON,您应该公开资源……为什么您的UserBean
甚至是spring管理的bean?那么应该是什么呢??除了我得到的例外,这又有什么关系呢?只有Springbean应用了AOP,如果它不是Springbean,它就不会用AOP来丰富,因此应该可以消除您的错误。不是所有的东西都必须是春豆…谢谢你的回复。。但例如,我的UserBean类有一个其他类或服务的引用,需要向spring容器注册。那么,为了使上述代码正常工作,应该在代码中做哪些更改呢。。。
public class LogginAspect {
private static final Logger logger = Logger.getLogger(LogginAspect.class);
@After("execution(* com.fca.c2pc..*.*(..))")
public void logAfter(JoinPoint joinPoint) {
logger.info("After executing method : " + joinPoint.getSignature().getName());
logger.info("***************************************************************************");
}
@Before("execution(* com.fca.c2pc..*.*(..))")
public void logBefore(JoinPoint joinPoint) {
logger.info("***************************************************************************");
logger.info("Before executing method : " + joinPoint.getSignature().getName());
}
@Around("execution(* com.fca.c2pc..*.*(..))")
public Object logAround(ProceedingJoinPoint pjp) throws Throwable {
long start = System.currentTimeMillis();
Object clazz = pjp.getTarget().getClass().getName();
String methodName = pjp.getSignature().getName();
logger.info("Entering Class " + clazz + " With Method Name " + methodName);
Object[] obj = pjp.getArgs();
int i = 0;
try {
for (Object o : obj) {
logger.info(++i + " : Parameter Name :" + (null != o ? o.toString() : ""));
}
} catch (Exception e) {
e.printStackTrace();
}
Object output = pjp.proceed(pjp.getArgs());
logger.info("Excecution Completed for method : " + methodName + " in Class : " + clazz + " with result "
+ output);
long elapsedTime = System.currentTimeMillis() - start;
logger.info("Execution time for method : " + methodName + " in Class : " + clazz + " : " + elapsedTime
+ " milliseconds.");
return output;
}
@AfterThrowing(pointcut = "execution(* com.fca.c2pc..*.*(..))", throwing = "error")
public void logAfterThrowing(JoinPoint joinPoint, Throwable error) {
logger.info("Exception thrown by method : " + joinPoint.getSignature().getName());
logger.info("Exception name : " + error);
}
@AfterReturning(pointcut = "execution(* com.fca.c2pc..*.*(..))", returning = "result")
public void logAfterReturning(JoinPoint joinPoint, Object result) {
logger.info("Method : " + joinPoint.getSignature().getName() + " returned value is : " + result);
}
Could not write content:
No serializer found for class org.springframework.aop.TrueClassFilter
and no properties discovered to create BeanSerializer
(to avoid exception, disable SerializationFeature.FAIL_ON_EMPTY_BEANS) )
(through reference chain: com.fca.c2pc.bean.UserBean$$EnhancerBySpringCGLIB$$c56dc6af
["advisors"]->org.springframework.aop.interceptor.["pointcut"]->
org.springframework.aop.TruePointcut["classFilter"]);
nested exception is com.fasterxml.jackson.databind.JsonMappingException:
No serializer found for class org.springframework.aop.TrueClassFilter and
no properties discovered to create BeanSerializer
(to avoid exception, disable SerializationFeature.FAIL_ON_EMPTY_BEANS) )
(through reference chain: com.fca.c2pc.bean.UserBean$$EnhancerBySpringCGLIB$$c56dc6af
["advisors"]->org.springframework.aop.interceptor.["pointcut"]->
org.springframework.aop.TruePointcut["classFilter"])