当我发现把最初的一个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. Server.Transfer 和 Response.Redirect 用法区别

    在ASP.NET中,在后台传值方式目前大多都是用 Response.Redirect("页面地址") 来重定向页面的,但是现在还有一种方式也可以达到重定向页面的作用,而且在某些时刻 ...

  2. opencv算法学习

    1.改变图像的亮度和对比度: 算法介绍:对每一点像素值的r,g,b,值进行乘法和加法的运算. 代码使用: ; y < image.rows; y++ ) { ; x < image.col ...

  3. 如何在Windows 2003+IIS6的环境下找回应用程序池(application pool)中的服务账号密码

    上一篇文章说了说如何在Win2008+iis7中取出SharePoint管理账号密码的方法. 整个过程简单的讲,就是通过使用要找回密码的账号用来在SharePoint中创建一个临时的Web Appli ...

  4. 在一个SQL Server表中的多个列找出最大值

    在一个SQL Server表中一行的多个列找出最大值 有时候我们需要从多个相同的列里(这些列的数据类型相同)找出最大的那个值,并显示 这里给出一个例子 IF (OBJECT_ID('tempdb..# ...

  5. ArcGIS之Cartogram地图变形记

    一.地图会说谎 地图作为真实世界的抽象,是“用图说话”最可靠的工具,但是有的时候地图也会撒一些小小的谎言,其中最著名的例子当属美国总统大选.如图1是2012年美国总统大选后网上给出的一个结果图,红色代 ...

  6. springboot之HelloWorld

    简介 为了简化开发Spring的复杂度,Spring提供了SpringBoot可以快速开发一个应用,这里就简单介绍下SpringBoot如何快速开发一个J2EE应用 HelloWorld 首先在gra ...

  7. UWP自动填充控件AutoSuggestBox小优化

    UWP提供的AutoSuggestBox本身非常好用,在项目中经常用到,但是当我们使用时发现一下不人性化的设置,例子1如下: <Page x:Class="SelfInkCanvas. ...

  8. GitHub iOS-Top 100 简介

    GitHub排名前100的iOS第三方汇总简介,方便开发者选择适合的第三方框架. 项目名称 项目信息 1. AFNetworking 作者是 NSHipster 的博主, iOS 开发界的大神级人物, ...

  9. 学习nodejs有感

    接触nodejs一段时间了,不断的去接触接触,nodejs是一个能让前端程序员做后台开发的一项技术.  随着学习,让我更好的理解了前后端,以及浏览器是如何运作的

  10. SpringAOP之动态代理

    一.动态代理: 1.在原有的静态代理的基础上进一步的完善,由于静态代理中,重复写了相同的代码使得代码的整体结构显得冗余,而且还不同的核心类还需要有不用的代理类,是写死了的具体的类.所以需要使用动态代理 ...