极简SpringBoot指南-Chapter03-基于SpringBoot的Web服务
仓库地址
Chapter03-基于SpringBoot的Web服务
前言
终于,我们的教程来到了SpringBoot核心功能的介绍。对于本章,各位读者至少具备以下的知识:
- maven的使用
- HTTP
SpringBoot的maven配置介绍
对于一个简单的maven结构的项目来说,以下结构:
${basedir}
|-- pom.xml
|-- src
|-- main
|-- java
|| com.xxx.xxx 项目源码
|-- resources
|| 项目配置文件 .xml等
|-- test
|-- java
|-- resources
在我们前面的教程中,我们都是在编写着代码,从未关注过maven的pom.xml究竟是如何配置的,因为项目脚手架已经配置完成了,也许有细心的同学已经看过了pom.xml文件的内容了:
<?xml version="1.0" encoding="UTF-8"?>
<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 https://maven.apache.org/xsd/maven-4.0.0.xsd">
<!-- ... 省略了当前项目本身信息的配置,如有需要,请读者查看源码 -->
<!-- 当前pom的父级 -->
<parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>2.5.3</version>
<relativePath/> <!-- lookup parent from repository -->
</parent>
<dependencies>
<!-- springboot框架web功能starter -->
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
<!-- springboot框架的测试依赖starter -->
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-test</artifactId>
<scope>test</scope>
</dependency>
</dependencies>
<!-- 构建流程使用SpringBoot的maven插件 -->
<build>
<plugins>
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
</plugin>
</plugins>
</build>
</project>
对于一个普通的maven项目,其实核心的就是dependencies节点-dependency节点
了。通过dependency
节点,我们可以GAV坐标(Group-Artifact-Version)引入不同的库jar包,便于我们的项目使用这些库。
那么为什么在上面的pom出现了一个parent节点呢?实际上,pom允许所谓的继承:我们可以把一堆的pom依赖和配置,放到一个公共的pom里面,然后各个项目通过parent节点去引用这个公共的pom文件。由于SpringBoot并不是一个单一的jar包构成的框架,它的内部其实依赖了下面的核心库:
spring-core
spring-beans
spring-context
...还有很多
如果手动一项又一项编写dependency来使用Spring框架,不仅仅容易遗漏,而且十分不方便进行这些依赖库的版本管理。基于此,SpringBoot官方提供了父级pom:
<parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>2.5.3</version> <!-- 2021/08/09最新 -->
<relativePath/> <!-- lookup parent from repository -->
</parent>
那么再看我们的pom文件中,还依赖了spring-boot-starter-web
,这又是什么呢?难道有一个jar包叫做spring-boot-starter-web
吗?其实不然。我们上面提到了parent POM,但是Spring框架下的依赖包特别多,并且有些包是核心的包,有些包则是在某些功能需要的情况下才依赖的包。如果一股脑的全部通过parent引入会让你的项目依赖十分臃肿,所以Spring官方再次按照包的功能进行了一定的组合,形成了所谓的starter,如果你只是想做web API的服务开发,用spring-boot-starter-web
就可以了,要是需要使用AOP(面向切面编程,作切面开发),加上spring-boot-starter-aop
依赖即可。
一个简单的Controller
// 使用注解 @RestController,表明当前类是一个基于REST 规范的HTTP API Controller
@RestController
// @RequestMapping 注解放在Controller上,用于标记 HTTP url的路径入口,
// 例如 http://localhost:8080/hello/xxx 才会进入当前Controller
@RequestMapping("hello")
public class HelloController {
// @RequestMapping 注解放在Controller的里面的方法上,
// 将会与Controller上的RequestMapping组合成:"/hello/say"
// method用于指示通过何种HTTP方法访问
// 在程序启动后,我们可以使用GET方法访问:http://localhost:8080/hello/say
@RequestMapping(value = "say", method = RequestMethod.GET)
public String say() {
return "hello, world";
}
}
编写启动类,启动我们的SpringBoot程序:
@SpringBootApplication
public class Chapter03App {
public static void main(String[] args) {
SpringApplication.run(Chapter03App.class, args);
}
}
启动成功后,通过HTTP访问:
http://localhost:8080/hello/say
# 输出:hello, world
关于Controller需要注意的点
上面的例子中,我们使用注解@RestController
来标记了我们的Controller类,会有初学者使用@Controller
来标记Controller,让我们改成它试试:
@Controller // 改成使用 @Controller注解,其他不变,再次访问,看看有什么问题
@RequestMapping("hello")
public class HelloController {
// ...
}
重启启动应用后再次访问对应地址。如果是在浏览器中,你会看到:
Whitelabel Error Page
This application has no explicit mapping for /error, so you are seeing this as a fallback.
Mon Aug 09 10:16:59 CST 2021
There was an unexpected error (type=Not Found, status=404).
type=Not Found, status=404
,404!为什么会这样呢?
- 如果使用
@Controller
标记,那么将使用SpringMVC架构(自行了解),如果对应的方法返回的是字符串,则这个字符串表明需要查找对应的视图(View)名称,并将对应的视图通过视图解析器(InternalResourceViewResolver)解析修改为前端web页面。 - 如果使用
@RestController
注解Controller,则Controller中的方法无法返回jsp页面,或者html,配置的视图解析器不起作用,返回的内容就是return里的内容。
针对情况1,解决方法就是在方法的返回上加上注解:@ResponseBody
:
@ResponseBody // 如果当前Controller被@Controller注解,又想返回字符串或其他原始数据
@RequestMapping(value = "say", method = RequestMethod.GET)
public String say() {
return "hello, world";
}
Controller如何被加载的?
在之前的文章,我们已经介绍了SpringBoot是如何初始化Bean并且将其放在IOC容器的。我们提到了三种方式:1、@Component
;2、Java配置类;3、XML配置。对于第2、3点,好像目前我们的样例中并没有做手动配置的事情。那么是不是我们的Controller已经被@Component
标记了呢?
查看@RestController的定义:
@Target({ElementType.TYPE})
@Retention(RetentionPolicy.RUNTIME)
@Documented
@Controller // 原来你RestController也是一个Controller注解啊!
@ResponseBody // 并且,已经添加了@ResponseBody
public @interface RestController {
@AliasFor(
annotation = Controller.class
)
String value() default "";
}
@RestController
其实组合了@Controller
和@ResponseBody
两个注解了。我们再看看@Controller
的定义:
@Target({ElementType.TYPE})
@Retention(RetentionPolicy.RUNTIME)
@Documented
@Component // 原来你Controller注解也组合了Component注解
public @interface Controller {
@AliasFor(
annotation = Component.class
)
String value() default "";
}
原来@Controller
也已经组合了@Component
注解啊,难怪我们的定义的Controller能被加载。
Controller如何处理HTTP请求的?
要回答这个问题,需要你有一定的关于Java Tomcat Web容器的知识。本系列主要是对SpringBoot的快速入门,不便于讲的过细。简单一点就是:
- Tomcat是一个用Java编写的Web容器,可以接受HTTP请求。
- SpringBoot内置了Tomcat容器,并且对于HTTP请求,转到了内部处理
- 对于这些HTTP请求,根据请求的路径等上下文,匹配Bean容器中的Controller进行处理
极简SpringBoot指南-Chapter03-基于SpringBoot的Web服务的更多相关文章
- 黑科技抢先尝(续2) - Windows terminal中Powershell Tab的极简美化指南
目录 安装python 安装git 安装powerline字体 主题定制 安装oh-my-posh 查看策略组的执行权限 使用choco 安装终端模拟器 - ConEmu 优化 PowerShell ...
- 基于 REST 的 Web 服务:基础
代表性状态传输(Representational State Transfer,REST)在 Web 领域已经得到了广泛的接受,是基于 SOAP 和 Web 服务描述语言(Web Services D ...
- 超级好用的前端开发测试Chrome插件-基于REST的Web服务客户端
基于REST的Web服务客户端是一款功能强大的谷歌浏览器插件,使用基于REST的Web服务客户端(模拟REST客户端)可以让用户使用谷歌浏览器模拟REST请求来测试REST风格. 基于REST的Web ...
- 在chrome中安装基于REST的web服务客户端
基于REST的Web服务客户端的使用方法 点击转到基于REST的Web服务客户端下载页面 chrome浏览器如果安装扩展程序点击chrome浏览器右上角,选择“设置在“设置”对话框里选择“扩展程序”然 ...
- 基于Socket创建Web服务
基于Socket创建Web服务 为什么要使用Socket呢,我们来看下图
- 极简SpringBoot指南-Chapter04-基于SpringBoot的书籍管理Web服务
仓库地址 w4ngzhen/springboot-simple-guide: This is a project that guides SpringBoot users to get started ...
- Spring boot构建基于rest的Web服务
一.介绍:使用Spring Boot我们可以很容易的创建一个可独立运行的Rest web服务,其中内嵌tomact,我们只需“run”就可以查看效果了. Spring Boot利用Gradle或Mav ...
- Web Service 实例基于Socket创建Web服务
ServerSocket服务器端代码如下: public static void main(String[] args) throws IOException { // 1:建立服务器端的tcp so ...
- 极简SpringBoot指南-Chapter00-学习SpringBoot前的基本知识
仓库地址 w4ngzhen/springboot-simple-guide: This is a project that guides SpringBoot users to get started ...
随机推荐
- plsql developer中各个window的作用【转】
转载自,原文链接: -程序窗口(program window) :可以执行 sql,sqlplus 相关的语句,例如存储过程,方法,一般用来开发程序用的. -测试窗口(test window):一般是 ...
- git tag的用法及意义
git tag 介绍 命令是用来给当前项目状态(在某次commit后)打标签的,目的是便于以后将项目状态回滚到当时打标签的状态.可以把它与虚拟机的snapshot(快照)进行类比. 回想当时在看< ...
- ROS catkin_make error Could not find a package configuration file provided by "actionlib_msgs"
在使用ROS catkin_make编译的时候,出现类似如下错误 CMake Error at /opt/ros/kinetic/share/catkin/cmake/catkinConfig.cma ...
- Servlet学习笔记(三)之HttpServletResponse
init() 方法中参数 ServletConfig 对象使用 通过ServletConfig 获得 ServletContext对象 使用 HttpServletRequest 与HttpServl ...
- IPv6 QoS 多媒体应用:性能分析 (上)
IPv6 QoS 多媒体应用:性能分析 Assured Forwarding (AF):保证转发 Expedited Forwarding (EF):快速转发 Traffic aggregatio ...
- 【流程】Flowable流程定义总结
背景 近几年,互联网企业从消费互联网向产业互联网转型.在消费互联网时期,企业面对的时C端消费者,而产业互联网面对的是B端用户. 产业互联网涉及方方面面,企业信息化的建设就是B端用户的业务之一,在企业就 ...
- python-request 实现企业微信接口自动化-1(DDT)
环境准备 python+requests 读取企业微信api开发文档,得知调用企业微信接口必须先获取企业微信的accesstoken是通过 ("corpid","&quo ...
- Openswan支持的算法及参数信息:
数据封装加密算法: algorithm ESP encrypt: id=2, name=ESP_DES, ivlen=8, keysizemin=64, keysizemax=64 algorithm ...
- 取消input默认提示框
input输入框有自动保存记忆功能,点击的时候之前输入的内容会在下拉框自动提示 autocomplete="off",这是H5的一个属性. <input type=" ...
- 硕盟SM-T54(TYPE C转HDMI+VGA+USB3.0+PD3.0)
硕盟SM-T54是一款TYPE C转HDMI+VGA+USB3.0+PD3.0四口扩展坞,您可以将含有USB 3.1协议的电脑主机,通过此产品连接到具有HDMI或VGA的显示器.电视机或其他显示设备. ...