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請求內容請搜索好吧啦網以前的文章或繼續瀏覽下面的相關文章希望大家以后多多支持好吧啦網!
相關文章:
1. java實現圖形化界面計算器2. IntelliJ Idea2017如何修改緩存文件的路徑3. IntelliJ IDEA設置條件斷點的方法步驟4. IIS Express 取代 ASP.NET Development Server的配置方法5. python flask框架快速入門6. Spring-Richclient 0.1.0 發布7. javascript設計模式 ? 建造者模式原理與應用實例分析8. 淺談SpringMVC jsp前臺獲取參數的方式 EL表達式9. Python使用oslo.vmware管理ESXI虛擬機的示例參考10. Express 框架中使用 EJS 模板引擎并結合 silly-datetime 庫進行日期格式化的實現方法
