From b9634d8a394e53d4bf55d8585ae92c285af3856c Mon Sep 17 00:00:00 2001 From: Pyohwan Date: Thu, 22 Sep 2016 00:55:59 +0900 Subject: [PATCH] =?UTF-8?q?=EB=AA=BD=EA=B3=A0=EB=94=94=EB=B9=84=20write=20?= =?UTF-8?q?=EC=95=88=EB=90=98=EB=8A=94=20=ED=98=84=EC=83=81=20=EC=88=98?= =?UTF-8?q?=EC=A0=95.?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../RestExceptionHandler.java | 41 ++++++++++--------- .../core/configuration/MongoConfig.java | 26 +++++++++++- .../properties/core-local.properties | 3 +- .../properties/core-production.properties | 3 +- .../properties/core-staging.properties | 3 +- 5 files changed, 50 insertions(+), 26 deletions(-) diff --git a/jakduk-api/src/main/java/com/jakduk/api/restcontroller/exceptionHandler/RestExceptionHandler.java b/jakduk-api/src/main/java/com/jakduk/api/restcontroller/exceptionHandler/RestExceptionHandler.java index 2cf8547a..27cb459d 100644 --- a/jakduk-api/src/main/java/com/jakduk/api/restcontroller/exceptionHandler/RestExceptionHandler.java +++ b/jakduk-api/src/main/java/com/jakduk/api/restcontroller/exceptionHandler/RestExceptionHandler.java @@ -5,6 +5,7 @@ import com.jakduk.core.exception.SuccessButNoContentException; import com.jakduk.core.exception.UserFeelingException; import lombok.extern.slf4j.Slf4j; +import org.springframework.context.support.DefaultMessageSourceResolvable; import org.springframework.http.HttpHeaders; import org.springframework.http.HttpStatus; import org.springframework.http.ResponseEntity; @@ -13,6 +14,7 @@ import org.springframework.validation.FieldError; import org.springframework.validation.ObjectError; import org.springframework.web.bind.MethodArgumentNotValidException; +import org.springframework.web.bind.MissingServletRequestParameterException; import org.springframework.web.bind.annotation.ControllerAdvice; import org.springframework.web.bind.annotation.ExceptionHandler; import org.springframework.web.bind.annotation.ResponseBody; @@ -20,8 +22,8 @@ import org.springframework.web.context.request.WebRequest; import org.springframework.web.servlet.mvc.method.annotation.ResponseEntityExceptionHandler; -import java.util.ArrayList; import java.util.List; +import java.util.stream.Collectors; /** * @author pyohwan @@ -34,11 +36,6 @@ public class RestExceptionHandler extends ResponseEntityExceptionHandler { /** * Hibernate validator Exception - * @param ex - * @param headers - * @param status - * @param request - * @return */ @Override protected ResponseEntity handleMethodArgumentNotValid( @@ -49,15 +46,13 @@ protected ResponseEntity handleMethodArgumentNotValid( List fieldErrors = result.getFieldErrors(); List globalErrors = result.getGlobalErrors(); - List fields = new ArrayList<>(); + List fields = globalErrors.stream() + .map(DefaultMessageSourceResolvable::getDefaultMessage) + .collect(Collectors.toList()); - for (ObjectError objectError : globalErrors) { - fields.add(objectError.getDefaultMessage()); - } - - for (FieldError fieldError : fieldErrors) { - fields.add(fieldError.getDefaultMessage()); - } + fields.addAll(fieldErrors.stream() + .map(DefaultMessageSourceResolvable::getDefaultMessage) + .collect(Collectors.toList())); RestError restError = new RestError(ServiceError.FORM_VALIDATION_FAILED, fields); @@ -66,11 +61,6 @@ protected ResponseEntity handleMethodArgumentNotValid( /** * RequestBody에서 request 값들을 객체화 실패했을때 - * @param ex - * @param headers - * @param status - * @param request - * @return */ @Override protected ResponseEntity handleHttpMessageNotReadable( @@ -81,6 +71,19 @@ protected ResponseEntity handleHttpMessageNotReadable( return new ResponseEntity<>(restError, ServiceError.FORM_VALIDATION_FAILED.getHttpStatus()); } + /** + * 쿼리 스트링 검증 실패 + */ + @Override + protected ResponseEntity handleMissingServletRequestParameter( + MissingServletRequestParameterException ex, HttpHeaders headers, HttpStatus status, WebRequest request) { + + RestError restError = new RestError(ServiceError.FORM_VALIDATION_FAILED.getCode(), + String.format(ex.getMessage(), ex.getParameterType(), ex.getParameterName())); + + return new ResponseEntity<>(restError, ServiceError.FORM_VALIDATION_FAILED.getHttpStatus()); + } + // 에러는 아니지만 데이터가 없음. @ExceptionHandler(SuccessButNoContentException.class) @ResponseStatus(HttpStatus.NO_CONTENT) diff --git a/jakduk-core/src/main/java/com/jakduk/core/configuration/MongoConfig.java b/jakduk-core/src/main/java/com/jakduk/core/configuration/MongoConfig.java index 39790e77..f20562ba 100644 --- a/jakduk-core/src/main/java/com/jakduk/core/configuration/MongoConfig.java +++ b/jakduk-core/src/main/java/com/jakduk/core/configuration/MongoConfig.java @@ -2,6 +2,9 @@ import com.mongodb.Mongo; import com.mongodb.MongoClient; +import com.mongodb.ServerAddress; +import org.apache.commons.lang3.StringUtils; +import org.elasticsearch.common.transport.InetSocketTransportAddress; import org.jongo.Jongo; import org.springframework.context.annotation.Bean; import org.springframework.context.annotation.Configuration; @@ -11,6 +14,15 @@ import org.springframework.data.mongodb.repository.config.EnableMongoRepositories; import javax.annotation.Resource; +import java.net.InetAddress; +import java.net.MalformedURLException; +import java.net.URL; +import java.net.UnknownHostException; +import java.util.ArrayList; +import java.util.Arrays; +import java.util.List; +import java.util.stream.Collectors; +import java.util.stream.Stream; /** * @author pyohwan @@ -31,7 +43,19 @@ protected String getDatabaseName() { @Override public Mongo mongo() throws Exception { - return new MongoClient(environment.getProperty("mongo.host.name"), environment.getProperty("mongo.host.port", Integer.class)); + + List seeds = Stream.of(StringUtils.split(environment.getProperty("mongo.host.name"), ",")) + .map(hostName -> { + try { + URL url = new URL(hostName); + return new ServerAddress(url.getHost(), url.getPort()); + } catch (MalformedURLException e) { + return null; + } + }) + .collect(Collectors.toList()); + + return new MongoClient(seeds); } @Bean diff --git a/jakduk-core/src/main/resources/properties/core-local.properties b/jakduk-core/src/main/resources/properties/core-local.properties index 77cf32ab..32cd8db7 100644 --- a/jakduk-core/src/main/resources/properties/core-local.properties +++ b/jakduk-core/src/main/resources/properties/core-local.properties @@ -1,7 +1,6 @@ # database properties mongo.db.name=jakduk_test -mongo.host.name=172.30.1.40 -mongo.host.port=27017 +mongo.host.name=http://172.30.1.40:27017,http://172.30.1.43:27017 # elasticsearch elasticsearch.host.name=http://172.30.1.2:9200,http://172.30.1.2:9201 diff --git a/jakduk-core/src/main/resources/properties/core-production.properties b/jakduk-core/src/main/resources/properties/core-production.properties index 1044fc5f..4587264a 100644 --- a/jakduk-core/src/main/resources/properties/core-production.properties +++ b/jakduk-core/src/main/resources/properties/core-production.properties @@ -1,7 +1,6 @@ # database properties mongo.db.name=jakduk_sample -mongo.host.name=172.30.1.40 -mongo.host.port=27017 +mongo.host.name=http://172.30.1.40:27017,http://172.30.1.43:27017 #elasticsearch elasticsearch.host.name=http://172.30.1.2:9200,http://172.30.1.2:9201 diff --git a/jakduk-core/src/main/resources/properties/core-staging.properties b/jakduk-core/src/main/resources/properties/core-staging.properties index 42b14578..8f7669d1 100644 --- a/jakduk-core/src/main/resources/properties/core-staging.properties +++ b/jakduk-core/src/main/resources/properties/core-staging.properties @@ -1,7 +1,6 @@ # database properties mongo.db.name=jakduk_test -mongo.host.name=172.30.1.40 -mongo.host.port=27017 +mongo.host.name=http://172.30.1.40:27017,http://172.30.1.43:27017 # elasticsearch elasticsearch.host.name=http://172.30.1.2:9200,http://172.30.1.2:9201