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 = @Controller+@ResponseBody
一个效果
@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的
启动后自动停止
SpringBoot01——Framework Introduced and Helloworld的更多相关文章
- Entity Framework 6 vs NHibernate 4
This article is dedicated to discussing the latest releases of the NHibernate and Entity Framework. ...
- OSGI框架—HelloWorld小实例
OSGi(Open Service Gateway Initiative)技术是Java动态化模块化系统的一系列规范.OSGi一方面指维护OSGi规范的OSGI官方联盟,另一方面指的是该组织维护的基于 ...
- Entity Framework Code-First(2):What is Code-First?
What is Code-First?: Entity Framework introduced Code-First approach from Entity Framework 4.1. Code ...
- Designing for iOS: Graphics & Performance
http://robots.thoughtbot.com/designing-for-ios-graphics-performance [原文] In the previous article, w ...
- 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 Xc ...
- OSGI(面向Java的动态模型系统)
基本简介编辑 OSGI服务平台提供在多种网络设备上无需重启的动态改变构造的功能.为了最小化耦合度和促使这些耦合度可管理,OSGi技术提供一种面向服务的架构,它能使这些组件动态地发现对方.OSGi联 O ...
- JVM菜鸟进阶高手之路十二(jdk9、JVM方面变化, 蹭热度)
转载请注明原创出处,谢谢! 经过 4 次跳票,历经曲折的 Java 9 正式版终于发布了!今天看着到处都是jdk9发布了,新特性说明,心想这么好的蹭热度计划能错过嘛,哈哈,所以就发了这篇文章. 目前j ...
- Enterprise Craftsmanship
转自:http://enterprisecraftsmanship.com/2015/04/13/dto-vs-value-object-vs-poco/ DTO vs Value Object vs ...
- [No0000129]WPF(1/7)开始教程[译]
概要 在我使用了半年多 WPF 后,是时候写点关于 WPF 基础方面的东西了.我发表了一系列针对具体问题的文章.现在是到了让大家明白为什么说WPF是一款在界面开发上带来革命的产品了. 本文针对初级-中 ...
随机推荐
- layui学习地址
--layui学习地址 ,相当之好用,非常感谢为我们工作和学习提供方便的才子们,谢谢~https://www.layui.com/demo/layim.html
- C++面向对象实践
实践如下: class Person{ private: int age; ]; int hight; public: Person(int age, int hight, char* name); ...
- WireShark简单使用以及TCP三次握手
最近一直在用C#编写服务器端的程序,苦于一直找不到合适的方法来测试网络程序,这篇文章很好的解释了网络程序的底层实现. WireShark是最好的学习网络协议最好的工具. wireshark介绍 wir ...
- IIS部署网站 HTTP 错误 500.21 - Internal Server Error
HTTP 错误 500.21 - Internal Server Error处理程序“PageHandlerFactory-Integrated”在其模块列表中有一个错误模块“ManagedPipel ...
- 关于FPS游戏的设计问题
第一个想到的问题: 首先以unity的FPSCharactorController为例,这里规定,相机的方向中心一定是瞄准的方向中心.设置身体的扭曲朝向相机方向,这样身体可以弯腰.你们看图中,我让玩家 ...
- Java内部类介绍
在Java中,内部类包括:成员内部类(静态内部类.非静态内部类).匿名内部类.局部内部类(几乎不用). 1.成员内部类: 1.1非静态成员内部类 public class InnerClassTest ...
- 【Python开发】【神经网络与深度学习】如何利用Python写简单网络爬虫
平时没事喜欢看看freebuf的文章,今天在看文章的时候,无线网总是时断时续,于是自己心血来潮就动手写了这个网络爬虫,将页面保存下来方便查看 先分析网站内容,红色部分即是网站文章内容div,可以看 ...
- 2019JAVA第五次实验报告
Java实验报告 班级 计科二班 学号 20188442 姓名 吴怡君 完成时间2019/10/11 评分等级 实验四 类的继承 实验目的 理解抽象类与接口的使用: 了解包的作用,掌握包的设计方法. ...
- spring boot-7.日志系统
日志系统分为两部分,一部分是日志抽象层,一部分是日志实现层.常见的日志抽象层JCL,SLF4J,JBoss-Logging,日志实现层有logback,log4j,log4j2,JUL.日志抽象层的功 ...
- 小记------查看‘阿里云机器’yarn 日志
通过ip:8088 页面 复制正在运行的application ID 在linux客户端执行 xshell yarn logs -applicationId application_155869 ...