やりたいこと
Spring Data REST + MongoDBで作成したAPIのうち、PUTとPATCHメソッドのみを非公開としたい。
試したこと
Spring Data RESTを利用してAPIを作成しようと思い、下記のようなコードを書いてみました。
作成したコード
Java
1@SpringBootApplication 2public class SampleApplication { 3 4 public static void main(String[] args) { 5 SpringApplication.run(SampleApplication.class, args); 6 } 7}
Java
1@Data 2public class User { 3 4 @Id 5 private int userId; 6 7 private String firstName; 8 9 private String familyName; 10}
Java
1@RepositoryRestResource(collectionResourceRel = "user") 2public interface SampleRepository extends MongoRepository<User, String> { 3}
実行結果1
http://localhost:8080/profile/users にアクセスして、CRUDに対応するAPIが出来ていることを確認しました。
{
"alps": {
"version": "1.0",
"descriptor": [
{
"id": "user-representation",
"href": "http://localhost:8080/profile/users",
"descriptor": [
{
"name": "firstName",
"type": "SEMANTIC"
},
{
"name": "familyName",
"type": "SEMANTIC"
}
]
},
{
"id": "create-user",
"name": "user",
"type": "UNSAFE",
"rt": "#user-representation"
},
{
"id": "get-user",
"name": "user",
"type": "SAFE",
"descriptor": [
{
"name": "page",
"type": "SEMANTIC",
"doc": {
"format": "TEXT",
"value": "The page to return."
}
},
{
"name": "size",
"type": "SEMANTIC",
"doc": {
"format": "TEXT",
"value": "The size of the page to return."
}
},
{
"name": "sort",
"type": "SEMANTIC",
"doc": {
"format": "TEXT",
"value": "The sorting criteria to use to calculate the content of the page."
}
}
],
"rt": "#user-representation"
},
{
"id": "get-user",
"name": "user",
"type": "SAFE",
"rt": "#user-representation"
},
{
"id": "patch-user",
"name": "user",
"type": "UNSAFE",
"rt": "#user-representation"
},
{
"id": "delete-user",
"name": "user",
"type": "IDEMPOTENT",
"rt": "#user-representation"
},
{
"id": "update-user",
"name": "user",
"type": "IDEMPOTENT",
"rt": "#user-representation"
}
]
}
}
試したこと2
次に、更新系のAPIを非公開にしてみたいと思い
下記のようにコードを修正しました。
修正したリポジトリクラス
下記ページを参照したところ、
非公開にしたいメソッドをオーバーライドして
@RestResource(exported = false)
を付けると
そのメソッドは非公開になるとの記述があったので
org.springframework.data.repository.CrudRepositoryのsaveメソッドをオーバーライドした後に
@RestResource(exported = false)
を付けて実行してみました。
■参考サイト
http://pppurple.hatenablog.com/entry/2017/03/06/221334
Java
1@RepositoryRestResource(collectionResourceRel = "user") 2public interface SampleRepository extends MongoRepository<User, String> { 3 4 @Override 5 @RestResource(exported = false) 6 public User save(User user); 7 8}
実行結果2
http://localhost:8080/profile/users にアクセスすると、
データを登録&更新するAPI(POST,PUT,PATCH)が全て非公開になっていました。
{
"alps": {
"version": "1.0",
"descriptor": [
{
"id": "user-representation",
"href": "http://localhost:8080/profile/users",
"descriptor": [
{
"name": "firstName",
"type": "SEMANTIC"
},
{
"name": "familyName",
"type": "SEMANTIC"
}
]
},
{
"id": "get-user",
"name": "user",
"type": "SAFE",
"descriptor": [
{
"name": "page",
"type": "SEMANTIC",
"doc": {
"format": "TEXT",
"value": "The page to return."
}
},
{
"name": "size",
"type": "SEMANTIC",
"doc": {
"format": "TEXT",
"value": "The size of the page to return."
}
},
{
"name": "sort",
"type": "SEMANTIC",
"doc": {
"format": "TEXT",
"value": "The sorting criteria to use to calculate the content of the page."
}
}
],
"rt": "#user-representation"
},
{
"id": "delete-user",
"name": "user",
"type": "IDEMPOTENT",
"rt": "#user-representation"
},
{
"id": "get-user",
"name": "user",
"type": "SAFE",
"rt": "#user-representation"
}
]
}
}
ちなみに、
org.springframework.data.repository.CrudRepositoryの
deleteメソッドに@RestResource(exported = false)
を付けた場合は
DELETEメソッドが非公開になることが確認できましたが、
org.springframework.data.mongodb.repository.MongoRepositoryの
insertメソッドに@RestResource(exported = false)
を付けた場合は
実行結果1と同じく全てのメソッドが公開されることを確認しております。
継承している親クラスを辿って確認しましたが、
1レコード分のデータ登録、更新系のメソッドは、下記以外に見つけられませんでした。
org.springframework.data.repository.CrudRepository#save
org.springframework.data.mongodb.repository.MongoRepository#insert
MongoRepositoryを継承したリポジトリクラスの場合、
- POSTは公開
- PUT、PATCHは非公開
といった制御は出来ないのでしょうか?
対応方法をもしご存じの方がいらっしゃいましたら
ご教示いただけますと幸いです。
開発環境
Java8
Spring Boot 2.1.8
MongoDB 4.0
Lombok
あなたの回答
tips
プレビュー