Springboot入门——HelloWorld

实现功能:

       浏览器发送hello请求,服务器接受请求并处理,响应Hello World

1.创建maven工程:

     IDEA选择maven:不用勾选webapp

2.导入spring boot相关的依赖

1.基础核心依赖

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

2.web应用依赖

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


 

3.编写一个主程序:启动Spring Boot应用

/**
 * @SpringBootApplication 来标注一个主程序类,说明这是一个Spring Boot应用
 */
@SpringBootApplication
public class HelloWorldMainApplication {

    public static void main(String[] args) {

        //spring应用启动起来
        SpringApplication.run(HelloWorldMainApplication.class);
    }
}

@SpringBootApplication:

SpringBoot标注在某个类上说嘛这个类是SpringBoot的主配置类,SpringBoot就应该运行这个类的main方法来启动Spring Boot应用。                                

注解详解:

                

                @SpringBootConfiguration:标注在某个类上,表示这是一个Spring Boot的配置类

                           @Configuration:配置类上标注这个注解

4.编写相关的Controller、Service

@Controller
public class HelloWorldController {

    /*
    @RequestMapping("/hello") 表示接受浏览器的hello请求
    @ResponseBody 把返回值写回给浏览器
    */
    @RequestMapping("/hello")
    @ResponseBody
    public String hello(){
        return "Hello World";
    }

}

推荐编程:

//@ResponseBody:写在类上,是将类中的所有方法返回的数据直接写给浏览器,
// (如果返回值是对象,则自动生成转换成为json数据返回给浏览器)
/*
    @ResponseBody
    @Controller
*/
//RestController相当于@ResponseBody和@Controller

@RestController
public class HelloWorldControlller {

    //@ResponseBody: 只写在方法上,只对当前方法有效

    @RequestMapping("/hello")
    public String hello(){
        return "Hello World";
    }

}

5.启动,运行程序

   运行main方法即可

6.简化部署

    导入maven插件:

<!--这个插件可以将应用打包成一个可执行的jar包-->
    <build>
        <plugins>
            <plugin>
                <groupId>org.springframework.boot</groupId>
                <artifactId>spring-boot-maven-plugin</artifactId>
            </plugin>
        </plugins>
    </build>

   在maven的生命周期中选择package(lifecycle-package)

    打成jar包以后,在dos命令下切换到jar所在位置,

    运行 java -jar jar包名.jar

7.分析pom.xml文件

    (1)父项目   

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

     其父项目用来真正管理springboot应用里的所有依赖版本

<parent>
	<groupId>org.springframework.boot</groupId>
	<artifactId>spring-boot-dependencies</artifactId>
	<version>1.5.9.RELEASE</version>
	<relativePath>../spring-boot-dependencies</relativePath>
</parent>

     注意:spring boot版本控制中心:之后我们导入依赖默认不需要版本号,在dependencies中已经配置好了

              (没有在dependencies里面管理的依赖需要声明版本号)

  (2)导入的依赖  :  spring-boot-starter-web

              spring-boot-starter :spirng boot场景启动器,帮我们导入了web模块正常运行所依赖的组件

              spring boot 官方文档 https://docs.spring.io/spring-boot/docs/1.5.9.RELE

              更多启动器参考Table 13.1. Spring Boot application starters

                 https://docs.spring.io/spring-boot/docs/1.5.9.RELEASE/reference/htmlsingle/#using-boot-starter

              在项目中引入starter相关场景,所有相关依赖都会导入进来,需要什么功能就导入什么starter

猜你喜欢

转载自blog.csdn.net/xczjy200888/article/details/89048052
今日推荐