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

网友投稿 241 2022-11-07


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);

}

}


版权声明:本文内容由网络用户投稿,版权归原作者所有,本站不拥有其著作权,亦不承担相应法律责任。如果您发现本站中有涉嫌抄袭或描述失实的内容,请联系我们jiasou666@gmail.com 处理,核实后本网站将在24小时内删除侵权内容。

上一篇:java中PriorityBlockingQueue的入队知识点总结
下一篇:Java原生操作JDBC连接以及原理详解
相关文章

 发表评论

暂时没有评论,来抢沙发吧~