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

exposing composite ID through spring rest repository

我有一个域对象

我们想在 table 中显示它,所以我需要在我的休息请求中得到它。

按照建议,我实施了

@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 我怎样才能做到这一点?

好的,挖掘 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"
        }
    }
}