๋ณธ๋ฌธ ๋ฐ”๋กœ๊ฐ€๊ธฐ
JAVA/Spring

[JAVA] Spring Boot ํ™˜๊ฒฝ์—์„œ ํ†ฐ์บฃ ์‚ฌ์šฉํ•˜๊ธฐ

by soy๋ฏธ๋‹ˆ 2021. 9. 27.

 

 

 

1. Eclipse MarketPlace ์— sts ๊ฒ€์ƒ‰ํ•ด์„œ Spring Tools 4 (aka Spring Tool Suite 4) ์„ค์น˜

 

 

 

2. File - New - Other - Spring Boot ๊ฒ€์ƒ‰ํ•ด์„œ ์Šคํ”„๋ง ์›น ํ”„๋กœ์ ํŠธ ์ƒ์„ฑ

 

 

3. ํ”„๋กœ์ ํŠธ ๊ฒฝ๋กœ ์„ค์ •

  • Spring Boot ์—์„œ๋Š” jsp ํŒŒ์ผ ์ง€์›์„ ์•ˆํ•˜๊ธฐ ๋•Œ๋ฌธ์— jsp ํŒŒ์ผ์„ ์‚ฌ์šฉํ•˜๋ ค๋ฉด jsp ํŒŒ์ผ ๊ฒฝ๋กœ๋ฅผ ์ •ํ•œ ๋‹ค์Œ application.properties ํŒŒ์ผ์— view ๊ฒฝ๋กœ์— ๋Œ€ํ•œ ์„ค์ •์„ ์ถ”๊ฐ€ํ•ด ์ฃผ์–ด์•ผ ํ•œ๋‹ค.
// application.properties
server.port=8080
spring.mvc.view.prefix=/WEB-INF/Views/
spring.mvc.view.suffix=.jsp

 

 

Project ์šฐํด๋ฆญ - Run As - Spring Boot App ์œผ๋กœ ๋นŒ๋“œํ•˜๊ณ  ํŽ˜์ด์ง€๋ฅผ ๋กœ๋“œํ•˜๋ฉด Spring Boot ์ž์ฒด ๋‚ด์žฅ ํ†ฐ์บฃ์„ ์‚ฌ์šฉํ•˜๊ธฐ ๋•Œ๋ฌธ์— ๋นŒ๋“œ์— ํฐ ๋ฌธ์ œ๋Š” ์—†์—ˆ๋‹ค. localhost:8080 ์œผ๋กœ ์ ‘์†ํ•˜๋‹ˆ Whitelabel Error Page ๊ฐ€ ๋œจ๋ฉด์„œ 8080 ํฌํŠธ๋กœ ์ ‘์†์ด ์•ˆ๋œ๋‹ค๋Š” ์—๋Ÿฌ๋ฌธ๊ตฌ๊ฐ€ ๋œจ๊ธธ๋ž˜ application.properties ํŒŒ์ผ์˜ server.port ๋ถ€๋ถ„์„ ์ˆ˜์ •ํ•ด์„œ ๋‹ค๋ฅธ ํฌํŠธ๋กœ ์ ‘์†ํ•ด๋ณด์•˜๋”๋‹ˆ ์ž˜ ๋˜์—ˆ๋‹ค.

 

 

4. pom.xml ์ž‘์„ฑ

// 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 https://maven.apache.org/xsd/maven-4.0.0.xsd">
	<modelVersion>4.0.0</modelVersion>
	<packaging>war</packaging>
	<properties>
		<java.version>16</java.version>
	</properties>
	<dependencies>
		<dependency>
			<groupId>org.springframework.boot</groupId>
			<artifactId>spring-boot-starter-web</artifactId>
		</dependency>

		<dependency>
			<groupId>org.springframework.boot</groupId>
			<artifactId>spring-boot-starter-test</artifactId>
			<scope>test</scope>
		</dependency>
		
		<dependency>
			<groupId>org.apache.tomcat.embed</groupId>
			<artifactId>tomcat-embed-jasper</artifactId>
		</dependency>

		<dependency>
			<groupId>javax.servlet</groupId>
			<artifactId>jstl</artifactId>
			<version>1.2</version>
		</dependency>
	</dependencies>

	<build>
		<plugins>
			<plugin>
				<groupId>org.springframework.boot</groupId>
				<artifactId>spring-boot-maven-plugin</artifactId>
			</plugin>
		</plugins>
	</build>

</project>
  • <java.version> ํ™•์ธํ•˜๊ธฐ
  • war ํŒŒ์ผ๋กœ export ํ•  ๊ฒƒ์ด๊ธฐ ๋•Œ๋ฌธ์— <packaging>war</packaging> ๋กœ ์„ค์ •

 

 

 

 

VirtualBox์˜ ์šฐ๋ถ„ํˆฌ ํ™˜๊ฒฝ์—์„œ ํ†ฐ์บฃ์œผ๋กœ ์›น ํŽ˜์ด์ง€ ์ ‘์†ํ•ด๋ณด๊ธฐ

  • ์Šคํ”„๋ง ๋ถ€ํŠธ ํ”„๋กœ์ ํŠธ war ํŒŒ์ผ๋กœ export
  • ๋ฆฌ๋ˆ…์Šค var/lib/tomcat9/webapp ์•ˆ์— war ํŒŒ์ผ ๋“œ๋ž˜๊ทธ์•ค๋“œ๋žํ•ด์„œ ๋„ฃ๊ณ  ROOT.war ๋กœ ํŒŒ์ผ๋ช… ๋ณ€๊ฒฝ
  • sudo service tomcat9 start 

 

Whitelabel Error Page ์˜ค๋ฅ˜ ํ•ด๊ฒฐ

 

ํ•ด๊ฒฐ ๋ฐฉ์•ˆ

// pom.xml
<dependency>
	<groupId>org.springframework.boot</groupId>
	<artifactId>spring-boot-starter-tomcat</artifactId>
	<scope>provided</scope>
</dependency>
  • ์™ธ์žฅ ํ†ฐ์บฃ์œผ๋กœ ์ ‘์†์ด ๊ฐ€๋Šฅํ•˜๋„๋ก spring-boot-starter-tomcat ์˜์กด์„ฑ ์ฃผ์ž… <scope>provided</scope>

 

// Sts01Application.java

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

@SpringBootApplication
public class Sts01Application extends SpringBootServletInitializer{

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

	protected SpringApplicationBuilder configure(SpringApplicationBuilder application) {
		return application.sources(Sts01Application.class);
	}

}
  • Application.java ์—์„œ SpringBootServletInitializer ์ƒ์†๋ฐ›๊ณ  configure ๋ฉ”์„œ๋“œ ์ž‘์„ฑ

 

 

๋Œ“๊ธ€