Spring Boot 에러페이지 설정


Spring Boot는 xml 파일을 강요하지 않기 때문에 프로젝트를 생성하면 web.xml 파일이 자동 생성되지 않는다.

물론 web.xml 파일을 만들어 에러페이지를 설정하는 등 방법은 여러가지이다.

다음은 스프링부트에서 에러페이지를 심플하게 설정하는 방법 중 하나이다. 


발생하는 에러코드(HttpStatus)에 나타낼 ErrorPage를 설정해 컨테이너에 추가한다.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
@Configuration
public class ErrorConfig {
 
  @Bean
  public EmbeddedServletContainerCustomizer containerCustomizer() {
    return new EmbeddedServletContainerCustomizer() {
 
      @Override
      public void customize(ConfigurableEmbeddedServletContainer container) {
        ErrorPage error403Page = new ErrorPage(HttpStatus.FORBIDDEN, "/403.html");
        ErrorPage error404Page = new ErrorPage(HttpStatus.NOT_FOUND, "/404.html");
        ErrorPage error500Page = new ErrorPage(HttpStatus.INTERNAL_SERVER_ERROR, "/500.html");
 
        container.addErrorPages(error403Page, error404Page, error500Page);
      }
    };
  }
 
}
cs


src/main/resources/static 하단 경로에 보여줄 에러페이지를 생성한다.


:설정한 에러코드가 발생하면 해당하는 에러 화면을 보여준다. 


'Development > SpringBoot' 카테고리의 다른 글

Spring / Spring Boot 설정 비교  (0) 2016.05.15
Spring Boot 외장 톰캣 연동  (0) 2016.05.14
Spring Boot 소개  (0) 2016.04.17

Spring / Spring Boot 설정 비교


Spring Boot의 강점으로 Spring에서의 복잡한 설정들을 심플하게 할 수 있다.

Spring / Spring Boot 간의 차이를 간단한 예제로 비교해봤다. (Spring : xml 설정 방식 사용)


* Spring

servlet-context.xml (JSP View)

root-context.xml (MySQL 연동)

logback-spring.xml (Log 설정)

pom.xml (의존 라이브러리 설정)

web.xml (필터/리스너 설정)


* Spring Boot

pom.xml 

spring-boot-starter-parent가 의존 라이브러리들의 표준 버전을 정의한다.

각 spring-boot-starter 모듈이 해당 모듈을 사용하는데 관련된 설정을 한다.

application.properties (xml 파일 대신 권장)


간단한 프로젝트를 구성하는데도 Spring에서는 설정이 매우 복잡하였으나 Spring Boot에서는 심플하게 설정할 수 있다.

'Development > SpringBoot' 카테고리의 다른 글

Spring Boot 에러페이지 설정  (0) 2016.05.18
Spring Boot 외장 톰캣 연동  (0) 2016.05.14
Spring Boot 소개  (0) 2016.04.17
Spring Boot 외장 톰캣 연동

Spring Boot는 기본적으로 내장 톰캣을 지원한다. 
다음은 내장 톰캣이 아닌 외장 톰캣을 사용하는 방법이다.

1) 톰캣 설치 후 서비스 등록/실행

2) pom.xml 설정
* dependency 추가 
1
2
3
4
5
  <dependency>
    <groupId>org.springframework.boot</groupId>
    <artifactId>spring-boot-starter-tomcat</artifactId>
    <scope>provided</scope>
  </dependency>
cs


* WAR 패키징 (jar->war)

3) Initialize 추가

SpringBootServletInitializer를 상속 받아 SpringApplicationBuiler 메소드를 오버라이딩 해준다.

1
2
3
4
5
6
7
8
public class AppInitializer extends SpringBootServletInitializer {
 
  @Override
  protected SpringApplicationBuilder configure(SpringApplicationBuilder application) {
    return application.sources(Application.class);
  }
 
}
cs

4) 톰캣경로\conf\server.xml 수

<Host name="localhost"  appBase="프로젝트 경로"
         unpackWARs="true" autoDeploy="true"
         xmlValidation="false" xmlNamespaceAware="false">

5) 톰캣경로\bin\tomcat7w.exe 실행 -> Start


'Development > SpringBoot' 카테고리의 다른 글

Spring Boot 에러페이지 설정  (0) 2016.05.18
Spring / Spring Boot 설정 비교  (0) 2016.05.15
Spring Boot 소개  (0) 2016.04.17

Spring Boot 란 ?

: 스프링 기반의 애플리케이션을 쉽고 빠르게 개발할 수 있도록 해주는 스프링 프레임워크의 서브 프로젝트



- 의존성 자동 설정 지원

- 환경설정 심플 (application.properties 지원)

- 내장 WAS(Tomcat, Jetty, Undertow) 사용으로 빠르게 실행

- 웹 애플리케이션을 JAR 파일로 패키징하여 쉽게 배포 및 실행 가능

- Actuator 서비스 운영 지원 정보 제공



* 스프링 부트 프로젝트 생성 및 실행

Web 모듈 체크해 프로젝트 생성 -> hello() 메소드 작성 -> 프로젝트 실행 -> /hello 요청 -> 화면 출력

              


@SpringBootApplication = @Configuration + @EnableAutoConfiguration + @ComponentScan 

@Configuration 

: 해당 클래스가 JavaConfig용 클래스임을 컴파일러에게 알림

@EnableAutoConfiguration 

classpath의 내용을 기반으로 다양한 설정을 자동으로 수행

@ComponentScan 

: 해당 클래스의 패키지 내부에 있는 모든 클래스를 검색해 특정 애노테이션이 표시된 자바 클래스를 찾아 DI 컨테이너에 등록


@RestController = @ResponseBody + @Controller

@ResponseBody

핸들러 메소드의 리턴 값을 HTTP Response를 통해 바로 전달

@Controller

: 해당 클래스가 웹 애플리케이션에서 요청을 받아들이는 컨트롤러 클래스임을 알림


@RequestMaping

: 해당 메소드가 HTTP 요청을 받아들이는 메소드임을 알림

SpringApplication.run() 

: 이 메소드로 애플리케이션 실행. 이 때, 첫번째 인자는 @EnableAutoConfiguration 어노테이션이 선언된 클래스명 지정


'Development > SpringBoot' 카테고리의 다른 글

Spring Boot 에러페이지 설정  (0) 2016.05.18
Spring / Spring Boot 설정 비교  (0) 2016.05.15
Spring Boot 외장 톰캣 연동  (0) 2016.05.14

+ Recent posts