Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/clojure/3.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Spring数据rest不正确的资源href_Spring_Spring Data_Spring Data Jpa_Spring Data Rest_Spring Hateoas - Fatal编程技术网

Spring数据rest不正确的资源href

Spring数据rest不正确的资源href,spring,spring-data,spring-data-jpa,spring-data-rest,spring-hateoas,Spring,Spring Data,Spring Data Jpa,Spring Data Rest,Spring Hateoas,我在使用SpringDataRESTwithSpringDataJPA获取正确的url以显示self-href时遇到问题 所以我有一个学生班: @Entity @Table(name="student", schema="main") public class Student { @Id private Long id; ... ... @OneToOne @JoinColumn(name = "id") private Studen

我在使用SpringDataRESTwithSpringDataJPA获取正确的url以显示self-href时遇到问题

所以我有一个学生班:

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

    @Id
    private Long id;

    ...
    ...

    @OneToOne
    @JoinColumn(name = "id")
    private StudentInformation studentInformation;
}
具有相应的存储库文件

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

}
(省略了其他属性/getter/setter)

具有相应的存储库

@RepositoryRestResource(collectionResourceRel = "studentinformation", path = "students/studentinformation")
public interface StudentInformationRepository extends CrudRepository<StudentInformation, Long> {
}
除非我遵循从学生到学生信息的链接,否则学生信息的自我链接不正确

{
  "id": 1,
  ...
  ...
  "_links": {
    "self": {
      "href": "http://localhost:8080/students/studentinformation/1"
    },
    "studentinformation": {
      "href": "http://localhost:8080/students/studentinformation/1"
    }
  }
}
我该如何阅读该链接 “href”:” 而不是 “href”:”


谢谢

首先,像
学生/studentinformation
这样的多段路径可能不起作用,因为它不受支持,请参见,因此不要专注于设计URL。如果您确实需要
http://localhost:8080/students/1/studentinformation
representation-您需要为此定义自定义控制器,而不需要依靠弹簧数据支架

其次,在
/students
端点上使用不同的学生数据不是更好吗?如果这只是
学生
资源的不同表示形式,我会使用投影,例如
学生/1?投影=最小
学生/1?投影=完整


如果
studentinformation
包含与
students
完全不同的数据,并且它不是
Student
资源的表示,只需为
/studentinformation

定义一个端点,感谢您的响应。那么您是说没有办法在self-href URL中堆叠资源?比如,
http://localhost:8080/RESOURCE/{id}/SUB_RESOURCE
使用@RepositoryRestResource?我必须定义我自己的自定义REST控制器?这似乎很奇怪。是的,你可以在我提供的链接中检查,至少在存储库中没有。如果你能找到任何方法,很高兴知道。方法是定义@RepositoryRestController使用自定义实现并将其与资源关联。如果要使用存储库实现,可以执行
SUB_resource/{ID}?resource_ID={resource_ID}
{
  "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"
    }
  }
} 
{
  "id": 1,
  ...
  ...
  "_links": {
    "self": {
      "href": "http://localhost:8080/students/studentinformation/1"
    },
    "studentinformation": {
      "href": "http://localhost:8080/students/studentinformation/1"
    }
  }
}