您好,登錄后才能下訂單哦!
這篇文章將為大家詳細講解有關怎么在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發送請求就分享到這里了,希望以上內容可以對大家有一定的幫助,可以學到更多知識。如果覺得文章不錯,可以把它分享出去讓更多的人看到。
免責聲明:本站發布的內容(圖片、視頻和文字)以原創、轉載和分享為主,文章觀點不代表本網站立場,如果涉及侵權請聯系站長郵箱:is@yisu.com進行舉報,并提供相關證據,一經查實,將立刻刪除涉嫌侵權內容。