SpringBoot(二)--springboot案例_springboot如何写http项目路径?-程序员宅基地

技术标签: springboot  

一 、Hello案例

1.1 创建maven项目

上一节,我已经学会了手动创建springboot项目。接下来我们用官方的方式来创建

打开 https://start.spring.io/



填好红色部分内容,选择需要的粉红色部分内容,点击绿色箭头下载生成的,解压后导入到工作空间。

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>

访问路径: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>









版权声明:本文为博主原创文章,遵循 CC 4.0 BY-SA 版权协议,转载请附上原文出处链接和本声明。
本文链接:https://blog.csdn.net/qq_35558797/article/details/80381054

智能推荐

leetcode 172. 阶乘后的零-程序员宅基地

文章浏览阅读63次。题目给定一个整数 n,返回 n! 结果尾数中零的数量。解题思路每个0都是由2 * 5得来的,相当于要求n!分解成质因子后2 * 5的数目,由于n中2的数目肯定是要大于5的数目,所以我们只需要求出n!中5的数目。C++代码class Solution {public: int trailingZeroes(int n) { ...

Day15-【Java SE进阶】IO流(一):File、IO流概述、File文件对象的创建、字节输入输出流FileInputStream FileoutputStream、释放资源。_outputstream释放-程序员宅基地

文章浏览阅读992次,点赞27次,收藏15次。UTF-8是Unicode字符集的一种编码方案,采取可变长编码方案,共分四个长度区:1个字节,2个字节,3个字节,4个字节。文件字节输入流:每次读取多个字节到字节数组中去,返回读取的字节数量,读取完毕会返回-1。注意1:字符编码时使用的字符集,和解码时使用的字符集必须一致,否则会出现乱码。定义一个与文件一样大的字节数组,一次性读取完文件的全部字节。UTF-8字符集:汉字占3个字节,英文、数字占1个字节。GBK字符集:汉字占2个字节,英文、数字占1个字节。GBK规定:汉字的第一个字节的第一位必须是1。_outputstream释放

jeecgboot重新登录_jeecg 登录自动退出-程序员宅基地

文章浏览阅读1.8k次,点赞3次,收藏3次。解决jeecgboot每次登录进去都会弹出请重新登录问题,在utils文件下找到request.js文件注释这段代码即可_jeecg 登录自动退出

数据中心供配电系统负荷计算实例分析-程序员宅基地

文章浏览阅读3.4k次。我国目前普遍采用需要系数法和二项式系数法确定用电设备的负荷,其中需要系数法是国际上普遍采用的确定计算负荷的方法,最为简便;而二项式系数法在确定设备台数较少且各台设备容量差..._数据中心用电负荷统计变压器

HTML5期末大作业:网页制作代码 网站设计——人电影网站(5页) HTML+CSS+JavaScript 学生DW网页设计作业成品 dreamweaver作业静态HTML网页设计模板_网页设计成品百度网盘-程序员宅基地

文章浏览阅读7k次,点赞4次,收藏46次。HTML5期末大作业:网页制作代码 网站设计——人电影网站(5页) HTML+CSS+JavaScript 学生DW网页设计作业成品 dreamweaver作业静态HTML网页设计模板常见网页设计作业题材有 个人、 美食、 公司、 学校、 旅游、 电商、 宠物、 电器、 茶叶、 家居、 酒店、 舞蹈、 动漫、 明星、 服装、 体育、 化妆品、 物流、 环保、 书籍、 婚纱、 军事、 游戏、 节日、 戒烟、 电影、 摄影、 文化、 家乡、 鲜花、 礼品、 汽车、 其他 等网页设计题目, A+水平作业_网页设计成品百度网盘

【Jailhouse 文章】Look Mum, no VM Exits_jailhouse sr-iov-程序员宅基地

文章浏览阅读392次。jailhouse 文章翻译,Look Mum, no VM Exits!_jailhouse sr-iov

随便推点

chatgpt赋能python:Python怎么删除文件中的某一行_python 删除文件特定几行-程序员宅基地

文章浏览阅读751次。本文由chatgpt生成,文章没有在chatgpt生成的基础上进行任何的修改。以上只是chatgpt能力的冰山一角。作为通用的Aigc大模型,只是展现它原本的实力。对于颠覆工作方式的ChatGPT,应该选择拥抱而不是抗拒,未来属于“会用”AI的人。AI职场汇报智能办公文案写作效率提升教程 专注于AI+职场+办公方向。下图是课程的整体大纲下图是AI职场汇报智能办公文案写作效率提升教程中用到的ai工具。_python 删除文件特定几行

Java过滤特殊字符的正则表达式_java正则表达式过滤特殊字符-程序员宅基地

文章浏览阅读2.1k次。【代码】Java过滤特殊字符的正则表达式。_java正则表达式过滤特殊字符

CSS中设置背景的7个属性及简写background注意点_background设置背景图片-程序员宅基地

文章浏览阅读5.7k次,点赞4次,收藏17次。css中背景的设置至关重要,也是一个难点,因为属性众多,对应的属性值也比较多,这里详细的列举了背景相关的7个属性及对应的属性值,并附上演示代码,后期要用的话,可以随时查看,那我们坐稳开车了······1: background-color 设置背景颜色2:background-image来设置背景图片- 语法:background-image:url(相对路径);-可以同时为一个元素指定背景颜色和背景图片,这样背景颜色将会作为背景图片的底色,一般情况下设置背景..._background设置背景图片

Win10 安装系统跳过创建用户,直接启用 Administrator_windows10msoobe进程-程序员宅基地

文章浏览阅读2.6k次,点赞2次,收藏8次。Win10 安装系统跳过创建用户,直接启用 Administrator_windows10msoobe进程

PyCharm2021安装教程-程序员宅基地

文章浏览阅读10w+次,点赞653次,收藏3k次。Windows安装pycharm教程新的改变功能快捷键合理的创建标题,有助于目录的生成如何改变文本的样式插入链接与图片如何插入一段漂亮的代码片生成一个适合你的列表创建一个表格设定内容居中、居左、居右SmartyPants创建一个自定义列表如何创建一个注脚注释也是必不可少的KaTeX数学公式新的甘特图功能,丰富你的文章UML 图表FLowchart流程图导出与导入导出导入下载安装PyCharm1、进入官网PyCharm的下载地址:http://www.jetbrains.com/pycharm/downl_pycharm2021

《跨境电商——速卖通搜索排名规则解析与SEO技术》一一1.1 初识速卖通的搜索引擎...-程序员宅基地

文章浏览阅读835次。本节书摘来自异步社区出版社《跨境电商——速卖通搜索排名规则解析与SEO技术》一书中的第1章,第1.1节,作者: 冯晓宁,更多章节内容可以访问云栖社区“异步社区”公众号查看。1.1 初识速卖通的搜索引擎1.1.1 初识速卖通搜索作为速卖通卖家都应该知道,速卖通经常被视为“国际版的淘宝”。那么请想一下,普通消费者在淘宝网上购买商品的时候,他的行为应该..._跨境电商 速卖通搜索排名规则解析与seo技术 pdf

推荐文章

热门文章

相关标签