2021-08-08

This commit is contained in:
2021-08-08 20:15:27 +09:00
parent 2eef7786bd
commit 8ab9c4125b
26 changed files with 520 additions and 2 deletions

27
i18n/build.gradle.kts Normal file
View File

@@ -0,0 +1,27 @@
/*
* Spring-boot Examples
*
* Copyright (c) 2021. Elex. All Rights Reserved.
* https://www.elex-project.com/
*/
plugins {
id("elex-spring-boot")
id("org.springframework.boot") version "2.5.3"
id("io.spring.dependency-management") version "1.0.11.RELEASE"
}
dependencies {
implementation("org.springframework.boot:spring-boot-starter-web")
implementation("org.springframework.boot:spring-boot-starter-mustache")
compileOnly("org.projectlombok:lombok")
developmentOnly("org.springframework.boot:spring-boot-devtools")
annotationProcessor("org.springframework.boot:spring-boot-configuration-processor")
annotationProcessor("org.projectlombok:lombok")
testImplementation("org.springframework.boot:spring-boot-starter-test")
}

View File

@@ -0,0 +1,20 @@
/*
* Spring-boot Examples
*
* Copyright (c) 2021. Elex. All Rights Reserved.
* https://www.elex-project.com/
*/
package kr.pe.elex.examples;
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);
}
}

View File

@@ -0,0 +1,64 @@
package kr.pe.elex.examples;
import lombok.extern.slf4j.Slf4j;
import org.jetbrains.annotations.NotNull;
import org.springframework.web.servlet.LocaleResolver;
import org.springframework.web.servlet.i18n.AcceptHeaderLocaleResolver;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.util.List;
import java.util.Locale;
/**
* 로케일 리졸버
* AcceptHeaderLocaleResolver를 기본 정책으로하되, GET 파라미터로 로캐일 변환이 가능하도록 한다.
* @author Elex
*/
@Slf4j
public class CustomLocaleResolver implements LocaleResolver {
/**
* 지원되는 로캐일 목록. 첫 번째 로캐일은 기본 로캐일로 사용된다.
*/
private static final List<Locale> supportedLocales = List
.of(Locale.ENGLISH, Locale.KOREAN);
private Locale locale = null;
private final AcceptHeaderLocaleResolver acceptHeaderLocaleResolver;
public CustomLocaleResolver() {
super();
acceptHeaderLocaleResolver = new AcceptHeaderLocaleResolver();
acceptHeaderLocaleResolver.setSupportedLocales(supportedLocales);
acceptHeaderLocaleResolver.setDefaultLocale(supportedLocales.get(0));
}
@Override
public @NotNull Locale resolveLocale(@NotNull HttpServletRequest request) {
if (null == locale) {
return acceptHeaderLocaleResolver.resolveLocale(request);
} else {
for (Locale loc : supportedLocales) {
if (locale.equals(loc)) return loc;
}
for (Locale loc : supportedLocales) {
if (locale.getLanguage().equals(loc.getLanguage())) return loc;
}
return supportedLocales.get(0);
}
}
/**
* 안터셉터에서 처리될껄?
* @param request
* @param response
* @param locale
*/
@Override
public void setLocale(@NotNull HttpServletRequest request, HttpServletResponse response,
Locale locale) {
this.locale = locale;
}
}

View File

@@ -0,0 +1,30 @@
package kr.pe.elex.examples;
import com.samskivert.mustache.Mustache;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.MessageSource;
import org.springframework.web.bind.annotation.ControllerAdvice;
import org.springframework.web.bind.annotation.ModelAttribute;
import java.util.Locale;
/**
* Mustache 템플릿에 국제화 문자열을 끼워넣기 위해 사용된다.
*/
@Slf4j
@ControllerAdvice
public class I18nAdvice {
@Autowired
private MessageSource messageSource;
@ModelAttribute("i18n")
public Mustache.Lambda i18n(Locale locale){
return (frag, out) -> {
String body = frag.execute();
String message = this.messageSource.getMessage(body, new String[]{}, locale);
log.debug("User Locale: {} -> {}", locale, message);
out.write(message);
};
}
}

View File

