本文介绍了在SDN4 + REST中添加与另一个节点的关系的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

I have built a simple SDN4 + REST API:

一个名为玩家的端点,其中包含一组属性.

One endpoint, named player, which contains a set of properties.

每个播放器都有一个Set<Player> friends属性.

Each player has a Set<Player> friends property.

GET,POST,PUT,DELETE和PATCH就像在/player/{id}上的超级按钮一样工作

GET, POST, PUT, DELETE and PATCH are working like charm on /player/{id}

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

The problem here is /player/{id}/friends.

我不知道如何将朋友添加到玩家中,这是我到目前为止已经尝试过的方法:

I don't find out how to add a friend to a player, here is what I've tried so far:

  • 测试之前:

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

  • 测试

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

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

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

  • 测试后:

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

也尝试了PUT,结果相同.

Also tried with PUT, same result.

我也尝试过POST,但出现了不允许的方法".

I tried with POST too, but I get a "method not allowed".

这是我的存储库:

@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;
}

我觉得这是一个简单的愚蠢错误,但我找不到解决方法.

I feel like it's a simple dumb error, but I don't find how to fix it.

我尝试了此操作: $ addToSet实现,用于对PATCH请求进行数组更新

结果如下:

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,以防万一:

Here is my pom.xml, just in case:

<?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>

推荐答案

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

After looking everywhere for a solution, I found it.

您只能使用POSTContent-Type : text/uri-list 在spring-starter-parent 1.3.0 + 中添加项目.

You can add an item in a collection using POST with Content-Type : text/uri-list only with spring-starter-parent 1.3.0+.

以下是cUrl的示例:

Here is an example with 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的朋友.

This adds the player 1 as friend of the player 0.

这篇关于在SDN4 + REST中添加与另一个节点的关系的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

11-03 11:34