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
일이요? 제가 여기서 뭘 잘못하고 있나요?
슬래시를 사용할 수 없습니다.path
attribute. 그러나 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
'it-source' 카테고리의 다른 글
반환된 변수의 기억을 자유롭게 하는 적절한 방법 (0) | 2023.11.02 |
---|---|
어린이 중 한 명이 포커스를 받을 경우 블러 이벤트가 발생하지 않도록 합니다. (0) | 2023.11.02 |
인수 배열을 Powershell 명령줄에 전달하는 방법 (0) | 2023.11.02 |
Android M - 런타임 권한 확인 - 사용자가 "다시 묻지 않음"을 선택했는지 확인하는 방법은? (0) | 2023.10.28 |
32비트 응용 프로그램은 64비트 Linux에서 시스템 호출을 어떻게 합니까? (0) | 2023.10.28 |