SpringBoot里使用Servlet进行请求的实现示例
更新时间:2021年01月12日 14:27:40 作者:天龙至尊
这篇文章主要介绍了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请求内容请搜索脚本之家以前的文章或继续浏览下面的相关文章希望大家以后多多支持脚本之家!
您可能感兴趣的文章:
- SpringBoot集成WebServlet出现自定义servlet请求失败的问题解决方案
- springboot扫描自定义的servlet和filter代码详解
- Springboot注入成员变量HttpServletRequest的原理分析
- SpringBoot3.1.2 引入Swagger报错Type javax.servlet.http.HttpServletRequest not present解决办法
- 解决IDEA启动springboot项目报错java.lang.ClassNotFoundException: javax.servlet.ServletContext
- SpringBoot获取HttpServletRequest的3种方式总结
- Springboot如何添加server.servlet.context-path相关使用
- SpringBoot项目找不到javax.servlet.Filter的问题及解决
- SpringBoot如何切换成其它的嵌入式Servlet容器(Jetty和Undertow)
相关文章
@RequestParam 和@RequestBody注解的区别解析
在 Spring MVC 中,我们可以使用 @RequestParam 和 @RequestBody 来获取请求参数,但它们在用法和作用上有一些区别,这篇文章主要介绍了@RequestParam 和@RequestBody注解的区别,需要的朋友可以参考下2023-06-06
Spring Controller接收前端JSON数据请求方式
这篇文章主要为大家介绍了Spring Controller接收前端JSON数据请求方式详解,有需要的朋友可以借鉴参考下,希望能够有所帮助,祝大家多多进步,早日升职加薪2023-07-07
Spring Boot实现分布式系统中的服务发现和注册(最新推荐)
在本文中,我们深入探讨了Spring Boot如何实现分布式系统中的服务发现和注册,我们使用Eureka作为服务注册中心,Ribbon作为负载均衡器,Hystrix作为熔断器,成功地实现了服务发现、服务注册、负载均衡和服务熔断等功能,需要的朋友参考下吧2023-06-06


最新评论