zoukankan      html  css  js  c++  java
  • Spring Boot 2.0.1 入门教程

    简介

    Spring Boot是Spring提供的一套基础配置环境,可以用来快速开发生产环境级别的产品。尤其适合开发微服务架构,省去了不少配置麻烦。比如用到Spring MVC时,只需把spring-boot-starter-web依赖添加到Maven依赖中即可。另外它还有如下特性:

    • 创建独立的Spring项目
    • 内置Tomcat, Jetty,Undertow
    • 初始POM配置文件以简化Maven配置
    • 尽可能的自动配置Spring
    • 提供生产环境功能,如统计,健康检查和外部配置
    • 无需XML配置和代码生成

    创建 Spring Boot 应用

    • 开发环境:IntelliJ, JDK 1.8
    • 项目源代码 Gitee

    首先在IntelliJ中创建一个maven项目:

    • GroupID: cn.zxuqian
    • ArtifactId: helloworld

    创建完成后IntelliJ右下角会提示自动导入Maven配置,选择Enable Auto-Import来启动自动导入。然后在pom.xml添加入下代码:

    <?xml version="1.0" encoding="UTF-8"?>
    <project xmlns="http://maven.apache.org/POM/4.0.0"
             xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
             xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
        <modelVersion>4.0.0</modelVersion>
    
        <groupId>cn.zxuqian</groupId>
        <artifactId>helloworld</artifactId>
        <version>1.0-SNAPSHOT</version>
    
        <parent>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-parent</artifactId>
            <version>2.0.1.RELEASE</version>
        </parent>
    
        <dependencies>
            <dependency>
                <groupId>org.springframework.boot</groupId>
                <artifactId>spring-boot-starter-web</artifactId>
            </dependency>
        </dependencies>
    
        <properties>
            <java.version>1.8</java.version>
        </properties>
    
    
        <build>
            <plugins>
                <plugin>
                    <groupId>org.springframework.boot</groupId>
                    <artifactId>spring-boot-maven-plugin</artifactId>
                </plugin>
            </plugins>
        </build>
    
    </project>
    

    <dependencies> 标签添加了spring-boot-starter-web依赖,即 Spring MVC 和相关运行时环境。spring-boot-maven-plugin插件提供了一组maven运行目标,可以方便的打包,部署和运行应用。稍等片刻Maven自动下载依赖后就可以上手写代码了。

    创建第一个控制器

    src/main 下新建一个包 cn.zxuqian.controllers 并在其中新建一个类,名为 HelloController 并添加如下代码:

    package cn.zxuqian.controllers;
    
    import org.springframework.web.bind.annotation.RequestMapping;
    import org.springframework.web.bind.annotation.RestController;
    
    @RestController
    public class HelloController {
    
        @RequestMapping("/")
        public String index() {
            return "Hello World!";
        }
        
    }
    
    
    • @RestController 标记此类为 Rest 控制器,并准备好处理 Rest 请求。
    • @RequestMapping("/") 即此方法处理根路径请求,如 http://localhost:8080/
    • index 方法返回 String 类型,即响应返回的是字符串数据,这里是 "Hello World"。

    创建 Application 类

    cn.zxuqian 包下创建 Application 类,并添加如下代码:

    package cn.zxuqian;
    
    import org.springframework.boot.SpringApplication;
    import org.springframework.boot.autoconfigure.SpringBootApplication;
    
    @SpringBootApplication
    public class Application {
    
        public static void main(String[] args) {
            SpringApplication.run(Application.class, args);
        }
    }
    
    • @SpringBootApplication 标明了此类为 Spring Boot 应用的启动类。

    运行应用

    在IntelliJ的右侧选项卡中选择 Maven Projects,然后展开 Plugins-->spring-boot,选择 spring-boot:run 目标。待启动成功后,在浏览器中访问 http://localhost:8080 看到 Hello World! 即为成功。

    文章出自我的博客:http://zxuqian.cn/spring-boot-get-started/,欢迎访问。

  • 相关阅读:
    数据库专题(SQLServer、MySQL)
    第九节:IdentityServer4简介及客户端模式和用户密码模式的应用
    第八节:理解认证和授权、Oauth2.0及四种授权模式、OpenId Connect
    第七节:基于Ocelot网关层的微服务校验(手写jwt校验中间件和利用IdentityModel.Tokens.Jwt校验)
    第六节:Ocelot集成Polly熔断降级,以及缓存、限流、自身负载等其它功能
    第五节:基于Ocelot网关简介、路由功能、集成Consul使用
    第四节:Polly基于控制台和Web端用法(熔断、降级、重试、超时处理等)
    第三节:基于Consul做服务的配置中心
    第二节:Consul简介及服务注册、发现、健康检查
    第一节:微服务准备(Webapi复习、PostMan的使用、项目启动方式、业务代码准备)
  • 原文地址:https://www.cnblogs.com/zxuqian/p/8976122.html
Copyright © 2011-2022 走看看