1、springBoot使用文档10V10SpringBoot使用文档说明:一、SpringBoot简介1.1 SpringBoot自述世界上最好的文档来源自官方的Spring Boot Reference Guide,是这样介绍的:Spring Boot makes it easy to create stand-alone, production-grade Spring based Applications that you can “just run”.Most Spring Boot applications need very little Spring configuration.S
2、pring Boot(英文中是“引导”的意思),是用来简化Spring应用的搭建到开发的过程。应用开箱即用,只要通过 “just run”(可能是 java -jar 或 tomcat 或 maven插件run 或 shell脚本),就可以启动项目。二者,Spring Boot 只要很少的Spring配置文件(例如那些xml,property)。因为“习惯优先于配置”的原则,使得Spring Boot在快速开发应用和微服务架构实践中得到广泛应用。1.2 SpringBoot特点1.3 SpringBoot环境支持二、SpringBoot入门案例-HelloSpringBoot2.1 Hello
3、Word案例使用maven创建一个基础web项目添加pom.xml配置如下:4.0.0springbootspringboot-helloworld0.0.1-SNAPSHOTspringboot-helloworld : HelloWorld Demoorg.springframework.bootspring-boot-starter-parent1.3.3.RELEASEorg.springframework.bootspring-boot-starter-webjunitjunit4.122.2 创建应用启动类/* * 程序入口 * author Administrator */Spr
4、ingBootApplicationpublic class HelloWordApp public static void main(String args) SpringApplication.run(HelloWordApp.class, args); SpringBootApplication注解是springboot应用的标识,表明该应用是springboot应用,main方法为该应用程序的入口,可以直接运行此main方法,通过启用内置的Tomcat并初始化spring环境及各组件;2.3 controller层的编写/* * springboot案例-HelloWord * aut
5、hor Administrator */RestControllerpublic class HelloController RequestMapping(/) public String helloWord() return Hello Word ! Hello Spring Boot!; RestController和RequestMapping注解是来自SpringMVC的注解,它们不是SpringBoot的特定部分。1. RestController:提供实现了REST API,可以服务JSON,XML或者其他。这里是以String的形式渲染出结果。2. RequestMapping:
6、提供路由信息,/“路径的HTTP Request都会被映射到helloWord方法进行处理。2.4 启动与访问启动项目访问内置tomcat启动后默认端口8080,通过浏览器访问http:/localhost:8080即可实现访问;三、SpringBoot整合fastjsonSpringBoot默认使用jackson作为json解析工具;pom.xml org.springframework.boot spring-boot-starter-web com.fasterxml.jackson.core jackson-databind com.alibaba fastjson Java代码如下:
7、/* * 配置SpringBoot支持FastJson有两种方法; * 第一种:覆写configureMessageConverters的方式完成对fastjson的配置支持; * 1.1启动类extend WebMvcConfigurerAdapter,1.2覆写configureMessageConverters方法 * 第二种:使用Bean注入的方式配置Boot对fastjson的支持(推荐); * 2.1编写相关配置;2.2注入带有fastjson配置信息的HttpMessageConverters; * author SorryNO4 */SpringBootApplicationp
8、ublic class FastJsonApp extends WebMvcConfigurerAdapter public static void main(String args) SpringApplication.run(FastJsonApp.class, args); /* * 1.启动类extend WebMvcConfigurerAdapter * * 2.覆写configureMessageConverters方法 */ Override public void configureMessageConverters(ListHttpMessageConverter conve
9、rters) super.configureMessageConverters(converters); / 初始化转换器 FastJsonHttpMessageConverter4 fastJsonHttpMessageConverter4 = new FastJsonHttpMessageConverter4(); / 初始化一个转换器配置 FastJsonConfig fastJsonConfig = new FastJsonConfig(); / 设置fastjson的配置信息,如设置是否格式化返回信息 fastJsonConfig.setSerializerFeatures(Seri
10、alizerFeature.PrettyFormat); / 配置转换器 fastJsonHttpMessageConverter4.setFastJsonConfig(fastJsonConfig); / 添加转换器到转换器list中 converters.add(fastJsonHttpMessageConverter4); /* * 第二种方式添加对fastjson的支持 * return */ /*Bean public HttpMessageConverters fastJsonHttpMessageConverters() FastJsonHttpMessageConverter4
11、 fastJsonHttpMessageConverter4 = new FastJsonHttpMessageConverter4(); FastJsonConfig fastJsonConfig = new FastJsonConfig(); fastJsonConfig.setSerializerFeatures(SerializerFeature.PrettyFormat); fastJsonHttpMessageConverter4.setFastJsonConfig(fastJsonConfig); HttpMessageConverter converter4 = fastJso
12、nHttpMessageConverter4; HttpMessageConverters httpMessageConverters = new HttpMessageConverters(converter4); return httpMessageConverters; */使用fastjson解析工具后就可以使用fastjson工具的特性/* * 测试fastjson的实体类; * 添加了对fastjson的支持后就可以使用其特性;如 JSONField(format = yyyy-MM-dd HH24:mm) * author SorryNO4 */public class Fast
13、JsonVo private Integer id; /* 设置不序列化的字段 */ JSONField(serialize = false) private String msg; /* 时间格式化 */ JSONField(format = yyyy-MM-dd HH24:mm) private Date creatrtime; Getter and setter.ControllerRestControllerRequestMapping(/)public class FastJsonController /* * 测试SpringBoot对fastjson的支持 * http:/loc
14、alhost:8080/fastjsonvo * return FastJsonVo as json * creatrtime:1508001089654, * id:1002,msg:测试SpringBoot对FastJson的支持! */ RequestMapping(fastjsonvo) public FastJsonVo useFsatJson() FastJsonVo vo = new FastJsonVo(); vo.setCreatrtime(new Date(); vo.setId(1002); vo.setMsg(测试SpringBoot对FastJson的支持!); re
15、turn vo; 四、SpringBoot整合springboot-freemarkerpom.xml org.springframework.boot spring-boot-starter-web org.springframework.boot spring-boot-starter-freemarker application.properties#freemakerspring.freemarker.allow-request-override=falsespring.freemarker.cache=falsespring.freemarker.check-template-loc
16、ation=truespring.freemarker.charset=UTF-8spring.freemarker.content-type=text/htmlspring.freemarker.expose-request-attributes=falsespring.freemarker.expose-session-attributes=falsespring.freemarker.expose-spring-macro-helpers=false#spring.freemarker.prefix=#spring.freemarker.suffix=#spring.freemarker
17、.request-context-attribute=#spring.freemarker.template-loader-path=#spring.freemarker.view-names=Templates/myftl.ftl Hello freemarker ! Hello $name !controllerControllerpublic class FreemarkerController RequestMapping(/myftl) public ModelAndView myFty(HashMap map) ModelAndView view = new ModelAndVie
18、w(myftl); map.put(name, jack); return view; 五、SpringBoot整合thymeleafpom.xml org.springframework.boot spring-boot-starter-web org.springframework.boot spring-boot-starter-thymeleaf application.properties#thymeleaf#关闭缓存,开发过程中关闭缓存spring.thymeleaf.cache=false#spring.thymeleaf.prefix=classpath:/templates/
19、#spring.thymeleaf.suffix=#spring.thymeleaf.mode=HTML5#spring.thymeleaf.encoding=UTF-8#spring.thymeleaf.content-type=text/html; charset=utf-8Templates/one.html Hello thymeleaf ! Hello !controllerControllerpublic class TemplatesController RequestMapping(/one) public ModelAndView theOne(HashMap map) Mo
20、delAndView view = new ModelAndView(one); map.put(name, tom); return view; 六、SpringBoot整合jspSpringboot提供对jsp的支持,注意打包方式为war,项目最好具有web结构,在springboot中不建议使用jsp,目前对tomcat7服务器支持较好,对其他的服务器的支持较弱,建议使用thymeleaf渲染数据;Pom.xmlwar org.springframework.boot spring-boot-starter-web javax.servlet jstl org.apache.tomcat
21、.embed tomcat-embed-jasper provided application.propertiesspring.mvc.view.prefix=/WEB-INF/jsp/spring.mvc.view.suffix=.jspserver.port=8082controllerRestControllerRequestMapping(/)public class JspController RequestMapping(index) public ModelAndView showMsg(HashMap map) ModelAndView view = new ModelAnd
22、View(index); UserForJsp userForJsp = new UserForJsp(); userForJsp.setId(user001); userForJsp.setUsername(MAXtom); userForJsp.setPassword(123); map.put(name, Tom); map.put(user, userForJsp); return view; 工程结构七、SpringBoot整合Oracle数据库Springboot整合Oracle数据库,springboot本身集成了springdata,对持久层技术的选择很多,如hibernate,springdata,jdbctemplates等,还可以集成mybatis等持久层框架;Pom.xml org.springframework.boot spring-boot-starter-web org.springfra
copyright@ 2008-2022 冰豆网网站版权所有
经营许可证编号:鄂ICP备2022015515号-1