1

サンプルプロジェクトの https://spring.io/guides/gs/accessing-data-rest/ に基づいhttps://github.com/jcoig/gs-accessing-data-rest )私は次のように定義されたリポジトリを持っています:

@RepositoryRestResource
public interface PersonRepository extends PagingAndSortingRepository<Person, Long> {
    List<Person> findByLastName(@Param("name") String name);
}

このように定義されたリポジトリは経由で利用できhttp://localhost:8080/persons、応答は次のとおりです。

{
  "_links" : {
    "self" : {
      "href" : "http://localhost:8080/persons{?page,size,sort}",
      "templated" : true
    },
    "search" : {
      "href" : "http://localhost:8080/persons/search"
    }
  },
  "_embedded" : {
    "persons" : [ {
      "firstName" : "John",
      "lastName" : "Smith",
      "_links" : {
        "self" : {
          "href" : "http://localhost:8080/persons/1"
        }
      }
    } ]
  },
  "page" : {
    "size" : 20,
    "totalElements" : 1,
    "totalPages" : 1,
    "number" : 0
  }
}

personsURL に含めたくないし、返された JSON のキーとしても持ちたくありませんpersons。もちろん、リポジトリを次のように定義できます。

@RepositoryRestResource(collectionResourceRel = "key", path = "path")
public interface PersonRepository extends PagingAndSortingRepository<Person, Long> {
    List<Person> findByLastName(@Param("name") String name);
}

しかし、私の質問は、デフォルトのSpringの動作を変更し、カスタムキーとカスタムパスプロバイダーを取得する方法です(サフィックスを無効にする例として)。s

4

2 に答える 2

2

@Order(value = Ordered.HIGHEST_PRECEDENCE)カスタマイズされたインスタンスに適用するソリューションが機能しRelProviderない場合は、次の回避策が役立つ場合があります。

@Configuration
@Import(RepositoryRestMvcConfiguration.class)
public class RestMvcConfigurer extends RepositoryRestMvcConfiguration
{
...
@Override
public ResourceMappings resourceMappings()
{

  final Repositories repositories = repositories();
  final RepositoryRestConfiguration config = config();
  return new RepositoryResourceMappings( config, repositories, new YourCustomRelProvider());
}
}

evo-inflectorさらに、クラスパスから除外する必要がありました:

<dependency>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-data-rest</artifactId>
        <exclusions>
            <exclusion>
                <groupId>org.atteo</groupId>
                <artifactId>evo-inflector</artifactId>
            </exclusion>
        </exclusions>
    </dependency>

本当に良い解決策ではありませんが、私のセットアップではうまくいきます。

于 2014-10-14T09:00:35.227 に答える