@@ -0,0 +1,35 @@
/*
* Spring-boot Examples
*
* Copyright (c) 2021. Elex. All Rights Reserved.
* https://www.elex-project.com/
*/
package kr.pe.elex.examples;
import lombok.extern.slf4j.Slf4j;
import org.jetbrains.annotations.NotNull;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.core.io.Resource;
import org.springframework.http.ContentDisposition;
import org.springframework.http.HttpHeaders;
import org.springframework.http.ResponseEntity;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;
import org.springframework.web.servlet.mvc.support.RedirectAttributes;
import java.util.HashMap;
@Slf4j
@Controller
public class MyController {
@GetMapping(path = {"/"})
public String index() throws Exception {
return "main";
}
}

View File

@@ -0,0 +1,54 @@
package kr.pe.elex.examples;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.context.MessageSource;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.context.support.ReloadableResourceBundleMessageSource;
import org.springframework.web.servlet.LocaleResolver;
import org.springframework.web.servlet.config.annotation.InterceptorRegistry;
import org.springframework.web.servlet.config.annotation.WebMvcConfigurer;
import org.springframework.web.servlet.i18n.LocaleChangeInterceptor;
@Configuration
public class WebConfig implements WebMvcConfigurer {
@Value("${spring.messages.basename:i18n}")
String messagesBasename = null;
@Override
public void addInterceptors(InterceptorRegistry registry) {
registry.addInterceptor(localeChangeInterceptor());
}
/**
* Locale resolver, 디폴트 로캐일을 지정한다.
*/
@Bean
public LocaleResolver localeResolver() {
/*
SessionLocaleResolver sessionLocaleResolver = new SessionLocaleResolver();
sessionLocaleResolver.setDefaultLocale(Locale.US);
return sessionLocaleResolver;
*/
return new CustomLocaleResolver();
}
/**
* Locale change interceptor, 요청 파라미터에 따른 로케일 변경
*/
@Bean
public LocaleChangeInterceptor localeChangeInterceptor() {
LocaleChangeInterceptor localeChangeInterceptor = new LocaleChangeInterceptor();
localeChangeInterceptor.setParamName("language");
return localeChangeInterceptor;
}
@Bean
public MessageSource messageSource() {
ReloadableResourceBundleMessageSource messageSource
= new ReloadableResourceBundleMessageSource();
messageSource.setBasename("classpath:/" + messagesBasename);
messageSource.setDefaultEncoding("UTF-8");
return messageSource;
}
}

View File

@@ -0,0 +1,8 @@
/*
* Spring-boot Examples
*
* Copyright (c) 2021. Elex. All Rights Reserved.
* https://www.elex-project.com/
*/
package kr.pe.elex.examples;

View File

@@ -0,0 +1,9 @@
spring:
application:
name: My spring-boot project
mustache:
expose-request-attributes: true
messages:
basename: i18n
server:
port: 8080

View File

