如何在spring boot项目中注册Servlet呢?

由于没有web.xml,无法直接在xml中配置,但是spring boot提供了另外两种更为简洁的方式:

一. java代码实现servlet注册

1.创建servlet类。(一贯作风,直接上code,简单粗暴有效)

public class WeChatServlet extends HttpServlet {
protected void doGet(HttpServletRequest req, HttpServletResponse resp)
throws ServletException, IOException { String signature = req.getParameter("signature");
String timestamp = req.getParameter("timestamp");
String nonce = req.getParameter("nonce");
String echostr = req.getParameter("echostr");
PrintWriter out = resp.getWriter();
if (ValidUtil.checkSignature(signature, timestamp, nonce)) {
out.print(echostr);
}
}
}

2.在主类中注册

@SpringBootApplication
public class MyAppliction extends SpringBootServletInitializer { public static void main(String[] args) {
SpringApplication.run(MyApplication.class, args);
} @Override
protected SpringApplicationBuilder configure(SpringApplicationBuilder application) {
return application.sources(MyApplicaitioni.class);
} // 注册servlet
@Bean
public ServletRegistrationBean weChatValid(){
//第一个参数是第1步中创建的WeChatServlet实例,第二个参数是其对应的路径,相当于web.xml中配置时的url-pattern。
return new ServletRegistrationBean(new WeChatServlet(), "/weChatValid");
}
}

多个了weChatValid函数(注意要用@Bean注解),其中,返回ServletRegistrationBean的实例,其中两个参数…(自己看代码中注释0)。

完毕。

二、注解实现Servlet注册

1.创建Servlet类,并添加注解。

//注解实现
@WebServlet(urlPatterns = "/weChatValid", description = "微信接口验证")
public class WeChatServlet extends HttpServlet {
protected void doGet(HttpServletRequest req, HttpServletResponse resp)
throws ServletException, IOException {
String signature = req.getParameter("signature");
String timestamp = req.getParameter("timestamp");
String nonce = req.getParameter("nonce");
String echostr = req.getParameter("echostr");
PrintWriter out = resp.getWriter();
if (ValidUtil.checkSignature(signature, timestamp, nonce)) {
out.print(echostr);
}
}
}

类前多了个@WebServlet注解,参数urlParttern和descriptiion不言而喻。

2.给主类添加一个注解@ServletComponentScan

@ServletComponentScan //添加的注解
@SpringBootApplication
public class MyAppliction extends SpringBootServletInitializer { public static void main(String[] args) {
SpringApplication.run(MyApplication.class, args);
} @Override
protected SpringApplicationBuilder configure(SpringApplicationBuilder application) {
return application.sources(MyApplication.class);
}
}

多了个@ServletComponentScan注解。

完毕。

此时访问该Servlet的url就可以是:

http://localhost:8080/weChatValid

另外,还有一种方法:

@RestController
public class Servlet2 extends HttpServlet {
@RequestMapping("/serv2")//urlPatterns

url:

http://localhost:8080/ser2

搞定!

Spring boot中注册Servlet的更多相关文章

  1. Spring boot中使用servlet filter

    Spring boot中使用servlet filter liuyuhang原创,未经允许请勿转载! 在web项目中经常需要一些场景,如参数过滤防止sql注入,防止页面攻击,空参数矫正等, 也可以做成 ...

  2. 从零开始的Spring Boot(2、在Spring Boot中整合Servlet、Filter、Listener的方式)

    在Spring Boot中整合Servlet.Filter.Listener的方式 写在前面 从零开始的Spring Boot(1.搭建一个Spring Boot项目Hello World):http ...

  3. Spring Boot中使用Servlet与Filter

    在Spring Boot中使用Servlet,根据Servlet注册方式的不同,有两种使用方式.若使用的是Servlet3.0+版本,则两种方式均可使用:若使用的是Servlet2.5版本,则只能使用 ...

  4. spring boot中使用servlet、listener和filter

    spring boot中支持使用java Web三大组件(servlet.listener和filter),但是坑比较多,主要是spring boot内嵌tomcat和独立tomcat服务器有一些细节 ...

