1

各位 Spring Data REST 爱好者,我正在运行我的 Spring Data REST 1.1 应用程序,我正在尝试使用 curl 添加使用“text/uri-list”内容类型的实体关系,如链接中所述:Example-API-与卷曲一起使用

curl -v -d "http://localhost:8080/simplemvc/rest/enemies/3" -H "Content-Type: text/uri-list" http://localhost:8080/simplemvc/rest/heroes/1/defeatedEnemies

不幸的是,虽然服务器返回“201 Created”,但主体包含一个空的 JSON 对象,并且实体关系没有被创建:

{
  "links" : [ ],
  "content" : [ ]
}

我希望看到正在执行 SQL UPDATE,但分析 SQL 显示只有 SELECT 语句发生:

Hibernate: select hero0_.HERO_ID as HERO1_1_0_, hero0_.name as name2_1_0_ from HERO hero0_ where hero0_.HERO_ID=?
Hibernate: select defeateden0_.HERO_ID as HERO4_1_1_, defeateden0_.ENEMY_ID as ENEMY1_0_1_, defeateden0_.ENEMY_ID as ENEMY1_0_0_, defeateden0_.description as descript2_0_0_, defeateden0_.HERO_ID as HERO4_0_0_, defeateden0_.name as name3_0_0_ from ENEMY defeateden0_ where defeateden0_.HERO_ID=?
Hibernate: select enemy0_.ENEMY_ID as ENEMY1_0_1_, enemy0_.description as descript2_0_1_, enemy0_.HERO_ID as HERO4_0_1_, enemy0_.name as name3_0_1_, hero1_.HERO_ID as HERO1_1_0_, hero1_.name as name2_1_0_ from ENEMY enemy0_ left outer join HERO hero1_ on enemy0_.HERO_ID=hero1_.HERO_ID where enemy0_.ENEMY_ID=?

有趣的是,如果我通过在数据库客户端中执行 SQL 语句“手动”添加关系:

UPDATE ENEMY SET HERO_ID = 1 WHERE ENEMY_ID = 1;

然后执行 curl 语句:

curl -v -H "Accept: application/json" http://localhost:8080/simplemvc/rest/heroes/1/defeatedEnemies

我得到了一个 JSON 表示,它通过超媒体链接演示 Spring Data REST 识别 Hero 和 Enemy 实体之间的一对多关系:

{
  "links" : [ ],
  "content" : [ {
    "name" : "Red Ghost",
    "description" : "Likes to chase",
    "links" : [ {
      "rel" : "self",
      "href" : "http://localhost:8080/simplemvc/rest/enemies/1"
    }, {
      "rel" : "enemy.enemy.hero",
      "href" : "http://localhost:8080/simplemvc/rest/enemies/1/hero"
    } ]
  } ]
}

这是一个现有的 Spring MVC 应用程序,正在向其中添加 Spring Data REST,使用以下文章作为指南:Adding-Spring-Data-REST-to-an-existing-Spring-MVC-Application

我尝试过使用 H2 和 MySQL 数据库,结果相同。下面是我的 JPA 实体、Spring Data JPA 存储库、应用程序上下文和 web.xml:

英雄.java

@Entity
@Table(name = "HERO")
public class Hero {

    @TableGenerator(
            name="heroGen", 
            table="ID_GEN", 
            pkColumnName="GEN_KEY", 
            valueColumnName="GEN_VALUE", 
            pkColumnValue="HERO_ID", 
            allocationSize=1)
    @Id
    @GeneratedValue(strategy=TABLE, generator="heroGen")
    @Column(name = "HERO_ID")
    private Integer id;

    @Column
    private String name;

    @OneToMany(fetch = FetchType.LAZY, cascade = CascadeType.ALL, mappedBy = "hero")
    private Set<Enemy> defeatedEnemies;

    ...
}

敌人.java

@Entity
@Table(name = "ENEMY")
public class Enemy {

    @TableGenerator(name="enemyGen", 
            table="ID_GEN", 
            pkColumnName="GEN_KEY", 
            valueColumnName="GEN_VALUE", 
            pkColumnValue="ENEMY_ID", 
            allocationSize=1)
    @Id
    @GeneratedValue(strategy=TABLE, generator="enemyGen")
    @Column(name="ENEMY_ID")
    private Integer id;

    @Column
    private String name;

    @Column
    private String description;

    @ManyToOne
    @JoinColumn(name = "HERO_ID")
    private Hero hero;

    ...
}

HeroRepository.java

@RestResource(path = "heroes")
public interface HeroRepository extends CrudRepository<Hero, Integer> {

}

EnemyRepository.java

@RestResource(path = "enemies")
public interface EnemyRepository extends CrudRepository<Enemy, Integer> {

}

root-context.xml包括:

<jpa:repositories base-package="com.simple.simplemvc.repositories" />   

<bean id="restConfig" class="org.springframework.data.rest.webmvc.config.RepositoryRestMvcConfiguration"/>

web.xml包括:

<context-param>
    <param-name>contextConfigLocation</param-name>
    <param-value>/WEB-INF/spring/root-context.xml</param-value>
</context-param>

<servlet>
    <servlet-name>rest-dispatcher</servlet-name>
    <servlet-class>org.springframework.data.rest.webmvc.RepositoryRestDispatcherServlet</servlet-class>
    <load-on-startup>1</load-on-startup>
</servlet>

<servlet-mapping>
    <servlet-name>rest-dispatcher</servlet-name>
    <url-pattern>/rest/*</url-pattern>
</servlet-mapping>

有任何想法吗?谢谢!

4

2 回答 2

2

要添加现有实体之间的关系,请尝试使用 POST

Content-Type: application/json

http://localhost:8080/simplemvc/rest/enemies/3

有身体

{"hero":"http://localhost:8080/simplemvc/rest/heroes/1"}

要添加与英雄相关的新敌人实体,请尝试 POST

http://localhost:8080/simplemvc/rest/enemies

有身体

{"name":"Enemy name", "description":"Enemy description", "hero":"http://localhost:8080/simplemvc/rest/heroes/1"}

在 spring-data-rest-webmvc 版本 2.2.0.RELEASE 上测试

于 2014-10-14T12:18:48.203 回答
0

我想你也面临着我在这里问过的同样的问题:

Spring Data Rest,多对一

答案解决了在关系的多方添加实体的问题,尽管不使用“text/uri-list”。

于 2013-11-28T11:19:23.130 回答