springboot初体验-结合maven编写接口及部署到tomcat下访问


一.idea搭建springboot项目

按照以下图片傻瓜式点击下一步

二.编写helloword接口

package com.springboot.com_springboot.demo;

/**
 * Copyright (C) @2021
 *
 * @author: tcc
 * @version: 1.0
 * @date: 2021/12/19
 * @time: 19:26
 * @description:
 */
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.servlet.ModelAndView;
import org.springframework.web.servlet.*;

/**
 * 第一个boot应用
 */
@RestController
public class HelloBoot {
    @RequestMapping(value="/hello",method = RequestMethod.GET)
    public String sayHello(){
        return "Hello world";
    }
}

编写application.properties

server.port=9999
server.servlet.context-path=/SpringBoot

编写启动类及启动main方法

package com.springboot.com_springboot;

import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;

@SpringBootApplication
public class ComSpringbootApplication {

    public static void main(String[] args) {
        SpringApplication.run(ComSpringbootApplication.class, args);
    }

}

浏览器访问:http://localhost:9999/SpringBoot/hello

三.打包成war包并且部署到tomcat

1.首先在pom.xml增加

war



org.springframework.boot
spring-boot-starter-tomcat
provided

最终pom.xml

<?xml version="1.0" encoding="UTF-8"?>

         xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 https://maven.apache.org/xsd/maven-4.0.0.xsd">
    4.0.0
    
        org.springframework.boot
        spring-boot-starter-parent
        2.6.1
         
    
    com.springboot
    com_springboot
    0.0.1
    com_springboot
    war
    Demo project for Spring Boot
    
        1.8
    
    
        
            org.springframework.boot
            spring-boot-starter
        

        
            org.springframework.boot
            spring-boot-devtools
            runtime
            true
        
        
            org.springframework.boot
            spring-boot-starter-test
            test
        
        
            org.springframework.boot
            spring-boot-starter-web
        


        
            org.springframework.boot
            spring-boot-starter-tomcat
            provided
        
    

    
        
            
                org.springframework.boot
                spring-boot-maven-plugin
            
        
    

 2.新建一个SpringBootStartApplication 继承自 SpringBootServletInitializer

 作用:因为在外部容器部署的话,就不能依赖于Application的main函数了,而是要以类似于web.xml文件配置的方式来启动Spring应用上下文,

    此时我们需要在启动类中继承SpringBootServletInitializer并实现configure方法,这个类的作用与在web.xml中配置负责初始化Spring应用上下文的监听器作用类似

package com.springboot.com_springboot;

import org.springframework.boot.builder.SpringApplicationBuilder;
import org.springframework.boot.web.servlet.support.SpringBootServletInitializer;

/**
 * Copyright (C) @2021
 *
 * @author: tcc
 * @version: 1.0
 * @date: 2021/12/19
 * @time: 20:25
 * @description:
 */
public class SpringBootStartApplication extends SpringBootServletInitializer {
    @Override
    protected SpringApplicationBuilder configure(SpringApplicationBuilder builder) {
        return builder.sources(ComSpringbootApplication.class);
    }
}

3.将springboot项目打包成war包,双击package

 4.将war包抛到tomcat的webapps下,修改war包的项目名称,运行tomcat

5.访问接口:http://localhost:8080/tcc/hello

四.和传统spring项目的不同

部署不用管jar包,本地测试不用自己部署tomcat