1

spring-data jpa로 스프링 데이터 나머지를 사용하여 자체 href에 대한 올바른 URL을 표시하는 데 문제가 있습니다. StudentInformation 클래스스프링 데이터 - 나머지 잘못된 리소스 href

@Entity 
@Table(name="studentinformation", schema="main") 
public class StudentInformation { 

    @Id 
    private Long id; 

    ... 
    ... 
} 

(다른 속성/게터/세터도 있습니다 해당 저장소 파일

@RepositoryRestResource(collectionResourceRel = "students", path = "students") 
public interface StudentRepository extends PagingAndSortingRepository<Student, Long> { 

} 

@Entity 
@Table(name="student", schema="main") 
public class Student { 

    @Id 
    private Long id; 

    ... 
    ... 

    @OneToOne 
    @JoinColumn(name = "id") 
    private StudentInformation studentInformation; 
} 

:

그래서 나는 학생 클래스가 생략 됨)

해당 저장소 내가 ID로 하나를 검색 할 때이 기대하는 것처럼

@RepositoryRestResource(collectionResourceRel = "studentinformation", path = "students/studentinformation") 
public interface StudentInformationRepository extends CrudRepository<StudentInformation, Long> { 
} 

학생 디스플레이와

, 내가 studentInformation 학생들의 링크를 따라하는 경우를 제외하고

{ 
    "id": 1, 
    ... 
    ... 
    "_links": { 
    "self": { 
     "href": "http://localhost:8080/students/1" 
    }, 
    "student": { 
     "href": "http://localhost:8080/students/1" 
    }, 
    "studentinformation": { 
     "href": "http://localhost:8080/students/1/studentinformation" 
    } 
    } 
} 

, studentInformation는있다 잘못된 자기 링크.

{ 
    "id": 1, 
    ... 
    ... 
    "_links": { 
    "self": { 
     "href": "http://localhost:8080/students/studentinformation/1" 
    }, 
    "studentinformation": { 
     "href": "http://localhost:8080/students/studentinformation/1" 
    } 
    } 
} 

가 어떻게 "HREF"를 읽어 해당 링크를받을 수 있나요 : HREF "http://localhost:8080/students/1/studentinformation 대신 의" ":"http://localhost:8080/students/studentinformation/1 "

모두 감사의

답변

1

첫째, 다중 세그먼트 경로 students/studentinformation은 지원되지 않으므로 작동하지 않을 수 있습니다. this answer을 참조하십시오. 따라서 URL 디자인에 집중하지 마십시오. 실제로 http://localhost:8080/students/1/studentinformation 표현이 필요한 경우 - 해당 사용자 정의 컨트롤러를 정의해야합니다. Spring Data REST에 의존하지 마십시오.

두 번째로 /students 종단점에서 다른 학생 데이터를 사용하는 것이 더 좋지 않습니까? 이는 단지 Student 리소스의 다른 표현 일 뿐이므로 프로젝션과 함께 갈 것입니다. students/1?projection=minimalstudents/1?projection=full. 경우에 studentinformation

students보다 완전히 다른 데이터를 포함하고 Student 자원의 표현 아니며, 단지 /studentinformation의 엔드 포인트를 정의한다.

+0

응답 해 주셔서 감사합니다. 그렇다면 자체 href URL에 리소스를 스택 할 방법이 없다는 말입니까? like, 'http : // localhost : 8080/RESOURCE/{ID}/SUB_RESOURCE' @RepositoryRestResource 사용? 내 자신의 사용자 지정 REST 컨트롤러를 정의해야합니까? 이상하게 보입니다. – user2254140

+0

불행히도 예, 최소한 제공된 저장소의 링크가 아닌지 확인할 수 있습니다. 당신이 그것을 할 수있는 어떤 방법을 발견한다면 - 알고 좋은 것입니다. 이를 수행 할 수있는 방법은 사용자 정의 구현으로 @RepositoryRestController를 정의하고이를 자원과 연관시키는 것입니다. –

+0

저장소 구현으로 가고 싶다면'SUB_RESOURCE/{ID}? RESOURCE_ID = {RESOURCE_ID}'할 수 있습니다. –

관련 문제