Spring:Boot
lastmod 2018-08-17created 2015-04-2517 edits
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
Logging with slf4j and logback in Spring Boot can be applied as follows.
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>
Then use it as follows wherever logging is needed.
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
A warning message like the following may be printed.
This usually appears when implemented as follows:
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);
}
}
Change it as follows:
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
- [Recommended] Giving Spring some REST 1
- Introduction to Spring Boot
- Impressions after looking into Spring Boot
- Now is the time to use Spring Boot
- Spring Boot: Simple RestAPI and JPA example
- [Recommended] spring-boot-samples
- [Recommended] Spring Boot: Simple RestAPI and JPA example
- Using MySQL with Spring Boot JPA
- Spring Boot Data JPA configuration
- Spring Data – JPA basic workflow
Spring Documentation
- Converting a Spring Boot JAR Application to a WAR
- Deploying Spring Boot Applications
- Packaging executable jar and war files
- [Recommended] 74. Traditional deployment: 74.1 Create a deployable war file
Logging
- 66. Logging
- Bootstrap an application with Spring Boot – Part1 Command-line
- [Recommended] 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
References
Pages that link here