SpringBoot+IDEA+Maven快速入门

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

2、在pom文件中添加依赖

以下:java

<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>

 

3、开始写Java代码

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

@SpringBootApplication
public class Application {

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

 

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

二、而后建立一个HelloWord类,用来测试SpringBootapache

以下:浏览器

@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;
    }
}

 

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

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

Ok,就这样,一个简单的SpringBoot小demo就写出来了,咱们直接就能够运行了,而后输入:http://localhost:8080/hellohttp://localhost:8080/word/canshu 浏览器上便可获得输出结果服务器

相关文章
相关标签/搜索