自定义HATEOAS URI

时间:2014-12-15 01:18:26

标签: spring-boot

我正在进行一个Spring Boot Data Rest项目。

具体来说,我有以下依赖项:

dependencies {
    compile 'org.springframework.boot:spring-boot-starter-data-jpa:1.1.9.RELEASE'
    compile 'org.springframework.boot:spring-boot-starter-data-rest:1.1.9.RELEASE'
    compile 'org.springframework.boot:spring-boot-starter-actuator:1.1.9.RELEASE'
}

我有三个实体:

@Entity
@Table(name = "prefecture", uniqueConstraints={@UniqueConstraint(columnNames = {"code", "name"})})
public class Prefecture implements Serializable {

    private static final long serialVersionUID = -4664664252005282494L;

    @Id
    @Column(name = "code")
    private Integer code;

    @Column(name = "name")
    private String name;

    @OneToMany(cascade = CascadeType.ALL, mappedBy = "prefecture")
    private List<City> cities;

    ...



@Entity
@Table(name = "city", uniqueConstraints = {@UniqueConstraint(columnNames = {"code", "name"})})
public class City implements Serializable {

    private static final long serialVersionUID = 1077260811602686775L;

    @Id
    @Column(name = "code")
    private Integer code;

    @ManyToOne
    @JoinColumn(name = "prefecture_code", referencedColumnName = "code")
    private Prefecture prefecture;

    @Column(name = "name", unique = true)
    private String name;

    @OneToMany(cascade = CascadeType.ALL, mappedBy = "city")
    private List<TownArea> townAreas;

    ...



@Entity
@Table(name = "town_area", uniqueConstraints={@UniqueConstraint(columnNames = {"name"})})
public class TownArea implements Serializable {

    private static final long serialVersionUID = -4908446167092081914L;

    @Id
    @GeneratedValue(strategy = GenerationType.IDENTITY)
    @Column(name = "id")
    private Integer id;

    @Column(name = "name")
    private String name;

    @ManyToOne
    @JoinColumn(name = "city_code", referencedColumnName = "code")
    private City city;

    @Column(name = "prefecture_code")
    private Integer prefectureCode;

    @Column(name = "postal_code")
    private String postalCode;

    ...

还有三个存储库:

@RepositoryRestResource(collectionResourceRel = "cities", path = "cities")
public interface CityRepository extends PagingAndSortingRepository<City, Integer> {}


@RepositoryRestResource(collectionResourceRel = "prefectures", path = "prefectures")
public interface PrefectureRepository extends PagingAndSortingRepository<Prefecture, Integer> {}


@RepositoryRestResource(collectionResourceRel = "town_areas", path = "town_areas")
public interface TownAreaRepository extends PagingAndSortingRepository<TownArea, Integer> {}

鉴于此,当我在本地运行我的应用程序时,我有一组这样的URL:

http://localhost:8080/prefectures

http://localhost:8080/prefectures/1

http://localhost:8080/prefectures/1/cities

http://localhost:8080/cities/2/townareas

http://localhost:8080/townareas/3

但是,我想配置以下网址:

http://localhost:8080/prefectures/1/cities/2/

http://localhost:8080/prefectures/1/cities/2/townareas

http://localhost:8080/prefectures/1/cities/2/townareas/3

有没有办法可以自定义uris来实现这个目标?

0 个答案:

没有答案