当我发现把最初的一个demo整的面目全非的时候,突然想要找一个简单的demo做测试,发现与其在原来的上面该,还不如新建一个demo。

官方入门:http://projects.spring.io/spring-boot/

最熟悉maven,这次先做一个maven的demo。

创建maven project。

pom:

<?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.test</groupId>
<artifactId>demo</artifactId>
<version>1.0-SNAPSHOT</version> <repositories>
<!--<repository>-->
<!--<id>spring-snapshots</id>-->
<!--<url>http://maven.oschina.net/content/groups/public/</url>-->
<!--<snapshots>-->
<!--<enabled>true</enabled>-->
<!--</snapshots>-->
<!--</repository>-->
<repository>
<id>springsource-repos</id>
<name>SpringSource Repository</name>
<url>http://repo.spring.io/release/</url>
</repository>
<repository>
<id>central-repos</id>
<name>Central Repository</name>
<url>http://repo.maven.apache.org/maven2</url>
</repository>
<repository>
<id>central-repos2</id>
<name>Central Repository 2</name>
<url>http://repo1.maven.org/maven2/</url>
</repository>
</repositories> <dependencyManagement>
<dependencies>
<dependency>
<!-- Import dependency management from Spring Boot -->
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-dependencies</artifactId>
<version>1.4.0.RELEASE</version>
<type>pom</type>
<scope>import</scope>
</dependency>
</dependencies>
</dependencyManagement>
<dependencies>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-devtools</artifactId>
<optional>true</optional>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-thymeleaf</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-security</artifactId>
</dependency>
</dependencies> <build>
<plugins>
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
<configuration>
<fork>true</fork>
</configuration>
<dependencies>
<!-- spring热部署-->
<dependency>
<groupId>org.springframework</groupId>
<artifactId>springloaded</artifactId>
<version>1.2.6.RELEASE</version>
</dependency>
</dependencies>
</plugin>
</plugins>
</build>
</project>

这里添加了web基础依赖,模板为thymeleaf, 添加一个简单的security配置。

创建src/resources/application.yml:

spring:
profiles:
active: dev

创建src/resources/application-dev.yml:

#用户名 密码配置
security:
user:
name: admin
password: test

创建启动入口hello.SampleController:

package hello;

import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping; @Controller
@SpringBootApplication
public class SampleController { @RequestMapping("/")
public String home() {
return "redirect:/hello/index";
} public static void main(String[] args) throws Exception {
SpringApplication.run(SampleController.class, args);
}
}

创建一个Controller: hello.HelloController

package hello;

import hello.entity.User;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.validation.Errors;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody; import javax.validation.Valid;
import java.util.HashMap;
import java.util.Map; /**
* Created by miaorf on 2016/8/2.
*/
@RequestMapping("/hello")
@Controller
public class HelloController { @RequestMapping("/index")
public String hello(Model model, @RequestParam(defaultValue = "Ryan") String name){
model.addAttribute("name",name);
return "index";
} @ResponseBody
@RequestMapping("/info")
public Map info(@Valid @ModelAttribute("user")User user, Errors errors){ Map map = new HashMap();
if (errors.hasErrors()){
map.put("error",errors.getAllErrors());
}else{
map.put("user",user);
} return map;
}
}

这里连个路由。一个指向模板文件index,一个返回json并添加了参数校验。

需要使用的实体类:hello.entity.User

package hello.entity;

import org.hibernate.validator.constraints.Length;

import javax.validation.constraints.NotNull;
import javax.validation.constraints.Pattern; /**
* Created by miaorf on 2016/8/2.
*/
public class User {
@NotNull(message = "用户名不能为空")
@Length(min=5, max=20, message="用户名长度必须在5-20之间")
@Pattern(regexp = "^[a-zA-Z_]\\w{4,19}$", message = "用户名必须以字母下划线开头,可由字母数字下划线组成")
private String name;
@NotNull(message = "age不能为空")
private int age;
@NotNull(message = "sex不能为空")
private String sex; 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;
} public String getSex() {
return sex;
} public void setSex(String sex) {
this.sex = sex;
}
}

需要用到的模板文件:src/resources/templates/index.html

<!DOCTYPE HTML>
<html xmlns:th="http://www.thymeleaf.org">
<head>
<title>Getting Started: Serving Web Content</title>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8" />
</head>
<body>
<p th:text="'Hello, ' + ${name} + '!'" />
</body>
</html>

github: https://github.com/chenxing12/spring-boot-demo

