使用Maven命令创建一个Maven项目
mvn archetype:generate:Maven插件原型是一个Maven项目模板工具包。
-DgroupId 包名
-DartifactId 项目名
-DarchetypeArtifactId 类型maven-archetype-quickstart,创建一个Java Project,maven-archetype-webapp,创建一个Web Project
-DinteractiveMode 是否使用交互模式,如果为false,非交互式的命令后直接创建,否则会有控制台提示输入操作
执行构建命令
mvn archetype:generate -DinteractiveMode=false -DgroupId=com.xcy -DartifactId=springboot -Dversion=1.0.0-SNAPSHOT
使用IDEA打开
在pom文件中添加springboot依赖
<parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>2.0.5.RELEASE</version>
</parent>
<dependencies>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
</dependencies>
添加后的pom文件完整版如下:
<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/maven-v4_0_0.xsd">
<modelVersion>4.0.0</modelVersion>
<groupId>com.xcy</groupId>
<artifactId>springboot</artifactId>
<packaging>jar</packaging>
<version>1.0.0-SNAPSHOT</version>
<name>springboot</name>
<url>http://maven.apache.org</url>
<parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>2.0.5.RELEASE</version>
</parent>
<dependencies>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
<scope>test</scope>
</dependency>
</dependencies>
</project>
添加测试代码
将App改变为启动类
spring boot提供了一个统一的注解@SpringBootApplication。
@SpringBootApplication = (默认属性)@Configuration + @EnableAutoConfiguration + @ComponentScan
@SpringBootApplication
public class App
{
public static void main( String[] args ) {
SpringApplication.run(App.class,args);
}
}
测试
必须要写在App启动类一个包下才能够扫描到
@RestController注解相当于@ResponseBody + @Controller合在一起的作用。
@RestController
public class first {
@GetMapping("hello")
public String index(){
return "HelloWorld~";
}
完整代码如下:
package com.xcy;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RestController;
@RestController
class first {
@GetMapping("hello")
public String index() {
return "HelloWorld~";
}
}
@SpringBootApplication
public class App
{
public static void main( String[] args ) {
SpringApplication.run(App.class,args);
}
}
访问测试
IDEA 运行后,在web中输入