天天看點

使用eclipse建立第一個SpringBoot項目

1、new->maven->maven project,   勾選 Create a simple project,  下一個頁面中填入group id(項目組織唯一辨別, 如org.apache)和artifact id(應用名,本例子中為

SpringApplicationDemo),其他預設      
使用eclipse建立第一個SpringBoot項目

2、配置pom.xml

<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.admin</groupId>
    <artifactId>SpringTest</artifactId>
    <version>0.0.1-SNAPSHOT</version>
    <packaging>jar</packaging>

    <name>SpringApplicationDemo</name>
    <description>Demo project for Spring Boot</description>

    <parent>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-parent</artifactId>
        <version>2.0.3.RELEASE</version>
        <relativePath /> <!-- lookup parent from repository -->
    </parent>

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

        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-test</artifactId>
            <scope>test</scope>
        </dependency>

    </dependencies>

    <build>
        <plugins>
            <plugin>
                <groupId>org.springframework.boot</groupId>
                <artifactId>spring-boot-maven-plugin</artifactId>
            </plugin>
        </plugins>
    </build>
</project>      

3、在src/main/java下建立包com.admin,建立類Application,作為程式入口

@SpringBootApplication
public class Application {

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

在包下建立類Controller類, 這裡建立HelloController類

@RestController
public class HelloController {
    
    @RequestMapping("/hello")
    public String test() {
        return "Hello, World!";
    }
}      

4、在src/main/resources下建立application.properties檔案,寫上

   server.port=8080

5、run as -> java application, 等待啟動成功(檢視console輸出),

6、在浏覽器中輸入http://localhost:8080/hello

  即可在浏覽器中看到HelloController類中hello對應的字元串

轉載于:https://www.cnblogs.com/wushengwuxi/p/9777320.html