如何在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. pip批量安装库

    将需要安装的库名和版本号都写在一个txt文档中,每个库名占一行,例如requests==2.24.0. 然后在用pip install -r命令去找到这个txt文档批量安装里面填写的库,如果嫌速度太慢 ...

  2. springmvc学习指南 之---第24篇 国际化问题

    writedby 张艳涛,今天一天就搞了一个这个问题,主要是下路,遇到springmvc-config.web的配置和拦截器的使用问题, 看了几天的spring发现都没讲拦截器,之前看了两天sprin ...

  3. Spring最简单构建一个后台{msg:"登录成功",code:200,data:null}

    一.简介 {msg:"登录成功",code:200,data:null} 二.两种请求 如果严格msg code data也带"" @RestControlle ...

  4. HTTP_CLIENT_IP、HTTP_X_FORWARDED_FOR、REMOTE_ADDR

    REMOTE_ADDR 是你的客户端跟你的服务器"握手"时候的IP.如果使用了"匿名代理",REMOTE_ADDR将显示代理服务器的IP. HTTP_CLIEN ...

  5. 【Maven实战技巧】「插件使用专题」Maven-Assembly插件实现自定义打包

    前提概要 最近我们项目越来越多了,然后我就在想如何才能把基础服务的打包方式统一起来,并且可以实现按照我们的要求来生成,通过研究,我们通过使用maven的assembly插件完美的实现了该需求,爽爆了有 ...

  6. 免杀mimikatz

    mimikatz源码  下载地址https://github.com/gentilkiwi/mimikatz/releases/tag/2.2.0-20210709 使用vs2019打开工程mimik ...

  7. Spring Cloud Alibaba - RestTemplate

    Spring Cloud Alibaba - RestTemplate Controller导入依赖和相关属性 @SuppressWarnings("all") @RestCont ...

  8. connect()函数阻塞问题

    方法一:采用select 在学习嵌入式Linux网络编程中,很多同学都发现了一个问题,那就是调用connect函数时,如果服务端关闭,客户 端调用connect()函数时,发现阻塞在那里,而且利用ct ...

  9. DFT、DTFT、DFS、FFT之间的关系

    DFT.DTFT.DFS.FFT.FT.FS之间的关系 FT和FS是研究连续信号的,在数字信号处理中不涉及. 主要是前四种的关系: DFT(Discrete Fourier Transform):离散 ...

  10. 基于typescript编写vue的ts文件语法模板

    1 <template> 2 <div> 3 <input v-model="msg"> 4 <p>prop: {{ propMes ...