1.maven创建springboot

1.先配置一下maven的配置,让maven的编译选择1.8jdk,以后就不用收动改了

<mirrors>
      <mirror>
        <id>nexus-aliyun</id>
        <mirrorOf>central</mirrorOf>
        <name>Nexus aliyun</name>
        <url>http://maven.aliyun.com/nexus/content/groups/public</url>
      </mirror>
  </mirrors>
 
  <profiles>
         <profile>
              <id>jdk-1.8</id>
              <activation>
                <activeByDefault>true</activeByDefault>
                <jdk>1.8</jdk>
              </activation>
              <properties>
                <maven.compiler.source>1.8</maven.compiler.source>
                <maven.compiler.target>1.8</maven.compiler.target>
                <maven.compiler.compilerVersion>1.8</maven.compiler.compilerVersion>
              </properties>
         </profile>
  </profiles>

 

 

 

1.创建maven工程

选择 org.apache.maven.archetypes:maven-archetype-quickstart 快速开始

创建成功后在maven的配置文件中导入springboot的配置继承,和现在要用到的web依赖

在springboot中集成了非常多的依赖版本,只需要导入主要的依赖 比如web,就会包含其他很多关于web的依赖,比如tomcat json apo 等等相关的依赖,版本统一,版本如果要自己弄 那要崩的

<parent>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-parent</artifactId>
        <version>2.3.4.RELEASE</version>
    </parent>


    <dependencies>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-web</artifactId>
        </dependency>

    </dependencies>

 

2.创建主程序

/**
 * 主程序类
 * @SpringBootApplication:这是一个SpringBoot应用
 */
@SpringBootApplication
public class MainApplication {

    public static void main(String[] args) {
    ConfigurableApplicationContext Context = SpringApplication.run(MainApplication.class,args); 
  } 
}
@SpringBootApplication:申明这是一个SpringBoot应用 
ConfigurableApplicationContext Context = SpringApplication.run(MainApplication.class,args);  返回主是IOC容器

3.业务编写

@RestController
public class HelloController {


    @RequestMapping("/hello")
    public String handle01(){
        return "Hello, Spring Boot 2!";
    }


}
@RestController 这个注解的作用是 

@Controller:申明这个类是个组件

@ResponseBody 数据会转成json放在请求体中

4.测试运行

直接运行main方法运行

 

posted @ 2022-09-02 17:35  咖喱给给啊  阅读(562)  评论(0)    收藏  举报