第一个springboot项目经验

最后都变了- 提交于 2020-01-07 04:18:00

【推荐】2019 Java 开发者跳槽指南.pdf(吐血整理) >>>

1)根据https://blog.csdn.net/baidu_39298625/article/details/98102453及网上其它人的经验,搭建环境了,并且也建了@controller及@RequestMapping了,结果出现:Failed to configure a DataSource: 'url' attribute is not specified and no embedded datasource could be configured.

2)这说明,我选择了数据库,但没有配置,根据https://blog.csdn.net/qq_40223688/article/details/88191732 和 https://blog.csdn.net/Gentlemike/article/details/81567750 中的说明,通过在主类所在的@springbootapplication中增加属性exclude={DataSourceAutoConfiguration.class} 取消数据源自动配置,也可以另外再增 加一个注解

@EnableAutoConfiguration(exclude = DataSourceAutoConfiguration.class),效果一样;

3) 在游览器上输入http://localhost:8080/index 即可成功。

 

另外,在项目build过程中总是出现org.springframework.boot....红色的错误,那是因为导包出现问题,也就是maven配置仓库出现问题,我的项目是maven3,所以我在D:\Program Files\JetBrains\IntelliJ IDEA Community Edition 2018.2.4\plugins\maven\lib\maven3\conf中将setting.xml中的mirrors节点下增加

<id>nexus-aliyun</id>
        <!--该镜像用来取代的远程仓库,central是中央仓库的id-->
        <mirrorOf>central</mirrorOf>
        <name>Nexus aliyun</name>
        <!--该镜像的仓库地址,这里是用的阿里的仓库-->
        <url>http://maven.aliyun.com/nexus/content/groups/public</url>      

注意,只是在idea软件中配置maven仓库是不起作用的,参考https://blog.csdn.net/weixin_43584947/article/details/90714328 ,我的是maven3所以要在idea下的plugs下的相关maven3\conf中修改才起作用,当我这样修改之后就没有红色的错误了,所有包都导入正确了;不过我曾经刚开始学习的时候在c:\joe\.m2\setting.xml (在idea设置中maven下的User setting file的配置)中这样修改过

 <mirror>
            <id>alimaven</id>
            <name>aliyun maven</name>
            <url>http://maven.aliyun.com/nexus/content/groups/public/</url>
            <mirrorOf>central</mirrorOf>
        </mirror> 

,现在如下图中所示总是红色,现在还不知道如何处理:

后来我发现是在项目的pom.xml增加了:

<repositories>
   <repository>
      <id>alimaven</id>
      <name>Maven Aliyun Mirror</name>
      <url>http://maven.aliyun.com/nexus/content/repositories/central/</url>
      <releases>
         <enabled>true</enabled>
      </releases>
      <snapshots>
         <enabled>false</enabled>
      </snapshots>
   </repository>
</repositories>

的原因,去掉这个配置则上图中的红色错误就没有了。

其实真正的阿里maven地址应该是下面的这种配置,这是我将idea卸载重装之后的经验,这个办法是要在项目的总pom.xml中配置才行

<repositories>
        <repository>
            <id>aliyun</id>
            <name>aliyun</name>
            <url>http://maven.aliyun.com/nexus/content/groups/public</url>
        </repository>
    </repositories>

另外,后来我发现maven全局改阿里仓库其实应该是在D:\Program Files\IntelliJ IDEA 2018.3.1\plugins\maven\lib\maven3\conf\setting.xml中的mirrors标签下增加:

<mirror>
        <id>nexus-aliyun</id>
        <!--该镜像用来取代的远程仓库,central是中央仓库的id-->
        <mirrorOf>central</mirrorOf>
        <name>Nexus aliyun</name>
        <!--该镜像的仓库地址,这里是用的阿里的仓库-->
        <url>http://maven.aliyun.com/nexus/content/groups/public</url>    
    </mirror>

前面也就是说重装IDEA之前这么做了为什么不成功呢?其实是我漏了<mirror></mirror>这个标签对。

再说第二很大的坑:

当我重装IDEA,按照上面所述搞好之后,建立一个简单的spring boot项目hello,目的就是在游览器上输入http://localhost:8080/index之后显示hello,按照网上所述“Application启动类的位置不对”,这种方法对我现在的版本没用,但我记得得装idea之前是可以正常hello 的,各种注解都是一模一样的,为什么新版本idea就不行呢?最后我参考https://blog.csdn.net/qushaming/article/details/94593675这个帖子所述,感觉可能是我的idea版本下载得太低的原因,所以将controller类改为如下:

@Controller
public class hellocontroller {

    @GetMapping("/index")
    @ResponseBody
    public String sayhi()
    {
        return "hello";
    }
}

则成功了,而原来版本idea实验时的代码如下:

@Controller
public class HelloController {

    @RequestMapping("/index")
    public String sayhello()
    {
        return "hello";
    }
}

其实后来根据上面帖子中有人回复说将@Controller换成@RestController,我试了试:

@RestController
public class hellocontroller {

    @RequestMapping("/index")
    public String sayhi()
    {
        return "helloRestController";
    }
}

结果也OK。

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!