【发布时间】:2018-07-05 22:42:03
【问题描述】:
我有一个连接到 mysql 数据库的 HATEOAS Spring rest API。我无法控制数据库架构,它会定期更改,因此我会定期生成实体类并更新服务。
每条路线我有三个文件。生成的实体类、控制器和使用PagingAndSortingRepository 类自动为我的实体提供服务的存储库文件,无需太多配置。
实体类
package hello.models;
import javax.persistence.*;
import java.util.Objects;
@Entity
@Table(name = "animals", schema = "xyz123", catalog = "")
public class AnimalsEntity {
private Integer id;
private String name;
private String description;
@Id
@Column(name = "id", nullable = false)
public Integer getId() {
return id;
}
public void setId(Integer id) {
this.id = id;
}
@Basic
@Column(name = "name", nullable = true, length = 80)
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
@Basic
@Column(name = "description", nullable = true, length = 255)
public String getDescription() {
return description;
}
public void setDescription(String description) {
this.description = description;
}
@Override
public boolean equals(Object o) {
if (this == o) return true;
if (o == null || getClass() != o.getClass()) return false;
RoleEntity that = (RoleEntity) o;
return Objects.equals(id, that.id) &&
Objects.equals(name, that.name) &&
Objects.equals(description, that.description);
}
@Override
public int hashCode() {
return Objects.hash(id, name, description);
}
}
存储库类
package hello.repositories;
@RepositoryRestResource(collectionResourceRel = "animals", path = "animals")
public interface AnimalsRepository extends PagingAndSortingRepository<AnimalEntity, String> {
// Allows /animal/cheetah for example.
AnimalEntity findByName(String name);
// Prevents POST /element and PATCH /element/:id
@Override
@RestResource(exported = false)
public AnimalEntity save(AnimalEntity s);
// Prevents DELETE /element/:id
@Override
@RestResource(exported = false)
public void delete(AnimalEntity t);
}
控制器类
package hello.controllers;
import hello.models.AnimalsEntity;
import hello.repositories.AnimalsRepository;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Pageable;
import org.springframework.data.rest.webmvc.RepositoryRestController;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import java.util.List;
@RepositoryRestController
@RequestMapping("/animals")
class PrinterController {
@Autowired
private AnimalsRepository animalsRepo;
@RequestMapping("/{name}")
public @ResponseBody
List<AnimalsEntity> findAnimal(@PathVariable(value = "name") String name) {
return animalsRepo.findByName(name);
}
}
我希望我的 HATEOAS API 使用分页/排序选项提供服务。他们目前提供响应,例如..
{
"links" : [ {
"rel" : "first",
"href" : "http://localhost:8080/animals?page=0&size=20",
"hreflang" : null,
"media" : null,
"title" : null,
"type" : null,
"deprecation" : null
}, {
"rel" : "self",
"href" : "http://localhost:8080/animals{?page,size,sort}",
"hreflang" : null,
"media" : null,
"title" : null,
"type" : null,
"deprecation" : null
}, {
"rel" : "next",
"href" : "http://localhost:8080/animals?page=1&size=20",
"hreflang" : null,
"media" : null,
"title" : null,
"type" : null,
"deprecation" : null
}, {
"rel" : "last",
"href" : "http://localhost:8080/animals?page=252&size=20",
"hreflang" : null,
"media" : null,
"title" : null,
"type" : null,
"deprecation" : null
}, {
"rel" : "profile",
"href" : "http://localhost:8080/profile/animals",
"hreflang" : null,
"media" : null,
"title" : null,
"type" : null,
"deprecation" : null
}, {
"rel" : "search",
"href" : "http://localhost:8080/animals/search",
"hreflang" : null,
"media" : null,
"title" : null,
"type" : null,
"deprecation" : null
} ],
"content" : [ {
"id" : 1,
"name" : "cheetah",
"description": "xyz
]
},{
"id" : 2,
"name" : "tortise",
"description": "xyz
]
}],
"page" : {
"size" : 20,
"totalElements" : 5049,
"totalPages" : 253,
"number" : 0
}
}
这太棒了。但我需要我的 javascript 应用程序来访问其余的 api,例如 (GET) /animals/cheetah。
通常我会更改架构并在实体类的名称属性上设置@Id,但在这种情况下我不能这样做。我无法更改数据库架构,最终我想动态生成这些实体类以允许轻松更改架构。
我发现我可以覆盖端点并手动提供它,但我失去了分页/HATEOAS 格式。
[
{
"id": 1,
"name": "cheetah",
"description": "xyz"
},
{
"id": 2,
"name": "tortise",
"description": "xyz"
}
]
如何在不丢失 JSON 格式或更改实体类的情况下完成 @Id 更改?
【问题讨论】: