标签:ret pom int nts pos pat 文件 get class
1.Demo
(1)pom.xml文件
<?xml version="1.0" encoding="UTF-8"?> <project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://maven.apache.org/POM/4.0.0
http://maven.apache.org/xsd/maven-4.0.0.xsd"> <modelVersion>4.0.0</modelVersion> <parent> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-parent</artifactId> <version>1.5.21.RELEASE</version> <relativePath/> <!-- lookup parent from repository --> </parent> <groupId>com.mxj</groupId> <artifactId>01-springboot-hello</artifactId> <version>1.0-SNAPSHOT</version> <!--修改jdk版本--> <properties> <java.version>1.8</java.version> </properties> <!--springboot启动器--> <dependencies> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-web</artifactId> </dependency> </dependencies> </project>
(2)配置Controller
@Controller public class Helloworld { @RequestMapping("/hello") @ResponseBody public Map<String,Object> showHelloWorld(){ Map<String,Object> map = new HashMap<>(); map.put("msg","HelloWorld"); return map; } }
(3)编写启动器
注意:启动器存放的位置可以和Controller位于同一个包下,或者位于Controller的上一级包中,但是不能放在Controller的平级以及子包下
/** * SpringBoot 启动类 */ @SpringBootApplication public class App { public static void main(String[] args){ SpringApplication.run(App.class,args); } }
2.SpringBoot整合Servlet
(1)通过注解扫描完成Servlet组件的注册
编写Servlet
@WebServlet(name="FirstServlet",urlPatterns="/first") public class FirstServlet extends HttpServlet { @Override protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException { super.doGet(req, resp); } }
编写启动类
@SpringBootApplication @ServletComponentScan //在springBoot启动时会扫描@WebServlet,并将该类实例化 public class App { public static void main(String[] args){ SpringApplication.run(App.class,args); } }
(2)通过方法完成Servlet组件的注册
编写Servlet
public class SecondServlet extends HttpServlet { @Override protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException { System.out.println("SecondServlet.............."); } }
编写启动类
@SpringBootApplication public class App2 { public static void main(String[] args){ SpringApplication.run(App2.class,args); } @Bean public ServletRegistrationBean getServletRegistrationBean(){ ServletRegistrationBean bean = new ServletRegistrationBean(new SecondServlet()); bean.addUrlMappings("/second"); return bean; } }
标签:ret pom int nts pos pat 文件 get class
原文地址:https://www.cnblogs.com/mxj961116/p/11286821.html