SpringBoot+IDEA+Maven快速入门

一、首先创建一个Maven项目,比较简单

二、在pom文件中添加依赖

如下:


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

    <modelVersion>4.0.0</modelVersion>
    <artifactId>Springboot</artifactId>
    <packaging>war</packaging>
    <name>Springboot Maven Webapp</name>
    <url>http://maven.apache.org</url>

    <properties>
        <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
        <project.reporting.outputEncoding>UTF-8</project.reporting.outputEncoding>
        <java.version>1.8</java.version>
    </properties>
    <dependencies>
        <dependency>
            <groupId>junit</groupId>
            <artifactId>junit</artifactId>
            <version>3.8.1</version>
            <scope>test</scope>
        </dependency>

        <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>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-thymeleaf</artifactId>
        </dependency>

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

三、开始写Java代码

1、首先创建一个Application类,里面添加一个main()方法,


    @SpringBootApplication
public class Application {

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

需要注意的是:这个类的上面需要添加SpringBoot的注解@SpringBootApplication,然后在主函数中开始启动。

2、然后创建一个HelloWord类,用来测试SpringBoot

如下:


@RestController
@EnableAutoConfiguration
public class HelloWord {

    @RequestMapping("/hello")
    public String hello() {
        return "hello,Spring boot!";
    }

    //带参数
    @RequestMapping("/word/{name}")
    public String word(@PathVariable String name) {
        return "word--spring boot:" + name;
    }
}

需要注意的是这上面的几个注解:

  • @RestController:表示这是个控制器,和Controller类似
  • @EnableAutoConfiguration:springboot没有xml配置文件因为这个注解帮助我们干了这些事情,有了这个注解springboot启动的时候回自动猜测你的配置文件从而部署你的web服务器
  • @RequestMapping(“/hello”):这个和SpringMvc中的类似了。
  • @PathVariable:参数

Ok,就这样,一个简单的SpringBoot小demo就写出来了,我们直接就可以运行了,然后输入:http://localhost:8080/hellohttp://localhost:8080/word/canshu 浏览器上即可得到输出结果

源码下载


版权声明:本文为qq_28702545原创文章,遵循CC 4.0 BY-SA版权协议,转载请附上原文出处链接和本声明。