'2021/11'에 해당되는 글 12건

  1. 2021.11.30 [Spring] bean name conflict resolve.
  2. 2021.11.30 [Spring] @Autowired vs @RequiredArgsConstructor
  3. 2021.11.29 [MySQL] group_concat 사용 시 순서 지정.
  4. 2021.11.25 [Java] Snake to Camel 변환
  5. 2021.11.25 [Elasticsearch] LowLevelRestClient 를 이용한 개발 시 Json 결과 처리
  6. 2021.11.23 [cURL] cURL in Bash Script
  7. 2021.11.23 [Shell] Bash Random Number & Substring
  8. 2021.11.18 [Logstash] filter split 예제
  9. 2021.11.18 [Shell] for loop example
  10. 2021.11.16 [Jackson] AddSlash 출력 결과 포함

[Spring] bean name conflict resolve.

ITWeb/개발일반 2021. 11. 30. 12:17

간단하게는 명시적으로 선언을 해주시면 됩니다.

 

서로 다른 패키지에 같은 이름의 class 가 존재 할 경우 발생 할 수 있습니다.

이런 경우 아래와 같이 선언해 주시면 됩니다.

 

@RestController("cat.RestHealthController")

@Service("cat.service.HealthService")

 

@RestController("cluster.RestHealthController")

@Service("cluster.service.HealthService")

 

:

[Spring] @Autowired vs @RequiredArgsConstructor

ITWeb/개발일반 2021. 11. 30. 10:36

Spring DI 선언에 사용 합니다.

 

[Spring Autowired Annotation]

https://docs.spring.io/spring-framework/docs/current/javadoc-api/org/springframework/beans/factory/annotation/Autowired.html

 

[Lombok RequiredArgsConstructor Annotation]

https://projectlombok.org/api/lombok/RequiredArgsConstructor.html

 

:

[MySQL] group_concat 사용 시 순서 지정.

ITWeb/개발일반 2021. 11. 29. 18:28

참고문서)

https://dev.mysql.com/doc/refman/8.0/en/aggregate-functions.html#function_group-concat

 

group by 를 이용해서 특정 컬럼의 value 를 concat 할 경우 해당 테이블의 특정 컬럼으로 순서를 맞춰 줘야 할 경우가 있습니다.

group_concat 함수 내부에 order by 를 이용해서 적용 하시면 됩니다.

 

이렇게 하지 않았을 경우 순서에 대해 보장이 된다고 간혹 착각 할 수 있느데 주의 해서 사용하시기 바랍니다.

 

:

[Java] Snake to Camel 변환

ITWeb/개발일반 2021. 11. 25. 16:46

query parameter 로 넘어온 변수가 snake 로 작성 되어 있어서 이걸 camel 로 받도록 해주고 싶을 때 사용 하면 됩니다.

 

- jackson-databind 패키지에 포함 되어 있습니다.

- domain, model 클래스에 아래와 같이 선언 합니다.

// GET /_cluster/settings?flat_settings=true&master_timeout=30s


@Data
@JsonNaming(PropertyNamingStrategies.SnakeCaseStrategy.class)
public class SettingsModel {
	private String flatSettings;
    private String masterTimeout;
}

참고용으로 기록 합니다.

 

:

[Elasticsearch] LowLevelRestClient 를 이용한 개발 시 Json 결과 처리

Elastic/Elasticsearch 2021. 11. 25. 14:08

보통 Elasticsearch LLRC 를 이용해서 RESTful API 요청 하게 되면
...중략...
Request request = new Request(...중략...);
Response response = esClient.getRestClient().performRequest(request);
String body = EntityUtils.toString(response.getEntity());
...중략...

String 으로 결과를 받아서 리턴 하게 되는데 이 과정에서 JSON String 변환 시 slash 가 추가 되는 불편함이 있을 수 있습니다.
이걸 해소 하려면
...중략...
String body = EntityUtils.toString(response.getEntity());
JsonNode jsonNode = ObjectMapper.readTree(body);
...중략...
readTree 해서 JsonNode Object 로 변환 한 후 처리 하시면 {}, [] 등 모두 깔끔하게 처리가 가능 합니다.

 

ASIS)

{
	"response": "{ \"alias\": \"henry\" }"
}

