• Eclipse导入springboot项目遇到的一些坑


    			Eclipse导入springboot项目遇到的一些坑
    
    • 1
    • 用Eclipse生成springboot项目。
      我用的是在https://start.spring.io/上面生成一个项目,然后导入到Eclipse中的。
      在这里插入图片描述
      如图,这边可以对SpringBoot项目进行详细设置:
      在这里插入图片描述在这里插入图片描述
      点击按钮下载
      在这里插入图片描述在这里插入图片描述
      然后在Eclipse中导入下载下来的项目在这里插入图片描述然后下一步下一步就好了,注意第一次导入SpringBoot项目,会自动下载很多jar包,建议替换成阿里云的maven仓库,不然会很慢。
      这是主程序的启动类。
      在这里插入图片描述

    这是主程序中的代码。

    @SpringBootApplication
    public class DemoApplication {
    
    	public static void main(String[] args) {
    		SpringApplication.run(DemoApplication.class, args);
    	}
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6

    由于我们需要的是web程序,那么我们还需要在DemoApplication .java同级目录下创建一个SpringBootStartApplication.java的文件。
    需要继承【SpringBootServletInitializer】,并重写【configure】方法
    在这里插入图片描述

    public class SpringBootStartApplication extends SpringBootServletInitializer{
    	 @Override
    	    protected SpringApplicationBuilder configure(SpringApplicationBuilder builder) {
    	        return builder.sources(DemoApplication.class);
    	    }
    }
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6

    那么这里就要注意下,这里继承SpringBootServletInitializer后可能会出现错误,找了很多的解决办法最后在pom.xml文件中加上

    
    		    org.springframework.boot
    		    spring-boot-starter-web
    		
    
    • 1
    • 2
    • 3
    • 4

    嵌入式Web容器层面的约定和定制
    spring-boot-starter-web默认使用嵌套式的Tomcat作为Web容器对外提供HTTP服务,默认端口8080对外监听和提供服务。
    这样就解决了包的问题。
    这时运行程序发现不是web项目,访问地址访问不了,服务直接挂掉。
    这是就需要下一步操作了。
    选中项目右键—→【properties】—→选择【ProjectFacets】
    在这里插入图片描述
    然后运行主程序OK完美!
    在这里插入图片描述
    运行发现没有内容,于是还得加上一个controller。
    在这里插入图片描述
    在这里插入图片描述
    再运行。在这里插入图片描述
    完美,哈哈第一个搭建的springboot程序就成功了。
    最近也是很多公司都开始着手这个框架了,这里推荐一个博客相当不错。
    http://www.ityouknow.com/很不错,值得推荐。

  • 相关阅读:
    Elasticsearch应用场景(三)
    PD充电调试
    从功能测试到自动化测试你都知道他们的有缺点吗?
    HCIA网络基础10-交换网络及STP
    2023数维杯数学建模C题思路+代码+论文
    shell编程01_Shell基础
    if-else练习
    [附源码]java毕业设计基于JAVAWEB医院挂号系统
    微信公众号网页授权登录获取用户基本信息
    RFID固定资产管理的应用
  • 原文地址:https://blog.csdn.net/m0_67401153/article/details/126358219