springboot踩坑出坑记

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

<!--more-->java

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之类的种种,但没扫到,好吧~

解决方法:mysql

  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文件,我就一次性都配上了,这个你们具体遇到的时候再去搜一下就行,不用刻意的记:git

<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......github

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

<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得本身找...redis

<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...spring

  • 以后配置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中报错了:sql

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装配,再用...:apache

@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>

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

原文出处:https://www.cnblogs.com/cxylff/p/10969375.html

相关文章
相关标签/搜索