项目作者: igapyon

项目描述 :
Simple REST API sample.
高级语言: Java
项目地址: git://github.com/igapyon/simple-restapi.git
创建时间: 2020-04-25T13:44:46Z
项目社区:https://github.com/igapyon/simple-restapi

开源协议:Apache License 2.0

下载


simple-restapi

シンプルな Spring Web + REST API + Jackson のサンプルです。

最初に想定 JSON データを作成します。

simplesample.json

  1. [
  2. {
  3. "id": 2,
  4. "name": "hanako",
  5. "age": 22
  6. },
  7. {
  8. "id": 3,
  9. "name": "takuya",
  10. "age": 25
  11. }
  12. ]

次に この JSONデータをもちいて Java Bean を生成します。

  1. http://www.jsonschema2pojo.org/

これで出来あがった Java Bean をプロジェクトに含めて開発します。

あとは、出力したいデータを作って、ObjectMapper に文字列化させれば完成。

  1. ObjectMapper mapper = new ObjectMapper();
  2. mapper.enable(SerializationFeature.INDENT_OUTPUT);
  3. String result = mapper.writeValueAsString(resultUsers);

このサンプルの起動方法は以下の通り。

  1. mvn spring-boot:run

サンプル画面への到達方法は以下。

  1. http://localhost:8080/api/simpleget/id5535

このサンプルの実行結果

  1. [
  2. {
  3. "id" : 2,
  4. "name" : "hanako",
  5. "age" : 22
  6. },
  7. {
  8. "id" : 3,
  9. "name" : "takuya",
  10. "age" : 25
  11. }
  12. ]