Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

몽고디비 write 안되는 현상 수정. #115

Merged
merged 1 commit into from
Sep 21, 2016
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -13,15 +14,16 @@
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;
import org.springframework.web.bind.annotation.ResponseStatus;
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
Expand All @@ -34,11 +36,6 @@ public class RestExceptionHandler extends ResponseEntityExceptionHandler {

/**
* Hibernate validator Exception
* @param ex
* @param headers
* @param status
* @param request
* @return
*/
@Override
protected ResponseEntity<Object> handleMethodArgumentNotValid(
Expand All @@ -49,15 +46,13 @@ protected ResponseEntity<Object> handleMethodArgumentNotValid(
List<FieldError> fieldErrors = result.getFieldErrors();
List<ObjectError> globalErrors = result.getGlobalErrors();

List<String> fields = new ArrayList<>();
List<String> 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);

Expand All @@ -66,11 +61,6 @@ protected ResponseEntity<Object> handleMethodArgumentNotValid(

/**
* RequestBody에서 request 값들을 객체화 실패했을때
* @param ex
* @param headers
* @param status
* @param request
* @return
*/
@Override
protected ResponseEntity<Object> handleHttpMessageNotReadable(
Expand All @@ -81,6 +71,19 @@ protected ResponseEntity<Object> handleHttpMessageNotReadable(
return new ResponseEntity<>(restError, ServiceError.FORM_VALIDATION_FAILED.getHttpStatus());
}

/**
* 쿼리 스트링 검증 실패
*/
@Override
protected ResponseEntity<Object> 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)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -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
Expand All @@ -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<ServerAddress> 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
Expand Down
Original file line number Diff line number Diff line change
@@ -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
Expand Down
Original file line number Diff line number Diff line change
@@ -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
Expand Down
Original file line number Diff line number Diff line change
@@ -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
Expand Down