关于springboot:SpringBoot开发笔记1-springboot简介和helloworld

29次阅读

共计 2328 个字符,预计需要花费 6 分钟才能阅读完成。

1.1 长处:

简化 spring 开发 / 约定优于配置 :

  • 嵌入式的 servlet 容器;
  • starter 主动依赖和版本控制;
  • 大量主动配置, 简化开发;
  • 无需配置 xml, 无代码生成, 开箱即用;
  • 利用监控: 准生产环境的运行时利用监控;

1.2 毛病:

入门容易, 精通难~

1.3 微服务

springboot:

一个利用, 是一组小型服务, 能够通过 http 形式进行互通; 每一个性能元素都是一个能够独立替换和独立降级的单元;

springcloud:

整个利用的大型分布式网格之间的调用;

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 启动类

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

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

返回:

{"name":"niewj","age":33}

正文完
 0