SpringBoot01——Framework Introduced and Helloworld

1.介绍

SpringBoot主要解决的是在微服务的架构下简化配置(有快速配置)、前后端分离、快速开发

优点:

  • l 提供了快速启动入门
  • l 开箱即用、提供默认配置
  • l 内嵌容器化web项目
  • l 没有冗余代码生成和xml配置要求

2.运行Demo

创建SpringBoot项目的几种方式:

  • l 官网的Initializr
  • l 使用Eclipse、STS、Idea等IDE创建Maven项目并引入依赖
  • l 使用STS插件的Spring Initializr创建项目

访问http://start.spring.io/  进入Spring项目Initializr

生成下载demo.zip

导入项目

1.Import一个Maven项目

2.选择要导入的文件

3.项目骨架

启动项目

l 直接run启动程序里的Main()方法

l 安装过STS插件或使用STS可以在项目上右键RunAS->Spring Boot APP

运行成功提示信息:

如果运行报错,请参照常见问题。

个性化

修改启动banner

在resources目录下新建banner.txt

http://www.network-science.de/ascii/  英文

https://www.degraeve.com/img2txt.php 图片

${AnsiColor.BRIGHT_CYAN}
 _      _  _    _    _       ______
| |    (_)| |  | |  | |      | ___ \
| |     _ | |_ | |_ | |  ___ | |_/ /__ _   __ _   ___
| |    | || __|| __|| | / _ \|  __// _` | / _` | / _ \
| |____| || |_ | |_ | ||  __/| |  | (_| || (_| ||  __/
\_____/|_| \__| \__||_| \___|\_|   \__,_| \__, | \___|
                                           __/ |
                                          |___/
${AnsiColor.BRIGHT_RED}
Logo Designer: LittlePage
Spring Boot Version: ${spring-boot.version}${spring-boot.formatted-version}

Eclipse的皮肤

菜单栏中

Help -> EclipseMarketplace

搜索Theme

傻瓜式安装这个,安装完成会提示重启,跟随指引选择喜欢的风格。

简单使用

application.properties

把所有的配置全放在这个文件里,方便统一管理,maven也可以做到

修改tomcat端口

server.port=90

修改项目路径

server.servlet.context-path=/demo

多个入口main方法,打包之后找不到入库类

<build>
   <plugins>
      <plugin>
          <groupId>org.springframework.boot</groupId>
          <artifactId>spring-boot-maven-plugin</artifactId>
                <configuration>
                    <mainClass>com.yxxy.MyApp</mainClass>
                </configuration>
            </plugin>
        </plugins>
</build>

HelloWorld

RestController

RestController = @[email protected]

一个效果

@RestController

public class MyAppController {

@RequestMapping("/")

public Map<String, String>  index() {

Map<String, String> map = new HashMap<>();

map.put("aaa", "bbb");

map.put("aaa", "bbb");

map.put("aaa", "bbb");

map.put("aaa", "bbb");

return map;

}

使用thymeleaf模板引擎

Pom.xml引用
<dependency>

<groupId>org.springframework.boot</groupId>

<artifactId>spring-boot-starter-thymeleaf</artifactId>

</dependency>
Controller代码
@Controller

public class IndexController {

@RequestMapping("/")

public String index(ModelMap map) {

// 加入一个属性,用来在模板中读取

map.addAttribute("msg", "nihao~");

return模板文件的名称,对应src/main/resources/templates/index.html

return "index";

}

模板文件代码
<h1 th:text="${msg}">hi!</h1>

稍微复杂的restful api应用

UserRestfulController
@RequestMapping("/")

@RestController

public class UserRestfulController {

static Map<Long, User> users = Collections.synchronizedMap(new HashMap<Long,User>());

@RequestMapping(value="/User",method=RequestMethod.GET)

public List<User> getUserList(){

ArrayList<User> list = new ArrayList<>(users.values());

return  list;

}

@RequestMapping(value="User",method=RequestMethod.POST)

public String addUser(@ModelAttribute User user) {

users.put(user.getId(), user);

return "addUser Success";

}

}

User
public class User {

private Long id;

private String loginName;

private String password;

private String nickName;

注入Service
UserRestfulController

@Autowired

private UserService userSrv;

@RequestMapping(value="/User",method=RequestMethod.GET)

public List<User> getUserList(){

return  userSrv.getUserList();

}

@RequestMapping(value="User",method=RequestMethod.POST)

public String addUser(@ModelAttribute User user) {

String msg = userSrv.addUser(user);

return msg;

}

UserService
@Service

public class UserService {

static Map<Long, User> users = Collections.synchronizedMap(new HashMap<Long,User>());

public List<User> getUserList() {

ArrayList<User> list = new ArrayList<>(users.values());

return list;

}

public String addUser(User user) {

users.put(user.getId(), user);

return "addUser Success";

}

}

前端模板显示

 <h1>User list</h1>

  <table>

    <tr>

      <th>NAME</th>

      <th>loginName</th>

      <th>nickName</th>

    </tr>

    <tr th:each="user : ${list}">

      <td th:text="${user.id}">id</td>

      <td th:text="${user.loginName}">loginName</td>

      <td th:text="${user.nickName}">nickName</td>

    </tr>

  </table>

  <p>

    <a href="../home.html" th:href="@{/}">Return to home</a>

  </p>

常见问题

Pom.xml

Springboot项目必须要继承的parnet

Pom.xml第一行报错

进入本地库

for /r %i in (*.lastUpdated) do del %i   

然后update

缺少或包错误

删掉 重新update

找不到主类

所有错误都解决后

Jar方式运行 首先得有这个jar包

先clean package 生成jar文件,然后再run main方法

找不到jdk

把jre的路径换成jdk的

启动后自动停止

原文地址:https://www.cnblogs.com/littlepage/p/11031303.html

时间: 2024-10-27 08:01:52

SpringBoot01——Framework Introduced and Helloworld的相关文章

(译)Asynchronous programming and Threading in C# (.NET 4.5)

原文地址:http://www.codeproject.com/Articles/996857/Asynchronous-programming-and-Threading-in-Csharp-N 介绍: Asynchronous programming and threading is very important feature for concurrent or parallel programming. Asynchronous programming may or may not us

OSGI(面向Java的动态模型系统)

基本简介编辑 OSGI服务平台提供在多种网络设备上无需重启的动态改变构造的功能.为了最小化耦合度和促使这些耦合度可管理,OSGi技术提供一种面向服务的架构,它能使这些组件动态地发现对方.OSGi联 OSGI 盟已经开发了为例如象HTTP服务器.配置.日志.安全.用户管理.XML等很多公共功能标准组件接口.这些组件的兼容性插件实现可以从进行了不同优化和使用代价的不同计算机服务提供商得到.然而,服务接口能够基于专有权基础上开发. 因为OSGi技术为集成提供了预建立和预测试的组件子系统,所以OSGi技

Differences Between Xcode Project Templates for iOS Apps

Differences Between Xcode Project Templates for iOS Apps When you create a new iOS app project in Xcode, you get to choose between several project templates, from the aptly named “Empty Application” to specialized things like an “OpenGL Game”. I noti

论文笔记-Augmented Lagrange Multiplier Method for Recovery of Low-Rank Matrices

论文题目:The Augmented Lagrange Multiplier Method for Exact Recovery of Corrupted Low-Rank Matrices Abstract 1.Robust PCA问题: recovering a low-rank matrix with an unknown fraction of its entries being arbitrarily corrupted. RPCA问题是一个凸优化问题:minimizes a comb

FOUNDATION OF ASYNCHRONOUS PROGRAMMING

The async and await keywords are just a compiler feature. The compiler creates code by using the Task class. Instead of using the new keywords, you could get the same functionality with C# 4 and methods of the Task class; it's just not as convenient.

Instrumentation: querying the memory usage of a Java object

Copy from: http://www.javamex.com/tutorials/memory/instrumentation.shtml Instrumentation: querying the memory usage of a Java object The most reliable— but not necessarily the easiest— way to estimate the usage of a Java object is to ask the JVM. Que

Custom View

Creating a View Class 本处参考Android官方文档Creating Custom Views.为了能够设计出良好的自定义视图,需要遵守以下规则: 遵从Android标准. 提供自定义的styleable属性以便于在Android Xml中配置视图时使用. Send accessibility events. 兼容多个Android平台. Android平台提供了许多基础类和xml标签来帮助我们创建满足这些要求的view.接下来讨论如何自定义拥有核心功能的view. And

Designing for iOS: Graphics &amp; Performance

http://robots.thoughtbot.com/designing-for-ios-graphics-performance  [原文] In the previous article, we explored different techniques to customize the look and feel of UIButton, assigning to each a difficulty level based on the complexity of the Object

Net4.5 的异步和线程(未完)

来源链接: http://www.codeproject.com/Articles/996857/Asynchronous-programming-and-Threading-in-Csharp-N?msg=5070746 异步编程和线程并发是非常重要的功能. 异步编程即可以使用线程,也可以不用,同样的,也可以与线程一起使用.我们最好先了解下这两个功能. 本文概括: 1,异步编程 2,何时需要使用线程 3,基于异步的Task 4,并行编程 5,结论 异步编程 异步指的是,独立于主线程之外的操作.