码迷,mamicode.com
首页 > 编程语言 > 详细

SpringBoot

时间:2019-08-02 10:41:32      阅读:105      评论:0      收藏:0      [点我收藏+]

标签: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;
    }
}

 

SpringBoot

标签:ret   pom   int   nts   pos   pat   文件   get   class   

原文地址:https://www.cnblogs.com/mxj961116/p/11286821.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!