0
0

More than 3 years have passed since last update.

【Java・SpringBoot】RESTサービス② - POSTメソッド

Posted at

ユーザ情報を取得、登録、更新、削除するRESTサービスを実装します
前回GETを実装したので、今回はユーザーを新規登録するPOSTメソッド処理を実装します!

RESTサービス実装クラス

  • 一件登録用のメソッドを実装
RestServiceJdbcImpl.java
package com.example.demo.login.domain.service.jdbc;

import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import com.example.demo.login.domain.model.User;
import com.example.demo.login.domain.repository.UserDao;
import com.example.demo.login.domain.service.RestService;

@Transactional
@Service
public class RestServiceJdbcImpl implements RestService {

    @Autowired
    @Qualifier("UserDaoJdbcImpl")
    UserDao dao;

    //1件登録用メソッド
    @Override
    public boolean insert(User user) {

        int result = dao.insertOne(user);

        if(result == 0) {

            return false;

        } else {

            return true;

        }
    }

    //1件検索用メソッド
    @Override
    public User selectOne(String userId) {
        return dao.selectOne(userId);
//        return null;
    }

    //全件検索用メソッド
    @Override
    public List<User> selectMany() {
        return dao.selectMany();
//        return null;
    }

    //1件更新用メソッド
    @Override
    public boolean update(User user) {

            return false;
    }

    //1件削除用メソッド
    @Override
    public boolean delete(String userId) {

            return false;
    }
}

コントローラクラス

@RequestBody

  • HTTPリクエストのボディ部分を引数にマッピング
  • POSTメソッドでユーザ情報を受け取ることができる
UserRestController.java
//中略
    /**
     * ユーザー1件登録
     */
    @PostMapping("/rest/insert")
    public String postUserOne(@RequestBody User user) {

        // ユーザーを1件登録
        boolean result = service.insert(user);

        String str = "";

        if(result == true) {

            str = "{\"result\":\"ok\"}";

        } else {

            str = "{\"result\":\"error\"}";

        }

        // 結果用の文字列をリターン
        return str;
    }

起動して確認

  • ターミナルからPOSTします
  • jsonで{"result":"ok"}が帰ってきたらうまくいってます!^^
  • ユーザー一覧にもしっかり新規Userが反映されました🌟
$ curl -XPOST "http://localhost:8080/rest/insert" -H 'Content-Type: application/json' -d '{"userId":"yamada@co.jp","password":"pass","userName":"yamada","birthday":"1911-11-11","age":"111","marriage":"false","role":"ROLE_ADMIN"}'
{"result":"ok"}

スクリーンショット 2020-12-21 11.03.14.png

(参考)コード全文

UserRestController.java
package com.example.demo.login.controller;

import java.util.List;

import com.example.demo.login.domain.model.User;
import org.springframework.beans.factory.annotation.Autowired;
//import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
//import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RestController;

//import com.example.demo.login.domain.model.User;
import com.example.demo.login.domain.service.RestService;

@RestController
public class UserRestController {

    @Autowired
    RestService service;

    /**
     * ユーザー全件取得
     */
    @GetMapping("/rest/get")
    public List<User> getUserMany() {

        // ユーザー全件取得
        return service.selectMany();
    }

    /**
     * ユーザー1件取得
     */
    @GetMapping("/rest/get/{id:.+}")
    public User getUserOne(@PathVariable("id") String userId) {

        // ユーザー1件取得
        return service.selectOne(userId);
    }

    /**
     * ユーザー1件登録
     */
    @PostMapping("/rest/insert")
    public String postUserOne(@RequestBody User user) {

        // ユーザーを1件登録
        boolean result = service.insert(user);

        String str = "";

        if (result == true) {

            str = "{\"result\":\"ok\"}";

        } else {

            str = "{\"result\":\"error\"}";

        }

        // 結果用の文字列をリターン
        return str;
    }
}
0
0
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
0
0