spring-boot - demo的更多相关文章

  1. Spring Boot demo系列(二):简单三层架构Web应用

    2021.2.24 更新 1 概述 这是Spring Boot的第二个Demo,一个只有三层架构的极简Web应用,持久层使用的是MyBatis. 2 架构 一个最简单的Spring Boot Web应 ...

  2. Spring Boot demo系列(十):Redis缓存

    1 概述 本文演示了如何在Spring Boot中将Redis作为缓存使用,具体的内容包括: 环境搭建 项目搭建 测试 2 环境 Redis MySQL MyBatis Plus 3 Redis安装 ...

  3. Spring Boot demo系列(九):Jasypt

    2021.2.24 更新 1 概述 Jasypt是一个加密库,Github上有一个集成了Jasypt的Spring Boot库,叫jasypt-spring-boot,本文演示了如何使用该库对配置文件 ...

  4. Spring Boot demo系列(六):HTTPS

    2021.2.24 更新 1 概述 本文演示了如何给Spring Boot应用加上HTTPS的过程. 2 证书 虽然证书能自己生成,使用JDK自带的keytool即可,但是生产环境是不可能使用自己生成 ...

  5. Spring Boot demo系列(五):Docker部署

    2021.2.24 更新 1 概述 本文讲述了如何使用Docker部署Spring Boot应用,首先介绍了Docker的安装过程,接着介绍了Docker的一些基础知识,最后讲述了Dockerfile ...

  6. Spring Boot demo系列(四):Spring Web+Validation

    2021.2.24 更新 1 概述 本文主要讲述了如何使用Hibernate Validator以及@Valid/@Validate注解. 2 校验 对于一个普通的Spring Boot应用,经常可以 ...

  7. Spring Boot demo系列(一):Hello World

    2021.2.24 更新 1 新建工程 打开IDEA选择新建工程并选择Spring Initializer: 可以在Project JDK处选择JDK版本,下一步是选择包名,语言,构建工具以及打包工具 ...

  8. idea 启动官网spring boot demo 报错

    *************************** APPLICATION FAILED TO START *************************** Description: Fai ...

  9. Spring Boot demo系列(三):Spring Web+MyBatis Plus

    2021.2.24 更新 1 概述 Spring Web+MyBatis Plus的一个Demo,内容和上一篇类似,因此重点放在MyBatis Plus这里. 2 dao层 MyBatis Plus相 ...

  10. Spring Boot demo系列(八):Swagger

    2021.2.24 更新 1 概述 Swagger主要用于生成API文档,本文演示了如何使用目前最新的OpenAPI3以及Swagger来进行接口文档的生成. 2 依赖 <dependency& ...

随机推荐

  1. Nginx 配置从零开始

    作为一个 nginx 的初学者记录一下从零起步的点滴. 基本概念 Nginx 最常的用途是提供反向代理服务,那么什么反向代理呢?正向代理相信很多大陆同胞都在这片神奇的土地上用过了,原理大致如下图: 代 ...

  2. 初识 Html5

    1.1认识HTML5 HTML5并不仅仅只是做为HTML标记语言的一个最新版本,更重要的是它制定了Web应用开发的一系列标准,成为第一个将Web做为应用开发平台的HTML语言. HTML5定义了一系列 ...

  3. The Solution of UESTC 2016 Summer Training #1 Div.2 Problem B

    Link http://acm.hust.edu.cn/vjudge/contest/121539#problem/B Description standard input/output Althou ...

  4. canvas初探2

    2.2 canvas的绘图环境 canvas仅仅只是一个绘图的容器,其内存在一个绘图环境,该环境对象提供了全部的绘图功能. 目前canvas的绘图环境是2d,但canvas规范在着手准备支持其他类型的 ...

  5. python字符串的使用

    之前在网上看了关于python最基础的一些教程,看着都通俗易懂,但是在写的过程中却感觉还是很生涩.关于字符串的使用还是应该多写多练!如何将“teacher_id = 123 #老师ID”转换成字典或者 ...

  6. Windows系统上的.Net版本和.NETFramework的C#版本

    前言 注:本文内容摘自维基百科,用于在墙内时当作笔记看. WinForm 需要.Net最低版本 2.0 WPF需要的.Net最低版本 3.0 (Win7及之上版本自带) C#版本 版本 语言规格 日期 ...

  7. .Net开发笔记(十七) 应用程序扩展

    在很多场合,我们需要在已有软件程序上增加一些新的功能,几乎所有原因是因为原有软件功能不能满足我们的需要,我们平时做的插件就属于这种情况,最常见的是VS IDE的插件开发,网上老外写的一篇关于插件开发的 ...

  8. 修改Hosts为何不生效,是DNS缓存?

    Update: 如果浏览器使用了代理工具,修改 Hosts 也不会生效.这里是因为,浏览器会优先考虑代理工具(如添加 pac 文件.SwitchySharp等)的代理,建议调试的时候先关闭这些代理. ...

  9. 在职场中混,"讲演稿"的重要性

    背景: 在职场上工作的人,思维都像流水行云一样无拘无束.某时某刻说不定就会产生点“头脑风暴”. 但是在多数情总下,只是自己想想,跟朋友吹瞌子的时候随便说说,很少有用文字,图形把自己的思维给表现出来. ...

  10. Web3DGame之路,Babylonjs 和TypeScript学习笔记(二)

    先来认识一下Babylonjs,由于基于webgl来开发,所以先介绍一下基础知识. Webgl是一个html标准,他要在canvas元素上初始化. Html页面上的准备 所以我们先从html页面开始看 ...