使用SpringBoot入门案例
一、创建项目
今天给大家讲一个SpringBoot的一个入门案例,让更多人能用起SpringBoot来。假设项目名为MyProject项目,并添加MyProject-Web的子模块。
二、给根项目MyProject的pom.xml,加入parent节点(spring-boot-starter-parent)
<!--Add Spring boot Parent-->
<parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>2.0.2.RELEASE</version>
</parent>
三、为子模块MyProject-Web的pom.xml添加下列依赖
<dependencies>
<!--SpringBoot核心模块,包括自动配置支持、日志和YAML-->
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter</artifactId>
</dependency>
<!--Web模块-->
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
<!--测试模块,包括JUnit、Hamcrest、Mockito-->
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-test</artifactId>
</dependency>
<!--加载yml配置文件使用。-->
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-configuration-processor</artifactId>
</dependency>
<!--自动Get、Set-->
<dependency>
<groupId>org.projectlombok</groupId>
<artifactId>lombok</artifactId>
</dependency>
</dependencies>
四、添加项目包,整理项目结构
说明,子模块的pom中加入的依赖项:spring-boot-starter-test、spring-boot-configuration-processor、lombok不是必需的。因为演示例子中,加入了对yml配置转换为封装实体的样例。所以加入了spring-boot-configuration-processor、lombok。
五、在build包下加入ApplicationServer.java的结构如下:
package com.test.build; import org.springframework.boot.Banner;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.context.annotation.ComponentScan;
import org.springframework.context.annotation.PropertySource; @ComponentScan({"com.test"})
@SpringBootApplication
@PropertySource({"classpath:config/resource.properties"})
public class ApplicationServer {
public static void main(String[] args) {
SpringApplication application = new SpringApplication(ApplicationServer.class);
application.setBannerMode(Banner.Mode.OFF);//关闭启动时的Banner展示
application.run(args); //等价用法
//SpringApplication.run(ApplicationServer.class, args);
}
}
六、在resources/application.yml中结构如下:
server:
address: 0.0.0.0
port: 8080
servlet:
context-path: /app
tomcat:
uri-encoding: utf-8 constant:
company: testttttt
address: 北京市上地九街数码科技广场XXXXXX
七、在resources/config/resource.properties中结构如下:
person.name: Sindrol
八、在constant常量文件夹下ConstantSetting.java中结构如下:
package com.test.constant; import lombok.Data;
import org.springframework.boot.context.properties.ConfigurationProperties; @Data
@ConfigurationProperties(prefix = "constant")
public class ConstantSetting {
private String company;
private String address;
}
注意,这里使用的@ConfigurationProperties用来标注一个类,上文中将yml中前缀为constant的值映射到此实体中,它需要spring-boot-configuration-processor包依赖。@Data 是为实体类自动生成get、set。
九、在config夹中ApplicationConfig.java类中,内容如下:
package com.test.config; import com.test.constant.ConstantSetting;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration; @Configuration
public class ApplicationConfig {
public ApplicationConfig() {
System.out.println("ApplicationConfig inited ...");
} @Bean
public ConstantSetting constantSetting() {
return new ConstantSetting();
}
}
说明:@Configuration表示一个Spring的xml配置文件。@Bean表示Spring的配置文件中的Bean配置。
十、简单调用
在domain中添加一实体:TestEntry.java,内容如下:
package com.test.domain; import lombok.Data; @Data
public class TestEntry {
private Integer id;
private String name;
}
在controller中建一个HelloController控制器,然后内容如下:
package com.test.controller; import com.test.constant.ConstantSetting;
import com.test.domain.TestEntry;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.lang.Nullable;
import org.springframework.mock.web.MockMultipartFile;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.context.annotation.RequestScope;
import org.springframework.web.multipart.MultipartFile; import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.lang.ref.ReferenceQueue;
import java.nio.file.Files;
import java.util.List; @RestController
public class HelloController { /*
* 在ApplicationServer中使用了PropertySource配置了properties源后,从相应的源如resource.properties中加载值。
* */
@Value("${person.name}")
private String name; /*
* Autowired区别于Resource的不同处在于,Autowired是按类别导入,如果想指定名称,可以配合Qualifier使用。
* */
// @Autowired
// @Qualifier("constantSetting")
@Resource
private ConstantSetting constantSetting; /*
* 最简单的使用
* */
@RequestMapping("/hello")
public String hello() {
return "hello " + name + " !" + " from company " + constantSetting.getCompany();
} /*
* 系统自动直接将实体序列化为JSON返回。
* */
@RequestMapping("/constant")
@ResponseBody //相当于把结果JSON处理后返回。
public ConstantSetting constant() {
return constantSetting;
} /*
* 当使用Post请求,Content-Type=application/json调用测试: {"id":1,"name":"Sindrol"}
* */
@RequestMapping(value = "/test", method = RequestMethod.POST)
@ResponseBody
public TestEntry constant(@RequestBody TestEntry test) {
return test;
} /*
* 当使用Get请求
* */
@GetMapping(value = "/test")
@ResponseBody
public TestEntry constant(@RequestParam int id, @RequestParam String name) {
TestEntry test = new TestEntry();
test.setId(id);
test.setName(name);
return test;
} /*
* 当使用Get请求,并设置响应状态
* */
@GetMapping(value = "/request")
@ResponseBody
public TestEntry request(HttpServletRequest request, HttpServletResponse response, @RequestParam int id, @RequestParam String name) {
response.setStatus(500);
TestEntry test = new TestEntry();
test.setId(id);
test.setName(name);
return test;
} /*
* 当使用Post请求表单类型,传送的值可空
* */
@PostMapping(value = "/form")
@ResponseBody
public TestEntry form(@Nullable @RequestParam("id") Integer id, @Nullable @RequestParam("name") String name) {
TestEntry test = new TestEntry();
test.setId(id);
test.setName(name);
return test;
} /*
* 当使用Post请求form-data表单类型,注意在使用PostMan测试时,不要添加Content-Type头,会影响上传时真正使用的Content-Type的
* */
@PostMapping(value = "/file")
public String form(@RequestParam("filename") MultipartFile file) {
return "fileName:" + file.getOriginalFilename() + " fileSize:" + file.getSize() + " contentType:" + file.getContentType();
}
}
十一、运行ApplicationServer.java中的Main函数,并在浏览器中访问 http://127.0.0.1:8080/app/hello 即可看到效果.(中间的app虚拟应用名称是在application.yml中的server.servlet.context-path节点配置的)
使用SpringBoot入门案例的更多相关文章
- springboot入门案例----eclipse编写第一个springboot程序
对于刚入门的springboot的新手来说,学的过程中碰到的一些问题记录下. 首先,配置好Maven环境及本地仓库 之后进入Maven安装目录conf文件夹下的settings.xml配置文件,用No ...
- SpringBoot入门案例——创建maven Module方式
最近看到一个大牛写的spring boot案例,链接贴这 https://github.com/ityouknow/spring-boot-examples.git 这里通过在maven里创建多个mo ...
- SpringBoot快速入门(解析+入门案例源码实现)
这里写目录标题 SpringBoot入门 一.SpringBoot 概念 二.JavaConfig 入门 1. JavaConfig 概念 2. 项目准备 三.常用注解 四.SpringBoot 入门 ...
- spring-cloud-Zuul学习(一)【基础篇】--入门案例【重新定义spring cloud实践】
-- 2 ...
- springcloud+eureka简单入门案例
springcloud+eureka简单入门案例 一.服务提供者 直接提供服务,入门案例没有特别要设置的地方,注意下端口,由于要启动多个服务,可能会冲突 配置文件(src/main/resources ...
- Springboot入门及配置文件介绍(内置属性、自定义属性、属性封装类)
目的: 1.Springboot入门 SpringBoot是什么? 使用Idea配置SpringBoo使用t项目 测试案例 2.Springboot配置文件介绍 内置属性 自定义属性 属性封装类 Sp ...
- SpringBoot入门及深入
一:SpringBoot简介 当前互联网后端开发中,JavaEE占据了主导地位.对JavaEE开发,首选框架是Spring框架.在传统的Spring开发中,需要使用大量的与业务无关的XML配置才能使S ...
- (十八)整合Nacos组件,环境搭建和入门案例详解
整合Nacos组件,环境搭建和入门案例详解 1.Nacos基础简介 1.1 关键特性 1.2 专业术语解释 1.3 Nacos生态圈 2.SpringBoot整合Nacos 2.1 新建配置 2.2 ...
- Springboot 入门及Demo
一:SpringBoot入门1.1:SpringBoot简介Spring Boot是由Pivotal团队提供的全新框架,其设计目的是用来简化新Spring应用的初始搭建以及开发过程.该框架使用了特定的 ...
随机推荐
- ie7 下 float换行问题与vertical-align:middle; 失效问题
声明:web小白的笔记,欢迎大神指点!联系QQ:1522025433. ie7 下 float换行问题 请直接看代码中和代码中的注释: <!doctype html> <html&g ...
- 【动态规划】Part1
1. 硬币找零 题目描述:假设有几种硬币,如1.3.5,并且数量无限.请找出能够组成某个数目的找零所使用最少的硬币数. 分析: dp [0] = 0 dp [1] = 1 + ...
- ubuntu ufw防火墙软件的配置入门
顺便,一条龙作完安全吧. ufw的使用,是比iptables简单.但只能作简单的事儿,更改简单的netfilter里的iptable里的记录.难点的,可能还是得iptables原生命令. 自打2.4版 ...
- Delphi---TServerSocket和TClientSocket发送和接收大数据包
https://www.cnblogs.com/zhangzhifeng/p/6065244.html TServerSocket和TClientSocket用非阻塞模式发送和接收比较大的数据时,可能 ...
- 【Java】 剑指offer(67) 把字符串转换成整数
本文参考自<剑指offer>一书,代码采用Java语言. 更多:<剑指Offer>Java实现合集 题目 请你写一个函数StrToInt,实现把字符串转换成整数这个功能 ...
- hdu1698 Just a Hook 【区间修改】(模板题)
题目链接:https://vjudge.net/contest/182746#problem/E 题目大意: 一段线段由n条小线段组成,每次操作把一个区间的小线段变成金银铜之一(金的价值为3,银为2, ...
- POJ 2250 Compromise【LCS】+输出路径
题目链接:https://vjudge.net/problem/POJ-2250 题目大意:给出n组case,每组case由两部分组成,分别包含若干个单词,都以“#”当结束标志,要求输出最长子序列. ...
- pyrhon SQLite数据库
pyrhon SQLite数据库 目录 介绍 导入模块 创建数据库/打开数据库 创建表 在表中插入行 查询/修改 删除表中的行 删除表 介绍 Python SQLITE数据库是一款非常小巧的嵌入式开源 ...
- long long or int
long long or int 很多时候long long爆空间,int有时又不够 . 在算乘法的时候,要保证乘出来的中间项也不爆long long
- Mysql学习(一)添加一个新的用户并用golang操作Mysql
Mysql添加一个新的用户并赋予权限 添加一个自己的用户到mysql 首先我们需要先用root用户登录mysql,但是刚安装完没有密码,我们先跳过密码 ailumiyana@ailumiyana:~/ ...