Spring bootで http://blog.rakugakibox.net/entry/2014/11/23/java_spring_boot_rest を参考にしてRest APIを作成しています。下記コントローラに対して、curlを使ってPOSTリクエストを送るとMethod now allowed
エラーが出てしまいます。
コントローラ
package controllers;
import java.util.ArrayList;
import java.util.List;
import org.springframework.http.HttpHeaders;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.util.UriComponentsBuilder;
import models.HogeData;
@RestController
@RequestMapping("/hoges")
public class HogeController {
private static List<HogeData> store = new ArrayList<HogeData>();
@RequestMapping(method = RequestMethod.POST)
public ResponseEntity<HogeData> post(@ModelAttribute HogeData hogeData, UriComponentsBuilder builder){
store.add(hogeData);
HttpHeaders headers = new HttpHeaders();
headers.setLocation(builder.path("/hoges/{id}").buildAndExpand(store.size()).toUri());
return new ResponseEntity<>(hogeData, headers, HttpStatus.CREATED);
}
@RequestMapping(method = RequestMethod.GET)
public List<HogeData> getter(){
return store;
}
@RequestMapping(value = "{id}", method = RequestMethod.GET)
public HogeData get(@PathVariable int id){
return store.get(id - 1);
}
}
curl
curl -i -H "Accept: application/json" -d "integer=1" -d "string=aaa" -d "strings=AAA1" -d "strings=AAA2" -d "strings=AAA3" http://localhost:8080/hoges
レスポンス
HTTP/1.1 405 Method Not Allowed
Server: Apache-Coyote/1.1
Allow: HEAD, GET
Content-Type: application/json;charset=UTF-8
Transfer-Encoding: chunked
Date: Mon, 26 Oct 2015 00:13:44 GMT
{"timestamp":1445818424033,"status":405,"error":"Method Not Allowed","exception":"org.springframework.web.HttpRequestMethodNotSupportedException","message":"Request method 'POST' not supported","path":"/hoges"}
実行環境
OS: Mac OS X El Captan
ビルド: Gradleでgradle bootRun
を実行
コントローラでPOSTリクエストに対するメソッドは定義しているのでできるはずですがなぜかできません。なぜだかわかりますか。