欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页  >  IT编程

SpringBoot里使用Servlet进行请求的实现示例

程序员文章站 2022-07-05 09:10:51
首先,在main方法的类上添加注解:@servletcomponentscan(basepackages = "application.servlet")示例代码:package applicatio...

首先,在main方法的类上添加注解:

@servletcomponentscan(basepackages = "application.servlet")

示例代码:

package application; 
import io.seata.spring.annotation.datasource.enableautodatasourceproxy;
import javafx.application.application;
import javafx.fxml.fxmlloader;
import org.mybatis.spring.annotation.mapperscan;
import org.springframework.boot.springapplication;
import org.springframework.boot.autoconfigure.springbootapplication;
import org.springframework.boot.builder.springapplicationbuilder;
import org.springframework.boot.web.servlet.servletcomponentscan;
import org.springframework.boot.web.servlet.support.springbootservletinitializer;
import org.springframework.cache.annotation.enablecaching;
import org.springframework.cloud.openfeign.enablefeignclients;
 
import javax.annotation.resource;
 
/**
 * @author wtl
 */
@springbootapplication
@enablefeignclients
@enablecaching
@enableautodatasourceproxy
@mapperscan(basepackages = "application.mybatis.mappers")
@servletcomponentscan(basepackages = "application.servlet")
public class springbootmain extends springbootservletinitializer {
 
  public static void main(string[] args) {
    springapplication.run(springbootmain.class,args);
    application.launch(fxmlrunner.class,args);
  }
 
  @override
  protected springapplicationbuilder configure(springapplicationbuilder builder) {
    return builder.sources(springbootmain.class);
  }
}

使用 @webservlet(name = "downloadservlet",urlpatterns = "/test") 进行使能servlet:

@webservlet(name = "downloadservlet",urlpatterns = "/test")

示例:

package application.servlet;
 
import application.service.bilibiliindexservice;
import lombok.sneakythrows;
 
import javax.annotation.resource;
import javax.servlet.*;
import javax.servlet.annotation.webservlet;
import javax.servlet.http.httpservlet;
import javax.servlet.http.httpservletrequest;
import javax.servlet.http.httpservletresponse;
import java.io.ioexception;
 
/**
 * @author: wtl
 * @date: 2020/7/5
 * @time: 18:48
 * @description:
 */
@webservlet(name = "downloadservlet",urlpatterns = "/test")
public class downloadservlet extends httpservlet {
 
  @resource
  private bilibiliindexservice bilibiliindexservice;
 
  @sneakythrows
  @override
  protected void doget(httpservletrequest httpservletrequest, httpservletresponse httpservletresponse) throws servletexception, ioexception {
    string aid = httpservletrequest.getparameter("aid");
    string cid = httpservletrequest.getparameter("cid");
    bilibiliindexservice.getvideostream(aid,cid,httpservletrequest,httpservletresponse);
  }
}

到此这篇关于springboot里使用servlet进行请求的实现示例的文章就介绍到这了,更多相关springboot servlet请求内容请搜索以前的文章或继续浏览下面的相关文章希望大家以后多多支持!