2015-11-04 12 views
5

SDN4 + REST内のノードとの関係を追加します。プロパティのセットが含まれているプレイヤーの名前別の私は、単純なSDN4 + REST APIを構築しています

一方のエンドポイント、、。

各プレーヤーにSet<Player> friendsプロパティがあります。

GET、POST、PUT、DELETEおよびパッチはここに/プレーヤー/ {ID}

に魔法のように問題を作業している/プレーヤー/ {ID} /友人です。私はプレーヤーに友人を追加する方法を見つけていない

は、ここで私がこれまで試したものです:

  • テストの前に:

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

答えて

2

どこでも解決策を見つけた後、私はそれを見つけました。

POSTを使用してコレクションにアイテムを追加できます。Content-Type : text/uri-listは、spring-starter-parent 1.3.0+のみです。ここで

はカールとの例である:これは、プレイヤーの友人として、プレイヤー1が追加されます

curl -i -X POST -H 'Content-type: text/uri-list' -d 'localhost:8080/api/player/1' http://localhost:8080/api/player/0/friends 
関連する問題