通过spring rest存储库公开复合ID

时间:2017-12-08 07:17:23

标签: spring rest converter composite-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 我怎么能实现呢?

1 个答案:

答案 0 :(得分:0)

好的,挖掘Spring代码,似乎方法签名关闭

RepositoryRestMvcConfiguration

已更改,公开ID的正确代码为:

@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"
        }
    }
}