首页 > 技术文章 > springboot踩坑出坑记

cxylff 2019-06-03 18:42 原文

4月15到4月17我都在把毕设从eclipse重构到IDEA中,springboot最让我头疼的是它的版本问题,因为每一个版本对应的依赖包都有可能出错,这里分享一下如何成功移植用eclipse写的springboot到IDEA中,比较简单的步骤我这里不详细说了,说一下我遇到的一些很难找出问题的地方
ps:只是针对于我的项目和我个人水平,大神勿喷嘿嘿

springboot-mybatis整合坑

  • 出现下方错误请查看启动类:XXXApplication 是否扫描到mapper映射文件,声明eclipse和idea不一样,这里eclipse可以跑通,idea中不行
        ***************************
        APPLICATION FAILED TO START
        ***************************

        Description:

        Field chapterDao in cn.yixue.service.ChapterServiceImp required a bean of type 'cn.yixue.dao.ChapterMapper' that could not be found.

        The injection point has the following annotations:
            - @org.springframework.beans.factory.annotation.Autowired(required=true)


        Action:

        Consider defining a bean of type 'cn.yixue.dao.ChapterMapper' in your configuration.


        以上提取出有用的信息:required a bean of type 'xxxx' that could not be found.
        
        代表bean没注入,从bean注入寻找方向,有的人会说我用@Autowired之类的种种,但没扫到,好吧~

解决方法:

  1. 在相应的mapper类中加@Mapper标注让springboot根据标注去将mapper注入
@Mapper
public interface ChapterMapper {
    ......
}
  1. 启动类加@MapperScan(value = "cn.yixue.video.dao") value 后的包一定要对应到mapper类对应的地方,比如我的mapper在dao下,就是cn.yixue.video.dao
@SpringBootApplication
@MapperScan(value = "cn.yixue.video.dao")
@EnableDiscoveryClient
public class YixueVideoApplication {

    public static void main(String[] args) {
        SpringApplication.run(YixueVideoApplication.class, args);
    }

}
  1. Spring Boot项目中含有Mybatis,打Jar包运行之后,报如下错误:
***************************
APPLICATION FAILED TO START
***************************

Description:

Failed to configure a DataSource: 'url' attribute is not specified and no embedded datasource could be configured.

Reason: Failed to determine a suitable driver class


Action:

Consider the following:
    If you want an embedded database (H2, HSQL or Derby), please put it on the classpath.
    If you have database settings to be loaded from a particular profile you may need to activate it (no profiles are currently active).

网上好多解决方案,针对于每个人都不一样,我的应该是打包的时候读不到我的配置文件,需要在pom.xml里面加resourses指定下配置文件,因为eclipse是识别的,Idea可能不会?我也不太知道,反正是加上了,因为好像有Idea读不到我的application.properties或者application.yml文件,我就一次性都配上了,这个大家具体遇到的时候再去搜一下就行,不用刻意的记:

<build>
<!-- 如果不添加此节点mybatis的mapper.xml文件都会被漏掉。 -->
<resources>
    <resource>
        <directory>src/main/java</directory>
        <includes>
            <include>**/*.yml</include>
            <include>**/*.properties</include>
            <include>**/*.xml</include>
        </includes>
        <filtering>false</filtering>
    </resource>
    <resource>
        <directory>src/main/resources</directory>
        <includes>
            <include>**/*.yml</include>
            <include>**/*.properties</include>
            <include>**/*.xml</include>
        </includes>
        <filtering>false</filtering>
    </resource>
</resources>
</build>

springBoot-SpringCloud整合坑

  • 利用SpringCloud做服务注册时,Eclipse需要自己导jar包依赖和配置版本,Idea直接可以再创建Springboot项目时鼠标点击引入,这个我就放几张图来解释:



最后一个next后直接finish......

之后再pom.xml里面会看到Idea自动为你引入的依赖和 spring-boot-maven-plugin 插件,插件版本我建议还是稍微低一点,因为boot真的是随着版本变动改动很大,我用的是

<plugin>
    <groupId>org.springframework.boot</groupId>
    <artifactId>spring-boot-maven-plugin</artifactId>
    <version>1.5.9.RELEASE</version>
</plugin>

这个也是在网上搜了很久之后找到的一个版本,还有一个1.4.9.RELEASE也可以,之后就是看看Idea导入的SpringCloud依赖的版本version,版本错误很容易报java.lang.AbstractMethodError: null这个错误我找了很久,原因也是看了一个大佬的博客找到的,具体就是因为Idea给你的依赖是根据你选择的springboot的版本来的,一般人不会去修改,这也就是为什么eclipse不容易报错,Idea容易的原因,因为eclipse得自己找...

<parent>
    <groupId>org.springframework.boot</groupId>
    <artifactId>spring-boot-starter-parent</artifactId>
    <version>2.0.5.RELEASE</version>
    <relativePath/> <!-- lookup parent from repository -->
</parent>