{
	"response": "[
    	{ \"alias1\": \"henry\" },
        { \"alias2\": \"jeong\" }
      ]"
}

 

TOBE)

{
	"response": { "alias": "henry" }
}

{
	"response": [
    	{ "alias1": "henry" },
        { "alias2": "jeong" }
      ]
}

 

:

[cURL] cURL in Bash Script

ITWeb/개발일반 2021. 11. 23. 15:35

Case 1)
response=`curl -s -X GET 'http://www.kakaopay.com' -H 'Content-Type: application/json' -d '{ "size": 100, "message": "test" }'`

Case 2)
curl -s -X GET 'http://www.kakaopay.com' -H 'Content-Type: application/json' -d '{ "size": 100, "message": "test" }'

Case 3)
size=100
message="test"

response=`curl -s -X GET 'http://www.kakaopay.com' -H 'Content-Type: application/json' -d '{ "size": '$size', "message": "'"$message"'" }'`

Case 4)
size=100
message="test"

curl -s -X GET 'http://www.kakaopay.com' -H 'Content-Type: application/json' -d '{ "size": '$size', "message": "'"$message"'" }'

proxy 를 지정 하고자 한다면 아래 내용 추가 합니다.
curl -x 'http://PROXY-IP:PROXY-PORT' -s -X GET 'http://www.kakaopay.com'

 

:

[Shell] Bash Random Number & Substring

ITWeb/개발일반 2021. 11. 23. 15:33

이런것도 기억력을 돕기 위해 기록해 봅니다.

 

- Bash Random Number
$ echo $(( $RANDOM % 4 ))

 

nodes=( '1' '2' '3' '4' )

i=$(( $RANDOM % 4 ))

node=${nodes[${i}]}

- Bash Substring
${String:Position:length} 문자:인덱스:길이

 

:

[Logstash] filter split 예제

Elastic/Logstash 2021. 11. 18. 12:12

참고문서)

https://www.elastic.co/guide/en/logstash/current/plugins-filters-split.html

https://www.elastic.co/guide/en/logstash/current/plugins-filters-mutate.html#plugins-filters-mutate-split

 

예제코드)

라인단위로 정보가 작성 되어 있을 때

데이터 예시)
1|A|가
2|B|나

filter {
	split { }

	mutate {
		split => { "message" => "|" }
		add_field => {
			"first" => "%{[message][0]}"
			"second" => "%{[message][1]}"
			"third" => "%{[message][2]}"
		}
	}
}

 

:

[Shell] for loop example

ITWeb/개발일반 2021. 11. 18. 11:25

간혹 서버간 ssh tunneling 을 위해 pub key 를 등록 해야 할 일이 있습니다.

노가다 하기 귀찮으니까 instance 목록만 가지고 쭈욱 돌리면 되겠죠.

 

#!/bin/bash

PUB="abcdefg"
HOSTS=("ip1" "ip2" "ip3" ...)

for host in "${HOSTS[@]}"
do
  echo "ssh -o StrictHostKeychecking=no USER@$host \"echo '$PUB' >> .ssh/authorized_keys\""
  ssh -o StrictHostKeychecking=no USER@$host "echo '$PUB' >> .ssh/authorized_keys"
done

이런것도 맨날 기억을 못해서 기록을 합니다.

:

[Jackson] AddSlash 출력 결과 포함

ITWeb/개발일반 2021. 11. 16. 16:54

jackson 사용 시 writeValueAsString(Object) 에서 간혹 JSON String 에 Addslash 된 결과가 출력

reponse.getEntity() -> EntityUtils.toString 후 writeValueAsString(Object)

하게 되면 의도하지 않게 List value 에 addslash 가 되어서 출력이 되버립니다.
이를 방지 하기 위해서는 아래 단계를 추가해서 구분해 주면 됩니다.

response.getEntity() -> EntityUtils.toString -> readValue(String, new TypeReference<List<Object>>() {}) -> writeValueAsString(Object)

하게 되면 원하는 결과를 얻을 수 있습니다.

Case 1) reponse.getEntity() 가 Object 형일 경우
reponse.getEntity() -> EntityUtils.toString -> writeValueAsString(Object)

Case 2) reponse.getEntity() 가 List 형일 경우
response.getEntity() -> EntityUtils.toString -> readValue(String, new TypeReference<List<Object>>() {}) -> writeValueAsString(Object)

: