Compare commits
1 Commits
feature/me
...
feature/co
| Author | SHA1 | Date | |
|---|---|---|---|
|
|
f256e7dbea |
@@ -25,7 +25,6 @@ dependencies {
|
||||
implementation 'org.springframework.boot:spring-boot-starter-web'
|
||||
implementation 'io.springfox:springfox-swagger2:2.6.1'
|
||||
implementation 'io.springfox:springfox-swagger-ui:2.6.1'
|
||||
implementation 'net.rakugakibox.util:yaml-resource-bundle:1.1'
|
||||
compileOnly 'org.projectlombok:lombok'
|
||||
runtimeOnly 'com.h2database:h2'
|
||||
runtimeOnly 'mysql:mysql-connector-java'
|
||||
|
||||
@@ -4,8 +4,6 @@ import com.rest.api.advice.exception.CUserNotFoundException;
|
||||
import com.rest.api.model.response.CommonResult;
|
||||
import com.rest.api.service.ResponseService;
|
||||
import lombok.RequiredArgsConstructor;
|
||||
import org.springframework.context.MessageSource;
|
||||
import org.springframework.context.i18n.LocaleContextHolder;
|
||||
import org.springframework.http.HttpStatus;
|
||||
import org.springframework.web.bind.annotation.ExceptionHandler;
|
||||
import org.springframework.web.bind.annotation.ResponseStatus;
|
||||
@@ -19,28 +17,15 @@ public class ExceptionAdvice {
|
||||
|
||||
private final ResponseService responseService;
|
||||
|
||||
private final MessageSource messageSource;
|
||||
|
||||
@ExceptionHandler(Exception.class)
|
||||
@ResponseStatus(HttpStatus.INTERNAL_SERVER_ERROR)
|
||||
protected CommonResult defaultException(HttpServletRequest request, Exception e) {
|
||||
// 예외 처리의 메시지를 MessageSource에서 가져오도록 수정
|
||||
return responseService.getFailResult(Integer.valueOf(getMessage("unKnown.code")), getMessage("unKnown.msg"));
|
||||
}
|
||||
// @ExceptionHandler(Exception.class)
|
||||
// @ResponseStatus(HttpStatus.INTERNAL_SERVER_ERROR)
|
||||
// protected CommonResult defaultException(HttpServletRequest request, Exception e) {
|
||||
// return responseService.getFailResult();
|
||||
// }
|
||||
|
||||
@ExceptionHandler(CUserNotFoundException.class)
|
||||
@ResponseStatus(HttpStatus.INTERNAL_SERVER_ERROR)
|
||||
protected CommonResult userNotFoundException(HttpServletRequest request, CUserNotFoundException e) {
|
||||
// 예외 처리의 메시지를 MessageSource에서 가져오도록 수정
|
||||
return responseService.getFailResult(Integer.valueOf(getMessage("userNotFound.code")), getMessage("userNotFound.msg"));
|
||||
}
|
||||
|
||||
// code정보에 해당하는 메시지를 조회합니다.
|
||||
private String getMessage(String code) {
|
||||
return getMessage(code, null);
|
||||
}
|
||||
// code정보, 추가 argument로 현재 locale에 맞는 메시지를 조회합니다.
|
||||
private String getMessage(String code, Object[] args) {
|
||||
return messageSource.getMessage(code, args, LocaleContextHolder.getLocale());
|
||||
return responseService.getFailResult();
|
||||
}
|
||||
}
|
||||
|
||||
@@ -1,63 +0,0 @@
|
||||
package com.rest.api.config;
|
||||
|
||||
import lombok.extern.slf4j.Slf4j;
|
||||
import net.rakugakibox.util.YamlResourceBundle;
|
||||
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.ResourceBundleMessageSource;
|
||||
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;
|
||||
import org.springframework.web.servlet.i18n.SessionLocaleResolver;
|
||||
|
||||
import java.util.Locale;
|
||||
import java.util.MissingResourceException;
|
||||
import java.util.ResourceBundle;
|
||||
|
||||
@Configuration
|
||||
public class MessageConfiguration implements WebMvcConfigurer {
|
||||
|
||||
@Bean // 세션에 지역설정. default는 KOREAN = 'ko'
|
||||
public LocaleResolver localeResolver() {
|
||||
SessionLocaleResolver slr = new SessionLocaleResolver();
|
||||
slr.setDefaultLocale(Locale.KOREAN);
|
||||
return slr;
|
||||
}
|
||||
|
||||
@Bean // 지역설정을 변경하는 인터셉터. 요청시 파라미터에 lang 정보를 지정하면 언어가 변경됨.
|
||||
public LocaleChangeInterceptor localeChangeInterceptor() {
|
||||
LocaleChangeInterceptor lci = new LocaleChangeInterceptor();
|
||||
lci.setParamName("lang");
|
||||
return lci;
|
||||
}
|
||||
|
||||
@Override // 인터셉터를 시스템 레지스트리에 등록
|
||||
public void addInterceptors(InterceptorRegistry registry) {
|
||||
registry.addInterceptor(localeChangeInterceptor());
|
||||
}
|
||||
|
||||
@Bean // yml 파일을 참조하는 MessageSource 선언
|
||||
public MessageSource messageSource(
|
||||
@Value("${spring.messages.basename}") String basename,
|
||||
@Value("${spring.messages.encoding}") String encoding
|
||||
) {
|
||||
YamlMessageSource ms = new YamlMessageSource();
|
||||
ms.setBasename(basename);
|
||||
ms.setDefaultEncoding(encoding);
|
||||
ms.setAlwaysUseMessageFormat(true);
|
||||
ms.setUseCodeAsDefaultMessage(true);
|
||||
ms.setFallbackToSystemLocale(true);
|
||||
return ms;
|
||||
}
|
||||
|
||||
// locale 정보에 따라 다른 yml 파일을 읽도록 처리
|
||||
private static class YamlMessageSource extends ResourceBundleMessageSource {
|
||||
@Override
|
||||
protected ResourceBundle doGetBundle(String basename, Locale locale) throws MissingResourceException {
|
||||
return ResourceBundle.getBundle(basename, locale, YamlResourceBundle.Control.INSTANCE);
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -31,8 +31,7 @@ public class UserController {
|
||||
|
||||
@ApiOperation(value = "회원 단건 조회", notes = "userId로 회원을 조회한다")
|
||||
@GetMapping(value = "/user/{msrl}")
|
||||
public SingleResult<User> findUserById(@ApiParam(value = "회원ID", required = true) @PathVariable long msrl,
|
||||
@ApiParam(value = "언어", defaultValue = "ko") @RequestParam String lang) {
|
||||
public SingleResult<User> findUserById(@ApiParam(value = "회원ID", required = true) @PathVariable long msrl) {
|
||||
// 결과데이터가 단일건인경우 getBasicResult를 이용해서 결과를 출력한다.
|
||||
return responseService.getSingleResult(userJpaRepo.findById(msrl).orElseThrow(CUserNotFoundException::new));
|
||||
}
|
||||
|
||||
@@ -7,12 +7,13 @@ import org.springframework.stereotype.Service;
|
||||
|
||||
import java.util.List;
|
||||
|
||||
@Service
|
||||
@Service // 해당 Class가 Service임을 명시합니다.
|
||||
public class ResponseService {
|
||||
|
||||
// enum으로 api 요청 결과에 대한 code, message를 정의합니다.
|
||||
public enum CommonResponse {
|
||||
SUCCESS(0, "성공하였습니디.");
|
||||
SUCCESS(0, "성공하였습니디."),
|
||||
FAIL(-1, "실패하였습니다.");
|
||||
|
||||
int code;
|
||||
String msg;
|
||||
@@ -51,11 +52,11 @@ public class ResponseService {
|
||||
return result;
|
||||
}
|
||||
// 실패 결과만 처리하는 메소드
|
||||
public CommonResult getFailResult(int code, String msg) {
|
||||
public CommonResult getFailResult() {
|
||||
CommonResult result = new CommonResult();
|
||||
result.setSuccess(false);
|
||||
result.setCode(code);
|
||||
result.setMsg(msg);
|
||||
result.setCode(CommonResponse.FAIL.getCode());
|
||||
result.setMsg(CommonResponse.FAIL.getMsg());
|
||||
return result;
|
||||
}
|
||||
// 결과 모델에 api 요청 성공 데이터를 세팅해주는 메소드
|
||||
|
||||
@@ -8,7 +8,7 @@ spring:
|
||||
username: sa
|
||||
jpa:
|
||||
database-platform: org.hibernate.dialect.H2Dialect
|
||||
properties.hibernate.hbm2ddl.auto: update
|
||||
#properties.hibernate.hbm2ddl.auto: none
|
||||
showSql: true
|
||||
messages:
|
||||
basename: i18n/exception
|
||||
|
||||
@@ -1,6 +0,0 @@
|
||||
unKnown:
|
||||
code: "-9999"
|
||||
msg: "An unknown error has occurred."
|
||||
userNotFound:
|
||||
code: "-1000"
|
||||
msg: "This member not exist"
|
||||
@@ -1,6 +0,0 @@
|
||||
unKnown:
|
||||
code: "-9999"
|
||||
msg: "알수 없는 오류가 발생하였습니다."
|
||||
userNotFound:
|
||||
code: "-1000"
|
||||
msg: "존재하지 않는 회원입니다."
|
||||
Reference in New Issue
Block a user