一 、Hello案例
1.1 创建maven项目
上一节,我已经学会了手动创建springboot项目。接下来我们用官方的方式来创建
填好红色部分内容,选择需要的粉红色部分内容,点击绿色箭头下载生成的,解压后导入到工作空间。
pom文件的依赖
<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.itcast</groupId>
<artifactId>springboot-hello-03</artifactId>
<version>0.0.1-SNAPSHOT</version>
<packaging>war</packaging>
<description>Demo project for Spring Boot</description>
<!-- 引入springboot父类依赖 -->
<parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>2.0.2.RELEASE</version>
<relativePath /> <!-- lookup parent from repository -->
</parent>
<properties>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
<project.reporting.outputEncoding>UTF-8</project.reporting.outputEncoding>
<java.version>1.8</java.version>
</properties>
<dependencies>
<!-- spring boot starter -->
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter</artifactId>
</dependency>
<!-- spring-boot-configuration-processor -->
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-configuration-processor</artifactId>
<optional>true</optional>
</dependency>
<!-- spring boot starter-test -->
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-test</artifactId>
<scope>test</scope>
</dependency>
<!-- spring boot starter-web -->
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
</dependencies>
<build>
<plugins>
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
</plugin>
</plugins>
</build>
</project>
1.2 修改入口类的名称,我改成自己喜欢的。生成的太长
package com.itcast;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
@SpringBootApplication
public class ItcastApplication {
public static void main(String[] args) {
SpringApplication.run(ItcastApplication.class, args);
}
}
1.3创建hello控制器HelloController
package com.itcast.controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
/**
* 访问路径:http://localhost:8080/hello
* @author jack
*
*/
@RestController
public class HelloController {
@RequestMapping("/hello")
public Object hello(){
return "hello springboot";
}
}
1.4 启动入口类,访问:http://localhost:8080/hello
二 、返回json案例
2.1 创建一个实体类User
package com.itcast.entity;
import java.util.Date;
public class User {
private String name;
private String password;
private Integer age;
private Date birthday;
private String desc;
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public String getPassword() {
return password;
}
public void setPassword(String password) {
this.password = password;
}
public Integer getAge() {
return age;
}
public void setAge(Integer age) {
this.age = age;
}
public Date getBirthday() {
return birthday;
}
public void setBirthday(Date birthday) {
this.birthday = birthday;
}
public String getDesc() {
return desc;
}
public void setDesc(String desc) {
this.desc = desc;
}
@Override
public String toString() {
return "User [name=" + name + ", password=" + password + ", age=" + age + ", birthday=" + birthday + ", desc="
+ desc + "]";
}
}
2.2编写控制层类 UserController
package com.itcast.controller;
import java.util.Date;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.bind.annotation.RestController;
import com.itcast.entity.User;
/**
* 访问路径:http://localhost:8080/User/getUser
* @RestController=@Controller+@ResponseBody 返回json
* @RestController+@ResponseBody返回json
* @author jack
*
*/
@Controller
//@RestController
@RequestMapping("/User")
public class UserController {
@RequestMapping("/getUser")
@ResponseBody
public User getUser(){
User user=new User();
user.setName("花狐貂");
user.setPassword("123456");
user.setAge(23);
user.setBirthday(new Date());
user.setDesc("帅哥");
return user;
}
}
2.3启动入口类访问,访问路径:http://localhost:8080/User/getUser
注意:经测试@RestController=@Controller+@ResponseBody 返回json,@RestController+@ResponseBody返回json
三 读取外部资源文件案例
3.1 创建资源配置文件resource.properties,放入classpath路径中
com.itcast.openresource.name=springboot
com.itcast.openresource.website=www.itcast.com
com.itcast.openresource.language=java
3.2创建资源类对象Resource
package com.itcast.entity;
import org.springframework.boot.context.properties.ConfigurationProperties;
import org.springframework.context.annotation.Configuration;
import org.springframework.context.annotation.PropertySource;
@Configuration
@ConfigurationProperties(prefix="com.itcast.openresource")
@PropertySource(value="classpath:resource.properties")
//@PropertySources(value={"classpath:resource.properties"})
public class Resource {
private String name;
private String website;
private String language;
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public String getWebsite() {
return website;
}
public void setWebsite(String website) {
this.website = website;
}
public String getLanguage() {
return language;
}
public void setLanguage(String language) {
this.language = language;
}
}
3.3 创建资源控制器类ResourceController
package com.itcast.controller;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import com.itcast.entity.Resource;
/**
* 访问路径:http://localhost:8080/Resource/getResource
* @author jack
*
*/
@RestController
@RequestMapping("/Resource")
public class ResourceController {
@Autowired
private Resource resource;
@RequestMapping("/getResource")
public Resource getResource(){
Resource source=new Resource();
BeanUtils.copyProperties(resource, source);
return source;
}
}
3.4 启动入口类访问 访问路径:http://localhost:8080/Resource/getResource
四 ,springboot整合模板引擎freemarker
4.1 新建maven项目,引入freemarker依赖
<!-- spring boot starter-freemarker freemarker模板依赖 -->
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-freemarker</artifactId>
</dependency>
完整pom文件
<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.itcast</groupId>
<artifactId>springboot-freemarker-004</artifactId>
<version>0.0.1-SNAPSHOT</version>
<packaging>war</packaging>
<!-- 引入springboot父类依赖 -->
<parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>2.0.2.RELEASE</version>
<relativePath /> <!-- lookup parent from repository -->
</parent>
<properties>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
<project.reporting.outputEncoding>UTF-8</project.reporting.outputEncoding>
<java.version>1.8</java.version>
</properties>
<dependencies>
<!-- spring boot 依赖 -->
<!-- spring boot starter -->
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter</artifactId>
</dependency>
<!-- spring-boot-configuration-processor 资源文件-->
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-configuration-processor</artifactId>
<optional>true</optional>
</dependency>
<!-- spring boot starter-test -->
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-test</artifactId>
<scope>test</scope>
</dependency>
<!-- spring boot starter-web -->
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
<!-- spring boot starter-freemarker freemarker模板依赖 -->
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-freemarker</artifactId>
</dependency>
</dependencies>
<build>
<plugins>
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
</plugin>
</plugins>
</build>
</project>
4.2 application.properties文件增加freemarker的配置
#########################################################################
#
#freemarker 静态资源配置
#
##########################################################################
#设定ftl文件模板的加载路径,多个以逗号分隔,默认: ["classpath:/templates/"]
spring.freemarker.template-loader-path=classpath:/templates
#是否开启template caching.关闭缓存,及时刷新,上线环境需要改为true
spring.freemarker.cache=false
#设定Template的编码.设置为uft-8
spring.freemarker.charset=utf-8
#是否检查templates路径是否存在.设置为true
spring.freemarker.check-template-location=true
#设定Content-Type.
spring.freemarker.content-type=text/html
#设定所有request的属性在merge到模板的时候,是否要都添加到model中.
spring.freemarker.expose-request-attributes=true
#设定所有HttpSession的属性在merge到模板的时候,是否要都添加到model中.
spring.freemarker.expose-session-attributes=true
#指定RequestContext属性的名.
spring.freemarker.request-context-attribute=request
#设定freemarker模板的前缀.
#spring.freemarker.prefix=
#设定freemarker模板的后缀.
spring.freemarker.suffix=.ftl
#指定使用模板的视图列表.
#spring.freemarker.view-names
#是否允许mvc使用freemarker.
#spring.freemarker.enabled
#指定HttpSession的属性是否可以覆盖controller的model的同名项
#spring.freemarker.allow-session-override
#指定HttpServletRequest的属性是否可以覆盖controller的model的同名项
#spring.freemarker.allow-request-override=false
#设定是否以springMacroRequestContext的形式暴露RequestContext给Spring’s macro library使用
#spring.freemarker.expose-spring-macro-helpers=false
#是否优先从文件系统加载template,以支持热加载,默认为true
#spring.freemarker.prefer-file-system-access
#设定FreeMarker keys.
#spring.freemarker.settings
4.3 新建FreemarkerController控制层
package com.itcast.controller;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.RequestMapping;
import com.itcast.entity.Resource;
/**
* 访问路径:http://localhost:8080/ftl/index
* 访问路径:http://localhost:8080/ftl/center
* @author jack
*
*/
@Controller
@RequestMapping("ftl")
public class FreemarkerController {
@Autowired
private Resource resource;
@RequestMapping("/index")
public String index(ModelMap map){
map.addAttribute("resource", resource);
return "freemarker/index";
}
@RequestMapping("/center")
public String center(){
return "freemarker/center/center";
}
}
4.4 在main/resource目录下新建templates目录,该目录中新增一个index.ftl文件,并增加一个center文件夹其中包含一个center.ft文件。
index.ftl
<!DOCTYPE HTML>
<html>
<head lang="en">
<meta charser="utf-8">
<tile>freemarker模板引擎-index page</title>
</head>
<body>
freemarker 模板引擎-index page
<h1>${resource.name}</h1>
<h1>${resource.website}</h1>
<h1>${resource.language}</h1>
</body>
</html>
center.ft
<!DOCTYPE HTML>
<html>
<head lang="en">
<meta charser="utf-8">
<tile>freemarker 模板引擎-center page</title>
</head>
<body>
freemarker 模板引擎-center page
<h1>center page</h1>
</body>
</html>
4.5 启动入口类访问
访问路径:http://localhost:8080/ftl/index
<!DOCTYPE HTML>
<html>
<head lang="en">
<meta charser="utf-8">
<tile>index page</title>
</head>
<body>
freemarker 模板引擎-index page
<h1>${resource.name}</h1>
<h1>${resource.website}</h1>
<h1>${resource.language}</h1>
</body>
</html>
![](https://i-blog.csdnimg.cn/blog_migrate/0e56ad28f6aea04dee825337bf6698f3.png)
访问路径:http://localhost:8080/ftl/center
<!DOCTYPE HTML>
<html>
<head lang="en">
<meta charser="utf-8">
<tile>center page</title>
</head>
<body>
freemarker 模板引擎-center page
<h1>center page</h1>
</body>
</html>