Maven專案Spring Boot啟動

learningops發表於2018-03-22

1. pom.xml中增加配置

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

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

2. 建立啟動類

建立package learningops.urlshortener

package learningops.urlshortener;

import org.springframework.boot.CommandLineRunner;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.context.annotation.ComponentScan;

/**
 * @author learningops
 * @date 01/03/2018
 */
@SpringBootApplication
@ComponentScan
public class Application implements CommandLineRunner {
    @Override
    public void run(String... args) throws Exception {
    }

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

3. 建立Controller

建立一個package learningops.urlshortener.controller

package learningops.urlshortener.controller;

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

/**
 * @author learningops
 * @date 22/03/2018
 */
@Controller
public class IndexController {

    @RequestMapping("/")
    @ResponseBody
    String home() {
        return "Hello World!";
    }
}

4. 執行

mvn spring-boot:run

相關文章