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

智能推荐

正规的IT外包公司的报价组成_软件劳务派遣报价-程序员宅基地

文章浏览阅读3k次。在IT驻场外包中,外包公司在派遣人员与用人单位之间到底从中抽了多少?_软件劳务派遣报价

ganglia安装-程序员宅基地

文章浏览阅读68次。我是参考 http://www.ibm.com/developerworks/cn/linux/l-ganglia-nagios-1/ 这篇文章搭建的ganglia,部分内容页引自这篇文章,与原文不同之处用红色标出,操作系统是CentOS 5.7 x86_64。安装 Ganglia先决条件假定您已经设置了 yum 库,安装先决条件在很大程度上应当十分简单。类...

Magics修复STL文件_magics导入零件 stp-程序员宅基地

文章浏览阅读7k次。Magics RP是比利时Materialise公司开发的、完全针对3D打印工序特征的软件,其目前最新版本为19.01。Magics为处理STL文件提供了理想的、完美的解决方案,具有功能强大、易用、高效等优点,是从事3D打印行业必不可少的软件。在3D打印行业,Magics常用于零件摆放、模型修复、添加支撑、切片等环节。  由于STL文件结构简单,没有几何拓扑_magics导入零件 stp

oracle 学习网站收集-程序员宅基地

文章浏览阅读1.7k次。《转载》Oracle官方站:Oracle中文官网metalink.oracle.comOracle官方知识库,需要付费帐号登陆tahiti.oracle.comsearch and download documentation for Oracle's server productsOracle11gR1Online DocumentationOracle10gR2 Online Docu

【毫米波雷达】毫米波雷达接收发射信号matlab仿真_毫米波雷达仿真-程序员宅基地

文章浏览阅读872次,点赞22次,收藏26次。毫米波雷达是一种利用毫米波段电磁波来探测目标的雷达系统。它具有体积小、重量轻、功耗低、分辨率高、抗干扰能力强等优点,广泛应用于汽车、航空、航天、军事等领域。毫米波雷达的工作原理是:雷达发射机发射毫米波电磁波,电磁波遇到目标后反射,反射波被雷达接收机接收,并根据反射波的强度、频率和相位等信息来确定目标的位置、速度和姿态。毫米波雷达的接收发射信号主要包括以下几个步骤:发射信号毫米波雷达发射机产生毫米波电磁波,并通过天线发射出去。发射信号的频率、功率和波形等参数由雷达系统的设计要求决定。信号传播。_毫米波雷达仿真

matlab for,while,break和continue循环和循环控制_matlab设计问题if,while,for都包含的例题-程序员宅基地

文章浏览阅读6.4k次,点赞6次,收藏8次。for循环循环特定的次数,用end表示循环快结束。示例如下:注意n可以从1取到10,总共循环10次。while循环只要条件为真,就一直循环,用end表示循环快结束。示例如下:该程序中,设置n初值为10,然后进入while循环中。m为产生不大于n的一个随机数,当产生的随机数m和n相等时,退出循环。否则把n加1,再次循环。运行结果如下:if end条件控制if后面要和一个布尔表达式,后面再跟一个或者多个语句分割,就是一个if..end语句。运行如下:i_matlab设计问题if,while,for都包含的例题

随便推点

IDEA中快捷创建SpringBoot主启动类的方法的设置_idea本地启动spring配置主类-程序员宅基地

文章浏览阅读4.9k次,点赞4次,收藏11次。IDEA中快捷创建SpringBoot主启动类的方法的设置,自动同步同类名的参数_idea本地启动spring配置主类

Android 动态添加View 并设置id_android字符串动态生成view id-程序员宅基地

文章浏览阅读2.7w次,点赞14次,收藏40次。主页面布局(main_activity.xml) LinearLayout 里面加一个Button,注意这里的LinearLayout要有orientation&lt;?xml version="1.0" encoding="utf-8"?&gt;&lt;LinearLayout ="http://schemas.android.com/apk..._android字符串动态生成view id

[arcgis插件]尖锐角检查/批量处理工具-GIS程序猿_arcgis如何查尖锐角-程序员宅基地

文章浏览阅读459次。2、设置合并优先级。选择字段,设置优先级。无需优先级,可以吧文字清空,则会根据与地块有相同信息字段的值来合并。[arcgis插件]尖锐角检查/批量处理工具,支持arcgis10.2-10.8版本。7、仅仅检查选中的地块:先选中地块再执行流程。5、处理流程设置:1 处理,2 切割,3 合并。6、顺便检查选择检查狭长面、自相交、重复节点。4、存在尖锐角并且面积小于这个面积阈值,则无需切割,直接合并。可以选择shp数据、GDB或者MDB的矢量面图层。年度变更,又是尖锐角,死磕尖锐角,就不信搞不定它。_arcgis如何查尖锐角

例子:BlackBerry真正的后台运行程序,Task里面看不到的哦_黑莓手机guid-程序员宅基地

文章浏览阅读5k次。说明:1.BlackBerry_App_Descriptor.xml设置程序为Auto-run on startup,Do not display the application icon on the BlackBerry home screen2.手机开机后自动运行 BackgroundApplication3.主程序BackgroundApplication的main中,执行BackgroundThread.waitForSingleton().start();启动后台线程4.BackgroundTh_黑莓手机guid

oracle中查找执行效率低下的SQL_oracle 怎么抓取执行慢的sql-程序员宅基地

文章浏览阅读9.9k次。oracle中查找执行效率低下的SQLkt431128 发布于 9个月前,共有 0 条评论v$sqltext:存储的是完整的SQL,SQL被分割v$sqlarea:存储的SQL 和一些相关的信息,比如累计的执行次数,逻辑读,物理读等统计信息(统计)v$sql:内存共享SQL区域中已经解析的SQL语句。(即时) select opname, ta_oracle 怎么抓取执行慢的sql

linux下iso8859乱码,在Linux上转换UTF-8和ISO-8859之间的文件-程序员宅基地

文章浏览阅读3k次。每当我遇到Unicode时,都没有用.我在Linux上,我从Windows获得这些文件:$file *file1: UTF-8 Unicode textfile2: ISO-8859 textfile3: ISO-8859 text在我发现文件有不同的编码之前,没有任何工作.我希望让我的生活变得轻松,并将它们全部放在相同的格式中:iconv -f UTF-8 -t ISO-8859 file1 &..._iso-8859 text

推荐文章

热门文章

相关标签