Spring:Boot
Quick start
<code>pom.xml</code>:
<parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>1.2.3.RELEASE</version>
</parent>
<dependencies>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
</dependencies>
hello/SampleController.java
:
package hello;
import org.springframework.boot.*;
import org.springframework.boot.autoconfigure.*;
import org.springframework.stereotype.*;
import org.springframework.web.bind.annotation.*;
@Controller
@EnableAutoConfiguration
public class SampleController {
@RequestMapping("/")
@ResponseBody
String home() {
return "Hello World!";
}
public static void main(String[] args) throws Exception {
SpringApplication.run(SampleController.class, args);
}
}
Logging
Spring Boot에서 slf4j와 logback을 사용한 로깅을 적용하는 방법은 아래와 같다.
pom.xml
:
<dependencies>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
<exclusions>
<!-- Exclude Commons Logging in favor of SLF4j -->
<exclusion>
<groupId>commons-logging</groupId>
<artifactId>commons-logging</artifactId>
</exclusion>
</exclusions>
</dependency>
<!-- Logback -->
<dependency>
<groupId>ch.qos.logback</groupId>
<artifactId>logback-classic</artifactId>
</dependency>
<dependency>
<groupId>org.slf4j</groupId>
<artifactId>jcl-over-slf4j</artifactId>
</dependency>
</dependencies>
src/main/resources/logback.xml
:
<?xml version="1.0" encoding="UTF-8"?>
<configuration>
<appender name="consoleAppender" class="ch.qos.logback.core.ConsoleAppender">
<encoder>
<Pattern>[%d{HH:mm:ss.SSS} %thread] %-5level %logger{36} - %msg %n</Pattern>
</encoder>
<filter class="ch.qos.logback.classic.filter.ThresholdFilter">
<level>TRACE</level>
</filter>
</appender>
<logger name="FirebellRestLogger" additivity="false">
<level value="DEBUG" />
<appender-ref ref="consoleAppender" />
</logger>
<root>
<level value="INFO" />
<appender-ref ref="consoleAppender" />
</root>
</configuration>
이후 로깅이 필요한 위치에서 아래와 같이 사용하면 된다.
src/main/java/LoginMap.java
:
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;
@RestController
public final class TestMap {
private static final Logger logger = LoggerFactory.getLogger(LoginMap.class);
@RequestMapping(value = "/test")
public String test() {
logger.info("Test!");
return "Test!";
}
}
Troubleshooting
HttpMapperProperties deprecated
아래와 같은 경고 메시지가 출력될 수 있다.
이 경우 보통 아래와 같은 방식으로 구현할 경우 나타난다:
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
@SpringBootApplication
public class Application {
public static void main(String[] args) {
SpringApplication.run(Application.class, args);
}
}
이 것을 아래와 같이 변경하면 된다:
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.boot.builder.SpringApplicationBuilder;
import org.springframework.boot.context.web.SpringBootServletInitializer;
@SpringBootApplication
public class Application extends SpringBootServletInitializer {
@Override
protected SpringApplicationBuilder configure(SpringApplicationBuilder application) {
return application.sources(Application.class);
}
public static void main(String[] args) throws Exception {
SpringApplication.run(Application.class, args);
}
}
Favorite site
- [추천] Giving Spring some REST 1
- Spring Boot 소개
- Spring boot 살펴본 느낌
- 이제는 Spring Boot를 써야할 때다
- Spring Boot: 간단한 RestAPI 및 JPA 예제
- [추천] spring-boot-samples
- [추천] Spring Boot: 간단한 RestAPI 및 JPA 예제
- Spring Boot JPA에서 MySQL 사용하기
- Spring Boot Data JPA 설정
- Spring Data – JPA 기본 작업 흐름
Spring Documentation
- Converting a Spring Boot JAR Application to a WAR
- Deploying Spring Boot Applications
- Packaging executable jar and war files
- [추천] 74. Traditional deployment: 74.1 Create a deployable war file
Logging
- 66. Logging
- Bootstrap an application with Spring Boot – Part1 Command-line
- [추천] How to log in Spring with SLF4J and Logback 2
- Stackoverflow: Spring Boot: How can I set the logging level with application.properties?
- Software configuration with Spring Boot
- Spring Boot Logging
- spring-boot-sample-web-ui project site