如何在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. odoo里面的read_group写法

    #计算数task_count = fields.Integer(compute='_compute_task_count', string="Task Count")def _co ...

  2. charles f配置sslproxy 对protobuf 接口抓包 -不推荐

    charles 配置sslproxy help-安装电脑证书 手机连接访问电脑,下载允许手机 手机连接vpn 手机访问chls.pro/ssl -下载证书 request可以看到protobuf_tx ...

  3. 深入刨析tomcat 之---第10篇 how tomcat works 第13章,Response 发送错误信息 sendError

    writedby 张艳涛 在浏览器中发送一个错误应用url 那么tomcat是如何发送错误的呢? 基本上是发送http 的response协议,分为两部分一部分是response设置头信息, 那么先分 ...

  4. 浅析线程池 ThreadPoolExecutor 源码

    首先看下类的继承关系,不多介绍: public interface Executor {void execute(Runnable);} public interface ExecutorServic ...

  5. 在 CentOS 7.5 64位上使用 yum 安装 MySQL 8.0

    前段时间在 CentOS 7.5 64位上安装 MySQL 8.0.查了些资料,在这里记录一下详细的安装和设置步骤. 一.安装 使用yum安装MySQL之前需要先下载对应的.rpm文件,下载方法: 去 ...

  6. FreeRTOS-04-内核控制函数+时间管理函数

    说明 本文仅作为学习FreeRTOS的记录文档,作为初学者肯定很多理解不对甚至错误的地方,望网友指正. FreeRTOS是一个RTOS(实时操作系统)系统,支持抢占式.合作式和时间片调度.适用于微处理 ...

  7. 防火墙和SElinux简单配置

    1.查看防火墙状态 # firewall-cmd --state          &    #systemctl status  firewalld.service 2.停止与开启firew ...

  8. JavaScript学习04(标准对象)

    标准对象 在JavaScript的世界里,一切都是对象. 但是某些对象还是和其他对象不太一样.为了区分对象的类型,我们用 typeof 操作符获取对象的类型,它总是返回一个字符串: typeof 12 ...

  9. 【笔记】使用scikit-learn解决回归问题

    使用sklearn解决回归问题 依然是加载数据 import numpy as np import matplotlib.pyplot as plt from sklearn import datas ...

  10. SpringBoot开发十八-显示评论

    需求介绍 显示评论,还是我们之前做的流程. 数据层:根据实体查询一页的评论数据,以及根据实体查询评论的数量 业务层:处理查询评论的业务,处理查询评论数量的业务 表现层:同时显示帖子详情数据时显示该帖子 ...