Spring boot中注册Servlet

如何在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

搞定!

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值