it-source

Using RepositoryRestResource annotation to change RESTful endpoint not working

criticalcode 2023. 11. 2. 21:50
반응형

Using RepositoryRestResource annotation to change RESTful endpoint not working

I am new to Spring boot. I was trying to create RESTful web service which also plugs into MongoDB. Everything works fine as the guide explains except for this.

package hello.requests;

import java.util.List;
import org.springframework.data.mongodb.repository.MongoRepository;
import org.springframework.data.repository.query.Param;
import org.springframework.data.rest.core.annotation.RepositoryRestResource;

import hello.models.CustomerModel;

@RepositoryRestResource(collectionResourceRel = "people", path = "people")
public interface CustomerRepository extends MongoRepository<CustomerModel, String> {

    List<CustomerModel> findByLastName(@Param("name") String name);

}

여기서 저장소에 대한 RESTful 끝점을 기본값에서 변경하려고 합니다./customerModels로./people. 하지만 이걸 실행하면 404가 나옵니다./people하지만 잘 작동합니다./customerModels. 넓은 의미에서 어떻게.@RepositoryRestResource일이요? 제가 여기서 뭘 잘못하고 있나요?

슬래시를 사용할 수 없습니다.pathattribute. 그러나 application.properties에서 기본 경로를 설정할 수 있습니다.

# DATA REST (RepositoryRestProperties)
spring.data.rest.base-path=/my/base/uri
# Base path to be used by Spring Data REST to expose repository resources.

Without seeing your entire configuration it is hard to know exactly what is going on in your situation. However using the latest guide at https://github.com/spring-guides/gs-accessing-data-mongodb.git I am able to get it working by making the following changes:

  • Adding spring-boot-starter-data-rest as a dependency in the POM file.

    <dependency>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-data-rest</artifactId>
    </dependency>
    
  • Adding this annotation to the CustomerRepository class.

    @RepositoryRestResource(path = "people")
    
  • Setting up getters and setters in the Customer class for the 2 name fields in the constructor to avoid a Jackson serialization error.

Using this when I run the application I am able to access the repository at http://localhost:8080/people. If I remove the annotation then the CustomerRepository is accessed at http://localhost:8080/customers. Let me know if you want me to post a fork on GitHub.

To answer your question about what RepositoryRestResource is that it overrides the attributes for the ResourceMapping that is created by default. It's attributes are used in creating the mapping and change the related return values of the methods on the mapping class. By default Spring Data Rest creates defaults based on the class names of the objects used in the repository definition.

/customerModels기본 메서드가 목록을 반환하므로 기본적으로 생성됩니다.CustomerModel. 그래서 당신은 이것을 추가할 수 있습니다.@RestResource(path = "names")다음과 같이 접근할 수 있습니다.http://localhost:8080/yourapp/people/search/names. 여기 보기: Spring data 문서

ReferenceURL : https://stackoverflow.com/questions/31609078/using-repositoryrestresource-annotation-to-change-restful-endpoint-not-working

반응형