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);
}
}
到此這篇關(guān)于SpringBoot里使用Servlet進行請求的實現(xiàn)示例的文章就介紹到這了,更多相關(guān)SpringBoot Servlet請求內(nèi)容請搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!
- 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種方式總結(jié)
- Springboot如何添加server.servlet.context-path相關(guān)使用
- SpringBoot項目找不到j(luò)avax.servlet.Filter的問題及解決
- SpringBoot如何切換成其它的嵌入式Servlet容器(Jetty和Undertow)
相關(guān)文章
SpringBoot2.0+阿里巴巴Sentinel動態(tài)限流實戰(zhàn)(附源碼)
這篇文章主要介紹了SpringBoot2.0+阿里巴巴Sentinel動態(tài)限流實戰(zhàn)(附源碼),文中通過示例代碼介紹的非常詳細,對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧2020-11-11
Java操作Mongodb數(shù)據(jù)庫實現(xiàn)數(shù)據(jù)的增刪查改功能示例
這篇文章主要介紹了Java操作Mongodb數(shù)據(jù)庫實現(xiàn)數(shù)據(jù)的增刪查改功能,結(jié)合完整實例形式分析了java針對MongoDB數(shù)據(jù)庫的連接、增刪改查等相關(guān)操作技巧,需要的朋友可以參考下2017-08-08
Java中String字符串轉(zhuǎn)具體對象的幾種常用方式
String對象可以用來存儲任何字符串類型的數(shù)據(jù),包括HTML、XML等格式的字符串,下面這篇文章主要給大家介紹了關(guān)于JavaString字符串轉(zhuǎn)具體對象的幾種常用方式,需要的朋友可以參考下2024-03-03
Java使用StampedLock實現(xiàn)高效讀寫功能
StampedLock 是 Java 8 引入的高性能鎖,提供了三種鎖模式:寫鎖、悲觀讀鎖和樂觀讀鎖,與傳統(tǒng)的 ReentrantReadWriteLock 相比,StampedLock 更注重性能,特別適合讀多寫少的場景,所以本文給大家介紹了Java使用StampedLock實現(xiàn)高效讀寫功能,需要的朋友可以參考下2025-01-01

