spring-boot 如何为特定的 url 提供服务? [英] How does spring-boot able to serve specific url?

查看:90
本文介绍了spring-boot 如何为特定的 url 提供服务?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

根据我之前的经验:

  • 当使用纯 servlet 时,我们定义了 servlet,以便它服务于匹配特定 url 的请求.
  • 在使用 struts2 时,我们定义了一个过滤器,以便它可以为匹配特定 url 的请求提供服务.
  • 在传统的 xml 配置样式中使用 springMVC 时,我们定义了一个调度程序 servlet,以便它可以为匹配特定 url 的请求提供服务.
  • When using pure servlet, we define servlets so that it will serve requests that match specific urls.
  • When using struts2, we define a filter so that it will serve requests that match specific urls.
  • When using springMVC in a traditional xml configuration style, we define a dispatcher servlet so that it will serve requests that match specific urls.

但是使用 spring-boot:

似乎没有明确定义 servlet 或过滤器.但它仍然可以提供特定的网址.

Seems no servlet or filter is defined explicitly. But it still could serve specific urls.

问题是:

  • 还在使用servlet吗?如果是,它如何在不明确定义 servlet 或过滤器的情况下提供 url?

其他相关问题 (基于评论提示):

  • SpringBootServletInitializer 的实现似乎会在部署时调用,但谁来调用它?
  • It seems the implementation of SpringBootServletInitializer will be invoked on deploy, but who is going to invoke it?

推荐答案

如您所见 此处 的详细信息,在启动,在初始化嵌入式服务器(默认为 Tomcat)时,Spring Boot 创建和注册 DispatcherServlet 作为 servlet.

As you can see here in details, on startup, while initializing an embedded server (Tomcat by default), Spring Boot creates and registers DispatcherServlet as a servlet.

Spring 然后像往常一样扫描您自己的类(包括您从中调用 SpringApplication.run() 的类)并为您的控制器设置相应的映射,如果您有任何.例如 /hello 的映射如下:

Spring then, as usual, scans your own classes (including the one you invoke SpringApplication.run() from) and sets corresponding mapping for your controllers, if you have any. For example mapping for /hello here:

@RestController
@EnableAutoConfiguration
public class TestSpring {

    @RequestMapping("/hello")
    String hello() {
        return "Hello World!";
    }

    public static void main(String[] args) throws Exception {
        SpringApplication.run(TestSpring.class, args);
    }

}

这篇关于spring-boot 如何为特定的 url 提供服务?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