1、创建一个自己的servlet
package com.test.controller.com.servlet; import javax.servlet.ServletException; import javax.servlet.annotation.WebServlet; import javax.servlet.http.HttpServlet; import javax.servlet.http.HttpServletRequest; import javax.servlet.http.HttpServletResponse; import java.io.IOException; @WebServlet("/hello") public class HelloServlet extends HttpServlet { @Override protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException { doPost(req, resp); } @Override protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException { System.out.println("sevlet''''''''''''''''''''''''''''''''''''''''''''''''"); super.doPost(req, resp); } } |
2、使@WebServlet注解生效,需要在启动类中添加servlet路径扫描:例子(@ServletComponentScan("com.test.controller.com.servlet"))
package com.test; import org.springframework.boot.SpringApplication; import org.springframework.boot.autoconfigure.SpringBootApplication; import org.springframework.boot.web.servlet.ServletComponentScan; import org.springframework.context.annotation.ComponentScan; import org.springframework.context.annotation.EnableAspectJAutoProxy; import org.springframework.web.servlet.config.annotation.EnableWebMvc; @EnableAspectJAutoProxy @SpringBootApplication @ComponentScan("com.test.controller.com.socket") @ServletComponentScan("com.test.controller.com.servlet") //添加servlet扫描路径 public class MockApplication { public static void main(String[] args) { new SpringApplication(MockApplication.class).run(args); } } |