org.babyfish.jimmer.spring.cloud.SpringCloudExchange Maven / Gradle / Ivy
Go to download
Show more of this group Show more artifacts with this name
Show all versions of jimmer-spring-boot-starter Show documentation
Show all versions of jimmer-spring-boot-starter Show documentation
A revolutionary ORM framework for both java and kotlin
package org.babyfish.jimmer.spring.cloud;
import com.fasterxml.jackson.core.JsonProcessingException;
import com.fasterxml.jackson.databind.ObjectMapper;
import com.fasterxml.jackson.databind.type.TypeFactory;
import org.babyfish.jimmer.impl.util.Classes;
import org.babyfish.jimmer.meta.ImmutableProp;
import org.babyfish.jimmer.runtime.ImmutableSpi;
import org.babyfish.jimmer.sql.ast.tuple.Tuple2;
import org.babyfish.jimmer.sql.fetcher.Fetcher;
import org.babyfish.jimmer.sql.runtime.MicroServiceExchange;
import org.springframework.web.client.RestTemplate;
import java.util.Collection;
import java.util.List;
public class SpringCloudExchange implements MicroServiceExchange {
private final RestTemplate restTemplate;
private final ObjectMapper mapper;
public SpringCloudExchange(RestTemplate restTemplate, ObjectMapper mapper) {
this.restTemplate = restTemplate;
this.mapper = mapper;
}
@Override
public List findByIds(
String microServiceName,
Collection> ids,
Fetcher> fetcher
) throws JsonProcessingException {
String json = restTemplate.getForObject(
"http://" +
microServiceName +
MicroServiceExporterController.BY_IDS +
"?" +
MicroServiceExporterController.IDS +
"={ids}&" +
MicroServiceExporterController.FETCHER +
"={fetcher}",
String.class,
mapper.writeValueAsString(ids),
fetcher.toString()
);
return mapper.readValue(
json,
mapper.getTypeFactory().constructParametricType(
List.class,
fetcher.getImmutableType().getJavaClass()
)
);
}
@Override
public List> findByAssociatedIds(
String microServiceName,
ImmutableProp prop,
Collection> targetIds,
Fetcher> fetcher
) throws JsonProcessingException {
String json = restTemplate.getForObject(
"http://" +
microServiceName +
MicroServiceExporterController.BY_ASSOCIATED_IDS +
"?" +
MicroServiceExporterController.PROP +
"={prop}&" +
MicroServiceExporterController.TARGET_IDS +
"={targetIds}&" +
MicroServiceExporterController.FETCHER +
"={fetcher}",
String.class,
prop.getName(),
mapper.writeValueAsString(targetIds),
fetcher.toString()
);
TypeFactory typeFactory = mapper.getTypeFactory();
return mapper.readValue(
json,
typeFactory.constructParametricType(
List.class,
typeFactory.constructParametricType(
Tuple2.class,
Classes.boxTypeOf(prop.getTargetType().getIdProp().getElementClass()),
fetcher.getImmutableType().getJavaClass()
)
)
);
}
}