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

 更新时间:2021年01月12日 14:27:40   作者:天龙至尊  
这篇文章主要介绍了SpringBoot里使用Servlet进行请求的实现示例,文中通过示例代码介绍的非常详细,对大家的学习或者工作具有一定的参考学习价值,需要的朋友们下面随着小编来一起学习学习吧

首先,在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请求内容请搜索脚本之家以前的文章或继续浏览下面的相关文章希望大家以后多多支持脚本之家!

相关文章

  • maven打包失败踩坑的解决方式详解

    maven打包失败踩坑的解决方式详解

    最近因为新项目有很多自定义的jar包,而且占内存很多,就直接拷贝过来,新开了一个maven仓库,用的时候没有问题,但是打包一直编译不通过,下面这篇文章主要给大家介绍了关于maven打包失败踩坑的解决方式,需要的朋友可以参考下
    2023-05-05
  • springboot vue接口测试定义编辑功能的实现

    springboot vue接口测试定义编辑功能的实现

    这篇文章主要为大家介绍了springboot vue接口测试定义编辑功能的实现,有需要的朋友可以借鉴参考下,希望能够有所帮助,祝大家多多进步,早日升职加薪
    2022-05-05
  • java实现单机版五子棋

    java实现单机版五子棋

    这篇文章主要为大家详细介绍了java实现单机版五子棋源码,以及五子棋游戏需要的实现,具有一定的参考价值,感兴趣的小伙伴们可以参考一下
    2018-03-03
  • SpringBoot基本web开发demo过程解析

    SpringBoot基本web开发demo过程解析

    这篇文章主要介绍了SpringBoot基本web开发demo过程解析,文中通过示例代码介绍的非常详细,对大家的学习或者工作具有一定的参考学习价值,需要的朋友可以参考下
    2019-11-11
  • springboot如何解决跨域后session获取不到sessionId不一致

    springboot如何解决跨域后session获取不到sessionId不一致

    这篇文章主要介绍了springboot如何解决跨域后session获取不到sessionId不一致问题,具有很好的参考价值,希望对大家有所帮助,如有错误或未考虑完全的地方,望不吝赐教
    2024-01-01
  • Java可变参数的应用小结

    Java可变参数的应用小结

    这篇文章主要介绍了Java可变参数的应用小结,实现同一个函数名,不同参数个数,实现的方法相同,本文通过实例代码给大家介绍的非常详细,对大家的学习或工作具有一定的参考借鉴价值,需要的朋友可以参考下
    2023-10-10
  • IDEA 离线迁移Springboot工程的方法步骤

    IDEA 离线迁移Springboot工程的方法步骤

    这篇文章主要介绍了IDEA 离线迁移Springboot工程的方法步骤,文中通过示例代码介绍的非常详细,对大家的学习或者工作具有一定的参考学习价值,需要的朋友们下面随着小编来一起学习学习吧
    2020-06-06
  • 浅谈Java中spring 线程异步执行

    浅谈Java中spring 线程异步执行

    这篇文章主要介绍了浅谈spring 线程异步执行,文中通过示例代码介绍的非常详细,对大家的学习或者工作具有一定的参考学习价值,需要的朋友们下面随着小编来一起学习学习吧
    2020-07-07
  • SpringBoot jar包大小优化问题及解决

    SpringBoot jar包大小优化问题及解决

    这篇文章主要介绍了SpringBoot jar包大小优化问题及解决方案,具有很好的参考价值,希望对大家有所帮助。如有错误或未考虑完全的地方,望不吝赐教
    2022-11-11
  • SpringBoot中使用多线程的方法示例

    SpringBoot中使用多线程的方法示例

    这篇文章主要介绍了SpringBoot中使用多线程的方法示例,文中通过示例代码介绍的非常详细,对大家的学习或者工作具有一定的参考学习价值,需要的朋友们下面随着小编来一起学习学习吧
    2021-03-03

最新评论