springboot简单入门


创建普通springboot项目

  1. file->new project->spring initialzr

 选择next,然后什么都不选直接确定,或者想要方便一点可以添加一个web的选项,区别就是后续添加注解@RequestMapping要用到springboot-web的依赖

 直接选择了springweb,然后finish就行了

<?xml version="1.0" encoding="UTF-8"?>

         xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 https://maven.apache.org/xsd/maven-4.0.0.xsd">
    4.0.0
    com.example
    firstHelloWorld
    0.0.1-SNAPSHOT
    firstHelloWorld
    firstHelloWorld

    
        1.8
        UTF-8
        UTF-8
        2.3.7.RELEASE
    

    
        
            org.springframework.boot
            spring-boot-starter
        

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

    
        
            
                org.springframework.boot
                spring-boot-dependencies
                ${spring-boot.version}
                pom
                import
            
        
    

    
        
            
                org.apache.maven.plugins
                maven-compiler-plugin
                3.8.1
                
                    1.8
                    1.8
                    UTF-8
                
            
            
                org.springframework.boot
                spring-boot-maven-plugin
                2.3.7.RELEASE
                
                    com.example.firsthelloworld.FirstHelloWorldApplication
                
                
                    
                        repackage
                        
                            repackage
                        
                    
                
            
        
    

项目结构如下

 在java下的包中建立子包,名叫controller包,在包中写一个映射方法(注意,包要和Application在同一个包下,和Application同级或者在Application之下,否则无法自动扫描,无法启动)

这是controller代码

package com.example.firsthelloworld.controller;

import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

/**
 * @author kkk Email:1959608508.com
 * @Description:
 * @version:
 * @Date 2021/12/24 17:00
 */
@Controller
public class hello {
    @ResponseBody
    @RequestMapping("hello")
    public String hello(){
        return "hello,springboot";
    }
}

直接启动项目,不用配置tomcat,tomcat在springboot中内置了

然后在浏览器访问http://localhost:8080/hello

想要更改tomcat启动的端口号需要在Application中进行配置



server.port=8989