spring遇到的bug

一、解决No converter found for return value of type: class java.util.ArrayList的问题一、

背景

  最近闲来无事,想自己搭建一套Spring+SpringMVC+Mybatis+Mysql的环境(搭建步骤会在以后博客中给出),结果运行程序时,适用@ResponseBody注解进行返回List<对象>的json数据时出现了:nested exception is java.lang.IllegalArgumentException: No converter found for return value of type: class java.util.ArrayList错误,就细细分析了下,而后解决了该问题,先拿来备份和分享!

错误原因及解决步骤

  1.原因:这是因为springmvc默认是没有对象转换成json的转换器的,需要手动添加jackson依赖。

  2.解决步骤:

    手动添加jackson依赖到pom.xml文件中

  <properties>
    <jackson.version>2.5.4</jackson.version>
  </properties> 
<dependency> <groupId>com.fasterxml.jackson.core</groupId> <artifactId>jackson-core</artifactId> <version>${jackson.version}</version> </dependency> <dependency> <groupId>com.fasterxml.jackson.core</groupId> <artifactId>jackson-databind</artifactId> <version>${jackson.version}</version> </dependency>

  如果还是没有解决,则进行以下步骤

  在springmvc配置文件中进行如下配置

<mvc:annotation-driven>
     <mvc:message-converters>
            <bean class="org.springframework.http.converter.StringHttpMessageConverter"/>
            <bean class="org.springframework.http.converter.json.MappingJackson2HttpMessageConverter"/>
   </mvc:message-converters>
</mvc:annotation-driven>

这样我们就完美解决了该问题。

二、Spring中@Transactional无法用在接口的实现类

报错详情

 nested exception is org.springframework.beans.factory.NoSuchBeanDefinitionException: No qualifying bean of type [com.approval.base.service.TestImp] found for dependency: expected at least 1 bean which qualifies as autowire candidate for this dependency. Dependency annotations: {@org.springframework.beans.factory.annotation.Autowired(required=true)}

解决办法

第一种:controller中@AutoWired 使用接口类型 第二种:proxy-target-class设置为true,使用cgLib动态代理。 ``

相关解释

proxy-target-class=“true” 基于类的代理将起作用(cglib库)

proxy-target-class=“false” 基于接口的代理将起作用(标准的JDK,默认选项) 高版本spring自动根据运行类选择JDK或CGLIB代理

cglib动态代理和jdk动态代理的区别 JDK动态代理只能对实现了接口的类生成代理,而不能针对类。 CGLIB是针对类实现代理,主要是对指定的类生成一个子类,覆盖其中的方法,因为是继承,所以该类或者方法最好不要生命成final。

三. java.lang.IllegalStateException: No ConfigurableListableBeanFactory set

使用Dubbo调用远程服务的时候,启动日志报错:java.lang.IllegalStateException: No ConfigurableListableBeanFactory set

 通过日志可以看出是spring的初始化启动出错,原因是使用的dubbo版本和spring的版本不支持,通过修改spring版本spring 5.1.5 RELEASE 为 5.0.5 RELEASE即可解决。

之后遇到的bug还在不断更新哦...........................

posted @ 2020-11-17 19:43  xuewenのblog  阅读(217)  评论(0)    收藏  举报