我的parent的版本是<version>2.0.5.RELEASE</version>,大佬的文章也提到了2.1.0和2.1.0以下的有区别,我还是建议用低的,低的差别不会太大,还挺稳......我还用过1.5.9.RELEASE...

  • 之后配置eureka的服务的时候Idea提供的版本也要改,这个原因是因为如果使用${spring-cloud.version}的话,当版本号下调到2.1.0以下的时候,一些组件的包还是2.1.0它不会跟随parent版本的下调而下调,也就是parent的版本小于组件的版本,这时候就会出问题
    当改为Finchley.RELEASE的时候,组件的依赖就会跟随parent的版本下调而下调
<properties>
    <java.version>1.8</java.version>
    <!-- 这是Idea给设的 -->
    <!--<spring-cloud.version>Greenwich.SR1</spring-cloud.version>-->
    <spring-cloud.version>Finchley.RELEASE</spring-cloud.version>
</properties>

<dependencyManagement>
    <dependencies>
        <dependency>
            <groupId>org.springframework.cloud</groupId>
            <artifactId>spring-cloud-dependencies</artifactId>
            <version>${spring-cloud.version}</version>
            <type>pom</type>
            <scope>import</scope>
        </dependency>
    </dependencies>
</dependencyManagement>

解决静态资源跨域时的坑

  • 之前在eclipse中静态资源访问是可以通过
@Autowired
private RestTemplate restTemplate;

直接装配的,之后到了Idea中报错了:

ERROR 31473 --- [           main] o.s.b.d.LoggingFailureAnalysisReporter   : 

***************************
APPLICATION FAILED TO START
***************************

Description:

Field restTemplate in 'xxxxxx'
required a bean of type 'org.springframework.web.client.RestTemplate' that could not be found.


Action:

Consider defining a bean of type 'org.springframework.web.client.RestTemplate' in your configuration.

解决方法如下,大致就是先靠@Bean装配,再用...:

@Bean
public RestTemplate restTemplate(RestTemplateBuilder builder) {
   // Do any additional configuration here
   return builder.build();
}
@Autowired
private RestTemplate restTemplate;

之后就不报错了(针对于我的错误)

我的 pom.xml(project的xml)

我的架构

圈住的地方是下方的pom.xml文件

<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0"
         xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
         xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
    <modelVersion>4.0.0</modelVersion>
    <groupId>cn.yixue</groupId>
    <artifactId>yixue</artifactId>
    <version>1.0-SNAPSHOT</version>
    <packaging>pom</packaging>

    <parent>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-parent</artifactId>
        <version>2.0.5.RELEASE</version>
        <relativePath/> <!-- lookup parent from repository -->
    </parent>

    <properties>
        <java.version>1.8</java.version>
        <lombok.version>1.14.8</lombok.version>
        <fastjson.version>1.2.31</fastjson.version>
    </properties>

    <dependencies>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter</artifactId>
        </dependency>
        <dependency>
            <groupId>org.mybatis.spring.boot</groupId>
            <artifactId>mybatis-spring-boot-starter</artifactId>
            <version>1.3.2</version>
        </dependency>
        <dependency>
            <groupId>mysql</groupId>
            <artifactId>mysql-connector-java</artifactId>
            <version>5.1.46</version>
        </dependency>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-jdbc</artifactId>
        </dependency>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-devtools</artifactId>
            <optional>true</optional>
        </dependency>
        <dependency>
            <groupId>com.github.pagehelper</groupId>
            <artifactId>pagehelper-spring-boot-starter</artifactId>
            <version>1.2.5</version>
        </dependency>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-data-redis</artifactId>
        </dependency>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-test</artifactId>
            <scope>test</scope>
        </dependency>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-web</artifactId>
        </dependency>

        <dependency>
            <groupId>org.projectlombok</groupId>
            <artifactId>lombok</artifactId>
            <version>${lombok.version}</version>
            <scope>provided</scope>
        </dependency>
        <dependency>
            <groupId>com.alibaba</groupId>
            <artifactId>fastjson</artifactId>
            <version>${fastjson.version}</version>
        </dependency>
    </dependencies>
    <build>
        <plugins>
            <plugin>
                <groupId>org.springframework.boot</groupId>
                <artifactId>spring-boot-maven-plugin</artifactId>
                <version>1.5.9.RELEASE</version>
                <configuration>
                    <!--该配置必须-->
                    <fork>true</fork>
                </configuration>
            </plugin>
            <plugin>
                <groupId>org.apache.maven.plugins</groupId>
                <artifactId>maven-compiler-plugin</artifactId>
                <configuration>
                    <source>1.8</source>
                    <target>1.8</target>
                </configuration>
            </plugin>
        </plugins>
    </build>

    <modules>
        <module>yixue-commom</module>
        <module>yixue-admin</module>
        <module>yixue-video</module>
    </modules>
</project>

这就是我移植项目遇到的一些问题,下面列一些大佬的博客,对我帮助很大,不胜感激
如有侵权,请联系我删除

推荐阅读