Spring MVC コントローラーを使用していますが、POST 操作でまだ問題が発生します。問題を解決せずに、stackoverflow で多くのソリューションを読みました。
現時点での私の成果:
- ID を指定して GET リクエストを送信し、JSON に正常に変換されたオブジェクトを返しました。
POST
JSON ボディのリクエストを送信できませんでした。return = 415 UNSUPPORTED_MEDIA_TYPE
1) pom.xml に Jackson API を追加しました: 1.8.5
2) 私の Spring 構成ファイル: 必要なすべてのパーツを追加しました:
- ビューリゾルバー
- org.springframework.web.servlet.mvc.annotation.AnnotationMethodHandlerAdapter
- MappingJacksonHttpMessageConverter
- mvc:注釈駆動型
- コントローラーをスキャン
3)私のモデルオブジェクトは単純です:ID、名前、金額を持つアカウント
@Document
public class Account implements Serializable {
private static final long serialVersionUID = 9058933587701674803L;
@Id
private String id;
private String name;
private Double amount=0.0;
// and all get and set methods
4)そして最後に私の単純化されたコントローラークラス:
@Controller
public class AdminController {
@RequestMapping(value="/account", method=RequestMethod.POST,
headers = {"content-type=application/json"})
@ResponseStatus( HttpStatus.CREATED )
public void addAccount(@RequestBody Account account){
log.debug("account from json request " + account);
}
@RequestMapping(value="/account/{accountId}", method=RequestMethod.GET)
@ResponseBody
public Account getAccount(@PathVariable("accountId") long id){
log.debug("account from json request " + id);
return new Account();
}
}
5) クライアント側では、curl コマンドを実行しました: 成功したGET
コマンド:
curl -i -GET -H 'Accept: application/json' http://myhost:8080/compta/account/1
失敗したPOST
コマンド:
curl -i -POST -H 'Accept: application/json' -d '{"id":1,"name":"test",amount:"0.0"}' http://myhost:8080/compta/account
私が間違っているアイデアはありますか?