Spring Boot War를 local Tomcat 서버에 이관

2021. 3. 8. 12:54Java/Spring Boot

SpringBootServletInitializer를 상속받도록 처리

SpringApplicationBuilder를 override

package com.example.demo;

import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.boot.builder.SpringApplicationBuilder;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.boot.web.servlet.support.SpringBootServletInitializer;;

@SpringBootApplication
@RestController
public class DemoApplication extends SpringBootServletInitializer{

	public static void main(String[] args) {
		SpringApplication.run(DemoApplication.class, args);
	}
	
	@Override
	protected SpringApplicationBuilder configure(SpringApplicationBuilder builder) {
		return builder.sources(DemoApplication.class);
	}
	
	@GetMapping("/hello")
	public String hello(@RequestParam(value="name", defaultValue="World") String name) {
		return String.format("Hello %s!", name);
	}
}

Tomcat 서버 server.xml에 Context 추가

      <Host name="localhost"  appBase="webapps"
            unpackWARs="true" autoDeploy="true">
          <Context docBase="demo-0.0.1-SNAPSHOT" path="/" reloadable="false"/>