SpringBoot開發筆記-(1) springboot簡介和helloworld

1.1 優勢:

簡化spring開發/約定優於配置 :java

  • 嵌入式的servlet容器;
  • starter自動依賴和版本控制;
  • 大量自動配置, 簡化開發;
  • 無需配置xml, 無代碼生成, 開箱即用;
  • 應用監控:準生產環境的運行時應用監控;

1.2 缺點:

入門容易, 精通難~web

1.3 微服務

springboot:spring

一個應用, 是一組小型服務, 能夠經過http方式進行互通; 每個功能元素都是一個能夠獨立替換和獨立升級的單元;shell

springcloud:apache

整個應用的大型分佈式網格之間的調用;springboot

1.4 helloworld

1.4.1 maven依賴:

<?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>com.niewj</groupId>
    <artifactId>springboot-study</artifactId>
    <version>1.0-SNAPSHOT</version>
    <properties>
        <spring-boot-version>2.3.2.RELEASE</spring-boot-version>
    </properties>
    <modules>
        <module>springboot-01-helloworld</module>
    </modules>

    <!-- 表示是一個pom總的父工程 -->
    <packaging>pom</packaging>

    <dependencies>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-parent</artifactId>
            <version>${spring-boot-version}</version>
        </dependency>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-web</artifactId>
            <version>${spring-boot-version}</version>
        </dependency>

    </dependencies>

</project>

1.4.2 類文件:

SpringBootApplication 啓動類app

package com.niewj.springboot;

import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;

/**
 * Created by niewj on 2020/8/4 18:04
 */
@SpringBootApplication
public class HelloApplication {
    public static void main(String[] args) {
        SpringApplication.run(HelloApplication.class, args);
    }
}

HelloController.javamaven

package com.niewj.springboot.controller;

import com.niewj.springboot.model.User;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

/**
 * Created by niewj on 2020/8/4 18:08
 */
@Controller
public class HelloController {

    @RequestMapping("/hello")
    @ResponseBody
    public Object hello(){
        return new User("niewj", 33);
    }
}

User實體類:分佈式

package com.niewj.springboot.model;

/**
 * Created by niewj on 2020/8/4 18:09
 */
public class User {

    private String name;
    private int age;

    public User(String name, int age) {
        this.name = name;
        this.age = age;
    }

    public String getName() {
        return name;
    }

    public void setName(String name) {
        this.name = name;
    }

    public int getAge() {
        return age;
    }

    public void setAge(int age) {
        this.age = age;
    }
}

完結; 訪問: http://localhost:8080/hellospring-boot

返回:

{"name":"niewj","age":33}
相關文章
相關標籤/搜索