@@ -0,0 +1,10 @@
('-. ('-. ) (`-.
_( OO) _( OO) ( OO ).
(,------.,--. (,------.(_/. \_)-.
| .---'| |.-') | .---' \ `.' /
| | | | OO ) | | \ /\
(| '--. | |`-' |(| '--. \ \ |
| .--'(| '---.' | .--' .' \_)
| `---.| | | `---. / .'. \
`------'`------' `------''--' '--'
powered by ELEX

View File

@@ -0,0 +1 @@
hello.text = Hello

View File

@@ -0,0 +1 @@
hello.text = 안녕

View File

@@ -0,0 +1,48 @@
<?xml version="1.0" encoding="UTF-8"?>
<!--
~ Spring-boot Examples
~
~ Copyright (c) 2021. Elex. All Rights Reserved.
~ https://www.elex-project.com/
-->
<configuration>
<include resource="org/springframework/boot/logging/logback/defaults.xml"/>
<include resource="org/springframework/boot/logging/logback/console-appender.xml"/>
<springProperty name="LOG_DIR" source="logging.path"
defaultValue="${user.home}/logs"/>
<property name="LOG_PATH" value="${LOG_DIR}/stephanie.log"/>
<appender name="CONSOLE" class="ch.qos.logback.core.ConsoleAppender">
<encoder>
<pattern>${CONSOLE_LOG_PATTERN}</pattern>
</encoder>
</appender>
<appender name="ROLLING-FILE" class="ch.qos.logback.core.rolling.RollingFileAppender">
<encoder class="ch.qos.logback.classic.encoder.PatternLayoutEncoder">
<charset>UTF-8</charset>
<pattern>${FILE_LOG_PATTERN}</pattern>
</encoder>
<file>${LOG_PATH}</file>
<rollingPolicy class="ch.qos.logback.core.rolling.TimeBasedRollingPolicy">
<fileNamePattern>${LOG_DIR}/sebastian_%d{yyyy-MM-dd}_%i.log.gz</fileNamePattern>
<timeBasedFileNamingAndTriggeringPolicy class="ch.qos.logback.core.rolling.SizeAndTimeBasedFNATP">
<maxFileSize>10MB</maxFileSize>
</timeBasedFileNamingAndTriggeringPolicy>
<maxHistory>60</maxHistory>
</rollingPolicy>
</appender>
<logger name="kr.pe.elex" level="debug" additivity="false">
<appender-ref ref="CONSOLE"/>
<appender-ref ref="ROLLING-FILE"/>
</logger>
<root level="info">
<appender-ref ref="CONSOLE"/>
<appender-ref ref="ROLLING-FILE"/>
</root>
</configuration>

View File

@@ -0,0 +1,2 @@
<h1>i18n</h1>
<p>{{#i18n}}hello.text{{/i18n}}</p>

27
mvc/build.gradle.kts Normal file
View File

@@ -0,0 +1,27 @@
/*
* Spring-boot Examples
*
* Copyright (c) 2021. Elex. All Rights Reserved.
* https://www.elex-project.com/
*/
plugins {
id("elex-spring-boot")
id("org.springframework.boot") version "2.5.3"
id("io.spring.dependency-management") version "1.0.11.RELEASE"
}
dependencies {
implementation("org.springframework.boot:spring-boot-starter-web")
implementation("org.springframework.boot:spring-boot-starter-mustache")
compileOnly("org.projectlombok:lombok")
developmentOnly("org.springframework.boot:spring-boot-devtools")
annotationProcessor("org.springframework.boot:spring-boot-configuration-processor")
annotationProcessor("org.projectlombok:lombok")
testImplementation("org.springframework.boot:spring-boot-starter-test")
}

View File

@@ -0,0 +1,20 @@
/*
* Spring-boot Examples
*
* Copyright (c) 2021. Elex. All Rights Reserved.
* https://www.elex-project.com/
*/
package kr.pe.elex.examples;
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);
}
}

View File

@@ -0,0 +1,47 @@
/*
* Spring-boot Examples
*
* Copyright (c) 2021. Elex. All Rights Reserved.
* https://www.elex-project.com/
*/
package kr.pe.elex.examples;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.util.MultiValueMap;
import org.springframework.web.bind.annotation.*;
@Slf4j
@Controller
@RequestMapping({"/my"})
public class MyController {
@Autowired
private MyService service;
@GetMapping({"/"})
public String index() {
return "main";
}
@GetMapping({"/path/{id}"})
public String withPathVar(@PathVariable String id) {
return "main";
}
@GetMapping({"/request"})
public String withReqVar(@RequestParam String id) {
return "main";
}
@PostMapping({"/post"})
public String withPostBody(@RequestBody Person person) {
return "main";
}
@GetMapping({"/request/header"})
public String withReqHeader(@RequestHeader MultiValueMap<String, String> headers) {
return "main";
}
}

View File

@@ -0,0 +1,14 @@
package kr.pe.elex.examples;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Service;
@Slf4j
@Service
public class MyService {
public String doSomething(String param) throws IllegalArgumentException, Exception {
if ("illegal" == param) throw new IllegalArgumentException("Oops~");
if ("exception" == param) throw new Exception("Oops~");
return "Hello";
}
}

View File

@@ -0,0 +1,12 @@
package kr.pe.elex.examples;
import com.fasterxml.jackson.annotation.JsonProperty;
import lombok.Data;
@Data
public class Person {
@JsonProperty
private String name;
@JsonProperty
private int age;
}

View File

@@ -0,0 +1,8 @@
/*
* Spring-boot Examples
*
* Copyright (c) 2021. Elex. All Rights Reserved.
* https://www.elex-project.com/
*/
package kr.pe.elex.examples;

View File

@@ -0,0 +1,9 @@
spring:
application:
name: My spring-boot project
servlet:
multipart:
max-file-size: 128KB
max-request-size: 128KB
server:
port: 8080

View File

@@ -0,0 +1,10 @@
('-. ('-. ) (`-.
_( OO) _( OO) ( OO ).
(,------.,--. (,------.(_/. \_)-.
| .---'| |.-') | .---' \ `.' /
| | | | OO ) | | \ /\
(| '--. | |`-' |(| '--. \ \ |
| .--'(| '---.' | .--' .' \_)
| `---.| | | `---. / .'. \
`------'`------' `------''--' '--'
powered by ELEX

View File

@@ -0,0 +1,48 @@
<?xml version="1.0" encoding="UTF-8"?>
<!--
~ Spring-boot Examples
~
~ Copyright (c) 2021. Elex. All Rights Reserved.
~ https://www.elex-project.com/
-->
<configuration>
<include resource="org/springframework/boot/logging/logback/defaults.xml"/>
<include resource="org/springframework/boot/logging/logback/console-appender.xml"/>
<springProperty name="LOG_DIR" source="logging.path"
defaultValue="${user.home}/logs"/>
<property name="LOG_PATH" value="${LOG_DIR}/stephanie.log"/>
<appender name="CONSOLE" class="ch.qos.logback.core.ConsoleAppender">
<encoder>
<pattern>${CONSOLE_LOG_PATTERN}</pattern>
</encoder>
</appender>
<appender name="ROLLING-FILE" class="ch.qos.logback.core.rolling.RollingFileAppender">
<encoder class="ch.qos.logback.classic.encoder.PatternLayoutEncoder">
<charset>UTF-8</charset>
<pattern>${FILE_LOG_PATTERN}</pattern>
</encoder>
<file>${LOG_PATH}</file>
<rollingPolicy class="ch.qos.logback.core.rolling.TimeBasedRollingPolicy">
<fileNamePattern>${LOG_DIR}/sebastian_%d{yyyy-MM-dd}_%i.log.gz</fileNamePattern>
<timeBasedFileNamingAndTriggeringPolicy class="ch.qos.logback.core.rolling.SizeAndTimeBasedFNATP">
<maxFileSize>10MB</maxFileSize>
</timeBasedFileNamingAndTriggeringPolicy>
<maxHistory>60</maxHistory>
</rollingPolicy>
</appender>
<logger name="kr.pe.elex" level="debug" additivity="false">
<appender-ref ref="CONSOLE"/>
<appender-ref ref="ROLLING-FILE"/>
</logger>
<root level="info">
<appender-ref ref="CONSOLE"/>
<appender-ref ref="ROLLING-FILE"/>
</root>
</configuration>

View File

@@ -0,0 +1,15 @@
<h1>File Upload Example!!!</h1>
<form action="/upload" method="POST" enctype="multipart/form-data">
<label for="file">Select a file: </label>
<input id="file" name="file" type="file" title="Upload"/>
<div>
<button>Upload</button>
</div>
</form>
<h2>Uploaded Files</h2>
<p>Click to download.</p>
<ul>
{{#files}}
<li><a href="/files/{{.}}">{{.}}</a></li>
{{/files}}
</ul>

View File

@@ -20,7 +20,6 @@ import org.springframework.web.servlet.config.annotation.WebMvcConfigurer;
@Slf4j @Slf4j
@Configuration @Configuration
@EnableWebMvc
public class WebConfig implements WebMvcConfigurer { public class WebConfig implements WebMvcConfigurer {
//@Autowired //@Autowired
//private JwtInterceptor jwtInterceptor; //private JwtInterceptor jwtInterceptor;

View File

@@ -9,5 +9,5 @@ rootProject.name = "spring-boot-examples"
include( include(
"file-upload", "security", "security-with-jpa", "validation", "testing", "file-upload", "security", "security-with-jpa", "validation", "testing",
"mqtt", "websocket", "restful", "swing", "rest-doc", "mqtt", "websocket", "restful", "swing", "rest-doc",
"cache", "security-with-jwt", "exception" "cache", "security-with-jwt", "exception", "i18n", "mvc"
) )