使用Maven创建基础的springboot项目

  1. 安装Maven 3.3.9
    安装链接:https://mirrors.bfsu.edu.cn/apache/maven/maven-3/
    解压到D:\apache-maven-3.3.9-bin
    环境变量的配置:

    path %M2_HOME%\bin
    M2_HOME D:\apache-maven-3.3.9-bin\apache-maven-3.3.9

    cmd 输入mvn -vyan

  2. 配置Maven
    D:\apache-maven-3.3.9-bin\apache-maven-3.3.9\conf\settings.xml
    使用阿里云下载路径

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

    设置仓库位置

    <localRepository>D:\Maven-Repository</localRepository>
  3. Eclipse配置Maven
    设置MAVEN路径
    菜单->Window->Preferences->Maven->Installations-> 指定

    D:\apache-maven-3.3.9-bin\apache-maven-3.3.9

    设置仓库路径
    菜单->Window->Preferences->Maven->User Settings->
    Global Settings 和 User Settings都使用:

    D:\apache-maven-3.3.9-bin\apache-maven-3.3.9\conf\settings.xml

    点击一下Reindex,确保 local Repository为:

    D:\Maven-Repository
  4. Eclipse创建Springboot项目
    菜单 -> File -> New -> Other -> Maven -> Maven -> Maven Project -> New Maven Project
    勾上 Create a simple project (skip archetype selection)

    groupId:com.java
    artifactId:springboot
    name:springboot
    description:springboot

    修改pom.xml,右键点击项目->Maven->Update Project 更新一下项目

    <project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
    <modelVersion>4.0.0</modelVersion>
    <groupId>com.java</groupId>
    <artifactId>springboot</artifactId>
    <version>0.0.1-SNAPSHOT</version>
    <name>springboot</name>
    <description>springboot</description>
    <!--   <!-从Spring Boot继承默认值->  -->
    <parent>
     <groupId>org.springframework.boot</groupId>
     <artifactId>spring-boot-starter-parent</artifactId>
     <version>2.0.0.RELEASE</version>
     <relativePath />
    </parent>
    <!--   <!-为Web应用程序添加典型的依赖关系->  -->
    <dependencies>
     <dependency>
         <groupId>org.springframework.boot</groupId>
         <artifactId>spring-boot-starter-web</artifactId>
     </dependency>
     <dependency>
         <groupId>junit</groupId>
         <artifactId>junit</artifactId>
         <version>3.8.1</version>
         <scope>test</scope>
      </dependency>
    </dependencies>
    
     <properties>
         <java.version>1.8</java.version>
     </properties>
    <!--  <!-打包为可执行jar->  -->
     <build>
         <plugins>
             <plugin>
                 <groupId>org.springframework.boot</groupId>
                 <artifactId>spring-boot-maven-plugin</artifactId>
             </plugin>
         </plugins>
     </build>
    </project>

    创建主类,springboot中内置了Tomcat

    package com.java;
    import org.springframework.boot.SpringApplication;
    import org.springframework.boot.autoconfigure.SpringBootApplication;
    @SpringBootApplication
    public class Application {
    
     public static void main(String[] args) {
         // TODO Auto-generated method stub
         SpringApplication.run(Application.class, args);
     }
    }
    

创建Controller

package com.java.controller;

import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
//将controller的方法返回的对象通过适当的转换器转换为指定的格式之后,写入到response对象的body区,通常用来返回JSON数据或者是XML数据
@ResponseBody
@Controller
public class HelloController {
    @RequestMapping("/hello")
    public String hello() {
        return "Hello Spring Boot!";
    }
}

运行主类,访问http://127.0.0.1:8080/hello

全部评论

相关推荐

点赞 2 评论
分享
牛客网
牛客企业服务