怎么在SpringBoot中利用Servlet发送请求

发布时间:2021-01-12 18:03:56 作者:Leah
来源:亿速云 阅读:359

这篇文章将为大家详细讲解有关怎么在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发送请求就分享到这里了,希望以上内容可以对大家有一定的帮助,可以学到更多知识。如果觉得文章不错,可以把它分享出去让更多的人看到。

推荐阅读:
  1. springboot发送http请求
  2. vue中怎么利用resource发送请求

免责声明:本站发布的内容(图片、视频和文字)以原创、转载和分享为主,文章观点不代表本网站立场,如果涉及侵权请联系站长邮箱:is@yisu.com进行举报,并提供相关证据,一经查实,将立刻删除涉嫌侵权内容。

springboot servlet

上一篇:如何在IDEA中部署一个tomcat项目

下一篇:怎么对postgresql的存储目录进行修改

相关阅读

您好,登录后才能下订单哦!

密码登录
登录注册
其他方式登录
点击 登录注册 即表示同意《亿速云用户服务条款》