zoukankan      html  css  js  c++  java
  • 编写第一个springboot应用

    1.1.1.   设置spring boot的parent

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

    说明:Spring boot的项目必须要将parent设置为spring boot的parent,该parent包含了大量默认的配置,大大简化了我们的开发。

    1.1.2.   导入spring boot的web支持

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

      

    1.1.3.   添加Spring boot的插件

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

    1.1.4.   编写第一个Spring Boot的应用

    @Controller
    
    @SpringBootApplication
    
    @Configuration
    
    public class HelloApplication {
    
       
    
        @RequestMapping("hello")
    
        @ResponseBody
    
        public String hello(){
    
            return "hello world!";
    
        }
    
       
    
        public static void main(String[] args) {
    
            SpringApplication.run(HelloApplication.class, args);
    
        }
    
     
    
    }
    
     
    

    代码说明:

    1、@SpringBootApplication:Spring Boot项目的核心注解,主要目的是开启自动配置。;

    2、@Configuration:这是一个配置Spring的配置类;

    3、@Controller:标明这是一个SpringMVC的Controller控制器;

    4、main方法:在main方法中启动一个应用,即:这个应用的入口;

    1.1.5.   启动应用

    在Spring Boot项目中,启动的方式有两种,一种是直接run Java Application另外一种是通过Spring Boot的Maven插件运行。

    第一种:

     

    第二种:

     

    启动效果:

     

    看到如下信息就说明启动成功了:

    INFO 6188 --- [           main] c.i.springboot.demo.HelloApplication     : Started HelloApplication in 3.281 seconds (JVM running for 3.601)

    1.1.6.   测试

    打开浏览器,输入地址:

     

    效果:

     

    是不是很Easy?

  • 相关阅读:
    Field client in com.rachel.web.ConsumerController required a bean of type 'org.springframework.cloud.netflix.ribbon.RibbonLoadBalancerClient' that could not be found.
    MySQl创建用户和授权
    MySQL之索引原理与慢查询优化
    MySQL之视图、触发器、事务、存储过程、函数
    Navicat工具、pymysql模块
    MySQL之多表查询
    MySQL之单表查询
    MySQL行(记录)的详细操作
    MySQL的库表详细操作
    MySQL数据库初识
  • 原文地址:https://www.cnblogs.com/renshengruozhiruchujian/p/7799189.html
Copyright © 2011-2022 走看看