通过春季休息存储库暴露复合ID



我的域对象

  • id
  • 版本

我们想在表中显示此内容,因此我需要在我的休息请求中获取它。

建议,我已经实施了

@Configuration
public class RepoConf extends RepositoryRestMvcConfiguration {
    public RepoConf(ApplicationContext context, ObjectFactory<ConversionService> conversionService) {
        super(context, conversionService);
    }
    protected void configureRepositoryRestConfiguration(RepositoryRestConfiguration config) {
        config.exposeIdsFor(Lot.class);
    }
}

我已经检查了加载此conf的日志:

2017-12-08 07:58:59.966  INFO 10344 --- [  restartedMain] o.s.b.f.s.DefaultListableBeanFactory     : Overriding bean definition for bean 'httpRequestHandlerAdapter' with a different definition: replacing [Root bean: class [null]; scope=; abstract=false; lazyInit=false; autowireMode=3; dependencyCheck=0; autowireCandidate=true; primary=false; factoryBeanName=repoConf; factoryMethodName=httpRequestHandlerAdapter; initMethodName=null; destroyMethodName=(inferred); defined in class path resource [fr/urssaf/genv/back/repository/RepoConf.class]] with [Root bean: class [null]; scope=; abstract=false; lazyInit=false; autowireMode=3; dependencyCheck=0; autowireCandidate=true; primary=false; factoryBeanName=org.springframework.boot.autoconfigure.web.servlet.WebMvcAutoConfiguration$EnableWebMvcConfiguration; factoryMethodName=httpRequestHandlerAdapter; initMethodName=null; destroyMethodName=(inferred); defined in class path resource [org/springframework/boot/autoconfigure/web/servlet/WebMvcAutoConfiguration$EnableWebMvcConfiguration.class]]

我有一个转换器来处理URL中的复合ID:

@Component
class CustomBackendIdConverter implements BackendIdConverter {
    @Override
    public Serializable fromRequestId(String id, Class<?> entityType) {
        switch (entityType.getSimpleName()) {
        case "Lot":
            String[] parts = id.split("_");
            return new LotId(Integer.valueOf(parts[0]), parts[1]);
        default:
            return null;
        }
    }
    @Override
    public String toRequestId(Serializable source, Class<?> entityType) {
        switch (entityType.getSimpleName()) {
        case "Lot":
            LotId id = (LotId) source;
            return String.format("%s_%s", id.getIdLot(), id.getVersionLot());
        default:
            return null;
        }
    }
    @Override
    public boolean supports(Class<?> type) {
        return Lot.class.equals(type);
    }
}

,但是当我在Lot Rest资源上提出请求时没有显示我的ID,例如:http://localhost:9000/lots/1_0我该如何实现?

好的,在弹簧代码中挖掘,似乎该方法签名

RepositoryRestMvcConfiguration

已更改,露出IDS的正确代码为:

@Configuration
public class RepoConf extends RepositoryRestMvcConfiguration {
    public RepoConf(ApplicationContext context, ObjectFactory<ConversionService> conversionService) {
        super(context, conversionService);
    }
    @Override
    public ProfileResourceProcessor profileResourceProcessor(RepositoryRestConfiguration config) {
        config.exposeIdsFor(Lot.class);
        return super.profileResourceProcessor(config);
    }
}

这是我在http://localhost上工作的请求:9000/lots/1_0:

{
    "id": {
        "idLot": 1,
        "versionLot": "0"
    },
    "descLot": "test col",
    "etatLot": "init",
    "typeLot": "install",
    "userMaj": "bibi",
    "dateMaj": null,
    "livraisonLots": [],
    "environnementLots": [],
    "_links": {
        "self": {
            "href": "http://localhost:9000/lots/1_0"
        },
        "lot": {
            "href": "http://localhost:9000/lots/1_0"
        }
    }
}

最新更新