  5. Spring Boot 自定义注册 Servlet、Filter、Listener

    前言 在 Spring Boot 中已经移除了 web.xml 文件,如果需要注册添加 Servlet.Filter.Listener 为 Spring Bean,在 Spring Boot 中有两种 ...

  6. spring boot中注册拦截器

    拦截器是动态拦截Action调用的对象.它提供了一种机制可以使开发者可以定义在一个action执行的前后执行的代码,也可以在一个action执行前阻止其执行,同时也提供了一种可以提取action中可重 ...

  7. Spring Boot之注册servlet三大组件

    由于Spring Boot默认是以jar包的形式启动嵌入式的Servlet容器来启动Spring Boot的web应用是,没有web.xml配置文件 注册三大组件用以下方式 ServletRegist ...

  8. 18. Spring Boot 、注册Servlet三大组件Servlet、Filter、Listener

    由于SpringBoot默认是以jar包的方式启动嵌入式的Servlet容器来启动SpringBoot的web应用,没有web.xml文件 public class MyServlet extends ...

  9. spring boot 中使用servlet

随机推荐

  1. Gogs+Drone搭建CI/CD平台

    Gogs 是由 Go 语言编写的 Git 服务器,由中国人主导开发的一款开源项目,搭建方便并且拥有完善的中文文档,配合 Drone 可以实现持续集成/持续部署.本文介绍如何通过 Docker 搭建 G ...

  2. xmind8-update9 安装破解激活教程

    xmind8是一款原型图设计流行的软件,相比于xmind2020功能更为丰富,比如画甘特图等.本教程来教大家如何对xmind8 update9进行安装激活,使用全部功能,无限期使用! 只看本文一篇即可 ...

  3. PySpider爬取去哪儿攻略数据项目

    1 创建项目 点击WEB中的Create创建项目 填入相关项目名和其实爬取URL 创建后进入项目首页 右边 Handler 是pyspider的主类,整个爬虫一个Handler,其中可定义爬虫的爬取. ...

  4. 用 5W1H 告诉你如何规划合理的测试策略

    ​​摘要:测试策略描述了测试工程的总体方法和目标.描述目前在进行哪一阶段的测试以及每个阶段内在进行的测试种类(功能测试.性能测试.覆盖测试等)以及测试人力安排等. 本文分享自华为云社区<浅谈敏捷 ...

  5. Redis挂了,流量把数据库也打挂了,怎么办?

    你好呀,我是歪歪. 是这样的,前几天有个读者给我发消息,说面试的时候遇到一个场景题: 他说他当时,一时间竟然找不到回答问题的角度,感觉自己没有回答到点子上. 我仔细想了一下,确实是感到这个问题有一丝丝 ...

  6. CRC校验原理和verilog实现方法(二)

    1 前言 在 前面的博客  CRC校验原理和verilog实现方法(一)  中,介绍了CRC校验的原理和手动计算过程.本文说一下我在学习CRC校验FPGA实现的一点心得体会. 2 线性反馈移位寄存器 ...

  7. C++STL—string类

    string容器 1.1 string容器的基本概念 string容器是一个类 这个容器中有一个指针,指针维护了一个数组 string容器提供copy.find.insert.replace等等功能 ...

  8. PXE高效批量装机

    目录 一.PXE概述 二.PXE的优点 三.搭建PXE的前提 四.搭建PXE远程安装服务器 4.1.安装并启用TFTP服务 4.2.安装dhcp服务 4.3.准备linux内核.初始化镜像文件 4.3 ...

  9. Longhorn 企业级云原生容器存储解决方案-部署篇

    内容来源于官方 Longhorn 1.1.2 英文技术手册. 系列 Longhorn 是什么? Longhorn 云原生分布式块存储解决方案设计架构和概念 安装 Longhorn 可以通过多种方式安装 ...

  10. centos7 更新源

    centos7 yum源更新   先进入到yum源文件cd /etc/yum.repo.d/  1.创建一个repo_bak目录,用于保存系统中原来yum的repo文件. sudo mkdir rep ...