四、创建第一个springboot项目

简介

spring boot 它的设计目的就是为例简化开发,开启了各种自动装配,你不想写各种配置文件,引入相关的依赖就能迅速搭建起一个web工程。它采用的是建立生产就绪的应用程序观点,优先于配置的惯例。

超过十年行业经验,技术领先,服务至上的经营模式,全靠网络和口碑获得客户,为自己降低成本,也就是为客户降低成本。到目前业务范围包括了:做网站、网站建设,成都网站推广,成都网站优化,整体网络托管,微信平台小程序开发,微信开发,重庆App定制开发,同时也可以让客户的网站和网络营销和我们一样获得订单和生意!

建构准备

  • jdk 1.8 或以上
  • maven 3.0+
  • IntelliJ IDEA
    打开Idea-> new Project ->Spring Initializr ->填写group、artifact ->钩上web(开启web功能)->点下一步就行了。
    四、创建第一个springboot项目
    四、创建第一个springboot项目
    四、创建第一个springboot项目
    创建完工程,工程的目录结构如下:
    四、创建第一个springboot项目
  • pom文件为基本的依赖管理文件
  • resouces 资源文件
  • statics 静态资源
  • templates 模板资源
  • application.properties 配置文件
  • SpringbootApplication程序的入口。
    POM文件源码:


    4.0.0
    
        org.springframework.boot
        spring-boot-starter-parent
        2.1.3.RELEASE
         
    
    com.honghh
    boot-first
    0.0.1-SNAPSHOT
    boot-first
    Demo project for Spring Boot

    
        1.8
    

    
        
            org.springframework.boot
            spring-boot-starter-web
        

        
            org.springframework.boot
            spring-boot-starter-test
            test
        
    

    
        
            
                org.springframework.boot
                spring-boot-maven-plugin
            
        
    


创建Controller

package com.honghh.bootfirst.controller;

import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

/**
 * ClassName: HelloWordController
 * Description:
 *
 * @author honghh
 * @date 2019/02/19 15:58
 */
@RestController
public class HelloWordController {
    @RequestMapping("/")
    public String index() {
        return "Hello Spring Boot!";
    }
}

启动项目,在浏览器中输入: http://localhost:8080/
四、创建第一个springboot项目
启动成功,第一个springboot项目搭建成功!

但是这个要注意一个点,现在我的controller是写在com.honghh.bootfirst下的,所以没有问题,我们将controller包放在com.honghh.controller下执行你会发现报404
四、创建第一个springboot项目
那我们应该怎么解决呢?

注意事项

Spring Boot 正常启动后访问Controller提示404

解决办法

方法一:

以启动类的包路径作为顶层包路径,列如启动类包为com.honghh.bootfirst,那么Controller包路径就为com.honghh.bootfirst.controller。

方法二:

在启动上方添加@ComponentScan注解,此注解为指定扫描路径,例如:

@ComponentScan(basePackages = {"com.honghh.*"})   #多个不同的以逗号分割。
package com.honghh.bootfirst;

import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.context.annotation.ComponentScan;

@ComponentScan(basePackages = {"com.honghh.*"})
@SpringBootApplication
public class BootFirstApplication {

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

}

文章来源: https://blog.csdn.net/qq_35098526/article/details/87715317


分享题目:四、创建第一个springboot项目
网站链接:http://lszwz.com/article/pjshop.html

其他资讯

售后响应及时

7×24小时客服热线

数据备份

更安全、更高效、更稳定

价格公道精准

项目经理精准报价不弄虚作假

合作无风险

重合同讲信誉,无效全额退款