# 3.4.3 GenericConverter 사용하기

**정교한 `Converter`구현이 필요한 경우, `GenericConverter`인터페이스 사용을 고려하십시오.** `Converter`보다 유연하면서도 타입 제약이 적은 `GenericConverter`는 **여러 개의 원본 타입과 대상 타입 간의 변환을 지원합니다.** 또한, **`GenericConverter`는 자신만의 변환 로직을 구현할 때 사용할 수 있는 원본 및 대상 필드 컨텍스트를 제공합니다.** 이러한 컨텍스트로 필드 어노테이션 또는 필드 정의에 선언된 일반 정보로 타입 변환을 할 수 있습니다. 다음 목록은 `GenericConverter`의 인터페이스 정의를 보여줍니다.&#x20;

```java
package org.springframework.core.convert.converter;

public interface GenericConverter {

    public Set<ConvertiblePair> getConvertibleTypes();

    Object convert(Object source, TypeDescriptor sourceType, TypeDescriptor targetType);
}
```

**`GenericConverter`를 구현하려면, source -> target 타입 쌍을 반환하는 `getConvertibleTypes()`를 정의하고 변환 로직을 포함하도록 `convert(Object, TypeDescriptor, TypeDescriptor)`를 구현하십시오. Source `TypeDescriptor`는 변환될 값이 있는 source 필드에 대한 액세스를 제공합니다. Target `TypeDescriptor`는 변환된 값이 할당될 target 필드에 대한 액세스를 제공합니다.**

{% hint style="info" %}
두 가지 이상의 타입 변환이 필요할 때 사용한다. 여러 개의 소스 및 대상 타입을 지정할 수 있고 소스나 대상 객체의 필드 컨텍스트(필드에 적용된 어노테이션이나 제네릭 등 필드와 관련된 모든 정보)를 사용할 수 있으므로 유연한 컨버터이지만, 구현하기 어렵고 복잡하므로 사용을 권장하지 않는다.&#x20;
{% endhint %}

`GenericConverter`의 좋은 예는 자바 배열과 컬렉션 간에 변환하는 Converter 입니다. 이러한 `ArrayToCollectionConverter`는 컬렉션의 요소 타입을 확인하기 위해 대상 컬렉션 타입을 선언하는 필드를 조사합니다. 이렇게 하면 컬렉션이 대상 필드에 설정되기 전에 원본 배열의 각 요소를 컬렉션 요소 타입으로 변환할 수 있습니다.&#x20;

| `GenericConverter`는 보다 복잡한 SPI 인터페이스이기 때문에 필요할 때만 사용해야합니다. 기본적인 타입 변환이 필요하다면 `Converter`또는 `ConverterFactory`를 사용하십시오. |
| ---------------------------------------------------------------------------------------------------------------------- |

#### **ConditionalGenericConverter 사용**

**경우에 따라, 특정 조건이 true인 경우에만 `Converter`를 실행하려고 할 수 있습니다.** 예를 들어, 특정한 어노테이션이 대상 필드에 있는 경우에만 `Converter`를 실행하거나 특정 메소드(`static valueOf`메소드 등)가 대상 클래스에 정의되어 있는 경우에만 `Converter`를 실행하고자 할 수 있습니다. **`ConditionalGenericConverter`는 사용자 정의 일치 조건을 정의할 수 있는 `GenericConverter`및 `ConditionalConverter`인터페이스의 통합입니다.**&#x20;

```java
public interface ConditionalConverter {

    boolean matches(TypeDescriptor sourceType, TypeDescriptor targetType);
}

public interface ConditionalGenericConverter extends GenericConverter, ConditionalConverter {
}
```

`ConditionalGenericConverter`의 좋은 예는 영속하는 엔티티 식별자와 엔티티 참조 간의 변환을 하는 `EntityConverter`입니다. 이러한 `EntityConverter`는 대상 엔터티 타입이 정적 finder 메소드(예: `findAccount(Long)`)를 정의하는 경우에만 수행됩니다. `matches(TypeDescriptor, TypeDescriptor)`의 구현에서 이와 같은 finder 메소드 검사를 수행할 수 있습니다.&#x20;


---

# Agent Instructions: Querying This Documentation

If you need additional information that is not directly available in this page, you can query the documentation dynamically by asking a question.

Perform an HTTP GET request on the current page URL with the `ask` query parameter:

```
GET https://wannaqueen.gitbook.io/spring5/spring-5.0/3.-by-ys-sh/3.4-by-sh/3.4.3-by-sh.md?ask=<question>
```

The question should be specific, self-contained, and written in natural language.
The response will contain a direct answer to the question and relevant excerpts and sources from the documentation.

Use this mechanism when the answer is not explicitly present in the current page, you need clarification or additional context, or you want to retrieve related documentation sections.
