SpringBoot里使用Servlet进行请求的实现示例
首先,在main方法的类上添加注解:
@ServletComponentScan(basePackages="application.servlet")
示例代码:
packageapplication;
importio.seata.spring.annotation.datasource.EnableAutoDataSourceProxy;
importjavafx.application.Application;
importjavafx.fxml.FXMLLoader;
importorg.mybatis.spring.annotation.MapperScan;
importorg.springframework.boot.SpringApplication;
importorg.springframework.boot.autoconfigure.SpringBootApplication;
importorg.springframework.boot.builder.SpringApplicationBuilder;
importorg.springframework.boot.web.servlet.ServletComponentScan;
importorg.springframework.boot.web.servlet.support.SpringBootServletInitializer;
importorg.springframework.cache.annotation.EnableCaching;
importorg.springframework.cloud.openfeign.EnableFeignClients;
importjavax.annotation.Resource;
/**
*@authorwtl
*/
@SpringBootApplication
@EnableFeignClients
@EnableCaching
@EnableAutoDataSourceProxy
@MapperScan(basePackages="application.mybatis.mappers")
@ServletComponentScan(basePackages="application.servlet")
publicclassSpringBootMainextendsSpringBootServletInitializer{
publicstaticvoidmain(String[]args){
SpringApplication.run(SpringBootMain.class,args);
Application.launch(FxmlRunner.class,args);
}
@Override
protectedSpringApplicationBuilderconfigure(SpringApplicationBuilderbuilder){
returnbuilder.sources(SpringBootMain.class);
}
}
使用@WebServlet(name="DownloadServlet",urlPatterns="/test")进行使能Servlet:
@WebServlet(name="DownloadServlet",urlPatterns="/test")
示例:
packageapplication.servlet;
importapplication.service.BiliBiliIndexService;
importlombok.SneakyThrows;
importjavax.annotation.Resource;
importjavax.servlet.*;
importjavax.servlet.annotation.WebServlet;
importjavax.servlet.http.HttpServlet;
importjavax.servlet.http.HttpServletRequest;
importjavax.servlet.http.HttpServletResponse;
importjava.io.IOException;
/**
*@author:wtl
*@Date:2020/7/5
*@Time:18:48
*@Description:
*/
@WebServlet(name="DownloadServlet",urlPatterns="/test")
publicclassDownloadServletextendsHttpServlet{
@Resource
privateBiliBiliIndexServicebiliBiliIndexService;
@SneakyThrows
@Override
protectedvoiddoGet(HttpServletRequesthttpServletRequest,HttpServletResponsehttpServletResponse)throwsServletException,IOException{
Stringaid=httpServletRequest.getParameter("aid");
Stringcid=httpServletRequest.getParameter("cid");
biliBiliIndexService.getVideoStream(aid,cid,httpServletRequest,httpServletResponse);
}
}
到此这篇关于SpringBoot里使用Servlet进行请求的实现示例的文章就介绍到这了,更多相关SpringBootServlet请求内容请搜索毛票票以前的文章或继续浏览下面的相关文章希望大家以后多多支持毛票票!