SpringBoot里使用Servlet進行請求的實現(xiàn)示例
首先,在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進行請求的實現(xiàn)示例的文章就介紹到這了,更多相關SpringBoot Servlet請求內(nèi)容請搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關文章希望大家以后多多支持腳本之家!
- SpringBoot集成WebServlet出現(xiàn)自定義servlet請求失敗的問題解決方案
- springboot掃描自定義的servlet和filter代碼詳解
- Springboot注入成員變量HttpServletRequest的原理分析
- SpringBoot3.1.2 引入Swagger報錯Type javax.servlet.http.HttpServletRequest not present解決辦法
- 解決IDEA啟動springboot項目報錯java.lang.ClassNotFoundException:?javax.servlet.ServletContext
- SpringBoot獲取HttpServletRequest的3種方式總結
- Springboot如何添加server.servlet.context-path相關使用
- SpringBoot項目找不到javax.servlet.Filter的問題及解決
- SpringBoot如何切換成其它的嵌入式Servlet容器(Jetty和Undertow)
相關文章
@RequestParam?和@RequestBody注解的區(qū)別解析
在 Spring MVC 中,我們可以使用 @RequestParam 和 @RequestBody 來獲取請求參數(shù),但它們在用法和作用上有一些區(qū)別,這篇文章主要介紹了@RequestParam?和@RequestBody注解的區(qū)別,需要的朋友可以參考下2023-06-06
擴展Hibernate使用自定義數(shù)據(jù)庫連接池的方法
這篇文章主要介紹了擴展Hibernate使用自定義數(shù)據(jù)庫連接池的方法,涉及Hibernate數(shù)據(jù)庫操作擴展的相關技巧,需要的朋友可以參考下2016-03-03
springboot實現(xiàn)修改請求狀態(tài)404改為200
這篇文章主要介紹了springboot實現(xiàn)修改請求狀態(tài)404改為200方式,具有很好的參考價值,希望對大家有所幫助。如有錯誤或未考慮完全的地方,望不吝賜教2023-07-07
Spring Controller接收前端JSON數(shù)據(jù)請求方式
這篇文章主要為大家介紹了Spring Controller接收前端JSON數(shù)據(jù)請求方式詳解,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進步,早日升職加薪2023-07-07
Spring?Boot實現(xiàn)分布式系統(tǒng)中的服務發(fā)現(xiàn)和注冊(最新推薦)
在本文中,我們深入探討了Spring?Boot如何實現(xiàn)分布式系統(tǒng)中的服務發(fā)現(xiàn)和注冊,我們使用Eureka作為服務注冊中心,Ribbon作為負載均衡器,Hystrix作為熔斷器,成功地實現(xiàn)了服務發(fā)現(xiàn)、服務注冊、負載均衡和服務熔斷等功能,需要的朋友參考下吧2023-06-06

