1

Spring Boot と Spring HATEOAS を使用して REST API を構築しています。

私は2つの単純なオブジェクトを持っています。まあ言ってみれば:

// Model
@Entity
public class Person {
    private String  name;
    private Address address;
    // ... usual methods omitted for brievity
}

@Entity
public class Address {
    private String street;
    private String city;
    // ...
}

// Repository. It exposes directly a REST service
public interface PersonRepository extends PagingAndSortingRepository<Person, Long> {}

// Application entry point
@ComponentScan
@EnableAutoConfiguration
@EnableJpaRepositories
@Import(RepositoryRestMvcConfiguration.class)
public class Application {

    public static void main(String[] args) {
        SpringApplication.run(Application.class, args);
    }

}

この単純なプロジェクトは、次のような出力を作成します。

{
    "_links": {
        "self": {
            "href": "http://localhost:8080/persons{?page,size,sort}",
            "templated": true
        }
    },
    "_embedded": {
        "persons": [
            {
                "name": "Some name",
                "_links": {
                    "self": {
                        "href": "http://localhost:8080/persons/1"
                    },
                    "address": {
                        "href": "http://localhost:8080/persons/1/address"
                    }
                }
            }
        ]
    }
}

わかりましたが、アプリケーションが Address オブジェクトを応答で直接送信するようにしたいと思います。アドレスの URL を照会する必要がないようにします。

何かのようなもの:

...
        "persons": [
            {
                "name": "Some name",
                "address": {
                    "street": "Some street name"
                    "city": "Some city name"
                }
                "_links": {
                    "self": {
                        "href": "http://localhost:8080/persons/1"
                    },
                    "address": {
                        "href": "http://localhost:8080/persons/1/address"
                    }
                }
            }
        ]
...

それを行うための構成はありますか?Spring HATEOAS docs でそれに関する構成を見つけることができませんでした。これは、通常の Spring コントローラーのみを使用する場合のデフォルトの動作です。

4

2 に答える 2

1

Spring Data REST のドキュメントの最新リリースは、抜粋プロジェクションを示しています。これにより、データ コレクションの別のデフォルト ビューが提供されます。

あなたのユースケースは、もともと設計されていた正確な角度です。

于 2015-09-24T02:23:48.103 に答える