创建一个将对象保存在对象中的控制器方法

问题描述 投票:0回答:1

我正在尝试为创建车辆对象的微服务创建控制器,但是在此车辆对象中,我们必须首先保存注册对象。我能够做车辆控制器,但不确定如何将登记对象保存在车辆对象中。以下是JSON对象最终应该是什么样子。

JSON输入:

{
    "make":"Chevrolet", 
    "Model":"Silverado 1500", 
    "modelYear":2009, 
    "registration": {
        "licensePlate":"TOWME2", 
        "licensedTo":"Ford Towers"
    }
}

JSON输出:

{
    "id":[auto-generated], 
    "make":"Chevrolet", 
    "model":"Silverado 1500", 
    "modelYear":2009, 
    "registration": {
        "id":[auto-generated], 
        "licensePlate":"TOWME2", 
        "licensedTo":"Ford Towers"
    }
}
spring-boot spring-mvc controller microservices
1个回答
0
投票

我也遇到了这个问题,因为spring-boot-starter-data-rest排除了ID字段。

要自定义其行为方式,可以扩展RepositoryRestConfigurerAdapter以显示特定类的ID。

import org.springframework.context.annotation.Configuration;
import org.springframework.data.rest.core.config.RepositoryRestConfiguration;
import org.springframework.data.rest.webmvc.config.RepositoryRestConfigurerAdapter;

@Configuration
public class RepositoryConfig extends RepositoryRestConfigurerAdapter {
    @Override
    public void configureRepositoryRestConfiguration(RepositoryRestConfiguration config) {
        config.exposeIdsFor(Vehicle.class);
    }
}
© www.soinside.com 2019 - 2024. All rights reserved.