javax.persistence.UriToStringConverter Maven / Gradle / Ivy
package javax.persistence;
import java.net.URI;
import org.springframework.util.StringUtils;
import org.springframework.web.util.UriComponentsBuilder;
@Converter
public class UriToStringConverter implements AttributeConverter {
@Override
public String convertToDatabaseColumn(URI attribute) {
if (attribute != null) {
String dbData = UriComponentsBuilder.fromUri(attribute).build().toUriString();
return StringUtils.hasText(dbData) ? dbData : null;
}
return null;
}
@Override
public URI convertToEntityAttribute(String dbData) {
return StringUtils.hasText(dbData) ? UriComponentsBuilder.fromUriString(dbData).build().toUri() : null;
}
}