Resource不能直接返回吗?

来源:5-1 SpringBoot 资源文件属性配置

SophieV5

2018-03-22 15:55

老师,Resource不能直接返回吗,为什么需要new一个新的对象,copy一份

写回答 关注

3回答

  • 開sennsei
    2018-06-13 23:30:08

    同问!



  • qq_amangirl_0
    2018-04-12 13:02:03

    老师,我直接返回Resource报错了,为什么

    @RequestMapping("/getResource")

    public JsonUtil getResource() {

    Resource bean=new Resource();

    //BeanUtils.copyProperties(resource, bean);

    //System.out.println(resource);

    //System.out.println(bean);

    return JsonUtil.ok(resource);

    }

    2018-04-12 13:01:56.862  WARN 10980 --- [nio-8090-exec-3] .w.s.m.s.DefaultHandlerExceptionResolver : Failed to write HTTP message: org.springframework.http.converter.HttpMessageNotWritableException: Could not write JSON: No serializer found for class org.springframework.context.expression.StandardBeanExpressionResolver and no properties discovered to create BeanSerializer (to avoid exception, disable SerializationFeature.FAIL_ON_EMPTY_BEANS); nested exception is com.fasterxml.jackson.databind.JsonMappingException: No serializer found for class org.springframework.context.expression.StandardBeanExpressionResolver and no properties discovered to create BeanSerializer (to avoid exception, disable SerializationFeature.FAIL_ON_EMPTY_BEANS) (through reference chain: com.imooc.util.JsonUtil["date"]->com.imooc.model.Resource$$EnhancerBySpringCGLIB$$21953e4a["$$beanFactory"]->org.springframework.beans.factory.support.DefaultListableBeanFactory["beanExpressionResolver"])

    2018-04-12 13:01:56.862  WARN 10980 --- [nio-8090-exec-3] .w.s.m.s.DefaultHandlerExceptionResolver : Resolved exception caused by Handler execution: org.springframework.http.converter.HttpMessageNotWritableException: Could not write JSON: No serializer found for class org.springframework.context.expression.StandardBeanExpressionResolver and no properties discovered to create BeanSerializer (to avoid exception, disable SerializationFeature.FAIL_ON_EMPTY_BEANS); nested exception is com.fasterxml.jackson.databind.JsonMappingException: No serializer found for class org.springframework.context.expression.StandardBeanExpressionResolver and no properties discovered to create BeanSerializer (to avoid exception, disable SerializationFeature.FAIL_ON_EMPTY_BEANS) (through reference chain: com.imooc.util.JsonUtil["date"]->com.imooc.model.Resource$$EnhancerBySpringCGLIB$$21953e4a["$$beanFactory"]->org.springframework.beans.factory.support.DefaultListableBeanFactory["beanExpressionResolver"])


    zsir94 回复開senns...

    其实不是加载顺序原因,调试可以发现,返回的json串中,该读取的属性是有值的,但是可以发现data中除了属性文件中的这些字段值,还多了一些属性,如$$beanFactory,原因应该是Resource对象是Spring构造的,Spring给这个对象加入元数据之类的其他属性,这些属性Jackson解析不了,所以报错了,可以看到异常里也提示了 找不到$$beanFactory

    2018-07-22 19:50:21

    共 2 条回复 >

  • itachy
    2018-03-22 19:12:13

    当然可以啦

SpringBoot开发常用技术整合

SpringBoot 极简开发的框架整合利器

102172 学习 · 508 问题

查看课程

相似问题