欢迎来到代码驿站!

JAVA代码

当前位置:首页 > 软件编程 > JAVA代码

SpringBoot里使用Servlet进行请求的实现示例

时间:2022-08-07 08:58:33|栏目:JAVA代码|点击:

首先,在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实现配置本地访问端口及路径

栏    目:JAVA代码

下一篇:Java编程在方法中哪些时候需要参数

本文标题:SpringBoot里使用Servlet进行请求的实现示例

本文地址:http://www.codeinn.net/misctech/210069.html

推荐教程

广告投放 | 联系我们 | 版权申明

重要申明:本站所有的文章、图片、评论等,均由网友发表或上传并维护或收集自网络,属个人行为,与本站立场无关。

如果侵犯了您的权利,请与我们联系,我们将在24小时内进行处理、任何非本站因素导致的法律后果,本站均不负任何责任。

联系QQ:914707363 | 邮箱:codeinn#126.com(#换成@)

Copyright © 2020 代码驿站 版权所有