添加与SDN4 + REST中另一个节点的关系

2024-05-16

我构建了一个简单的 SDN4 + REST API:

一个端点,名为player,包含一组属性。

每个玩家都有一个Set<Player> friends财产。

GET、POST、PUT、DELETE 和 PATCH 在 /player/{id} 上就像魅力一样工作

这里的问题是/player/{id}/friends。

我不知道如何向玩家添加好友,这是我迄今为止尝试过的:

  • 测试前:

curl http://localhost:8080/api/player/1/friends: { }

  • The Test

curl -i -X PATCH -H "Content-Type:application/json" -d '{"id":1, "name":"Player2", "password":"", "email":"[email protected] /cdn-cgi/l/email-protection", "elo":1200}' http://localhost:8080/api/player/1/friends:

HTTP/1.1 204 No Content Server: Apache-Coyote/1.1 Date: Wed, 04 Nov 2015 13:03:17 GMT

  • 测试后:

curl http://localhost:8080/api/player/1/friends { }

也尝试使用 PUT,结果相同。

我也尝试过 POST,但收到“不允许的方法”。

这是我的存储库:

@RepositoryRestResource(collectionResourceRel="player", path="player")
public interface PlayerRepository extends PagingAndSortingRepository<Player, Long> {

    Player findByid(@Param("0") long id);
}

还有我的模型:

@NodeEntity
public class Player {

    @GraphId Long id;

    String name;

    String email;

    @Transient
    String password;

    int elo;

    @RelatedTo(type="FRIEND_WITH", direction = Direction.BOTH)
    Set<Player> friends;
}

我觉得这是一个简单的愚蠢错误,但我不知道如何修复它。

EDIT:

我试过这个:$addToSet 实现用于根据 PATCH 请求更新数组 https://stackoverflow.com/questions/24814673/addtoset-implementation-for-array-update-on-patch-request

这是结果:

curl -i -X PATCH -H "Content-Type:application/json-patch+json" -d '{"op": "add", "path": "/player/2", "value":["test"]}' http://localhost:8080/api/player/1/friends
HTTP/1.1 415 Unsupported Media Type
Server: Apache-Coyote/1.1
Content-Type: application/json;charset=UTF-8
Transfer-Encoding: chunked
Date: Thu, 05 Nov 2015 11:40:31 GMT

{
  "timestamp" : "2015-11-05T11:40:31.579+0000",
  "status" : 415,
  "error" : "Unsupported Media Type",
  "exception" : "org.springframework.web.HttpMediaTypeNotSupportedException",
  "message" : "Content type 'application/json-patch+json' not supported",
  "path" : "/api/player/1/friends"
}

这是我的 pom.xml,以防万一:

<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
    xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
    <modelVersion>4.0.0</modelVersion>

    <groupId>org.afkgames</groupId>
    <artifactId>api-rest-sdn</artifactId>
    <version>0.1.0</version>

    <parent>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-parent</artifactId>
        <version>1.2.7.RELEASE</version>
    </parent>

    <dependencies>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-data-rest</artifactId>
        </dependency>
        <dependency>
            <groupId>org.springframework.data</groupId>
            <artifactId>spring-data-neo4j</artifactId>
        </dependency>
        <dependency>
            <groupId>org.hibernate</groupId>
            <artifactId>hibernate-validator</artifactId>
        </dependency>
    </dependencies>
    <properties>
        <java.version>1.8</java.version>
        <start-class>api.Bootstrap</start-class>
    </properties>
    <build>
        <plugins>
            <plugin>
                <groupId>org.springframework.boot</groupId>
                <artifactId>spring-boot-maven-plugin</artifactId>
            </plugin>
        </plugins>
    </build>

    <repositories>
        <repository>
            <id>spring-releases</id>
            <url>https://repo.spring.io/libs-release</url>
        </repository>
        <repository>
            <id>neo4j</id>
            <name>Neo4j</name>
            <url>http://m2.neo4j.org/</url>
        </repository>
    </repositories>
    <pluginRepositories>
        <pluginRepository>
            <id>spring-releases</id>
            <url>https://repo.spring.io/libs-release</url>
        </pluginRepository>
    </pluginRepositories>

</project>

到处寻找解决方案后,我找到了。

您可以使用以下方法在集合中添加项目POST with Content-Type : text/uri-list 仅适用于 spring-starter-parent 1.3.0+.

以下是 cUrl 的示例:

curl -i -X POST -H 'Content-type: text/uri-list' -d 'localhost:8080/api/player/1' http://localhost:8080/api/player/0/friends

这会将玩家 1 添加为玩家 0 的好友。

本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)

添加与SDN4 + REST中另一个节点的关系 的相关文章

随机推荐