1
1

More than 3 years have passed since last update.

【Java・SpringBoot】RESTサービス④ - DELETEメソッド

Posted at

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

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) {

        int result = dao.updateOne(user);

        if(result == 0) {
            return false;

        } else {
            return true;
        }
    }

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

        int result = dao.deleteOne(userId);

        if(result == 0) {
            return false;

        } else {
            return true;
        }
    }
}

コントローラ編集

  • @DeleteMapping("/rest/delete/{id:.+}")でエンドポイント指定
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.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;
    }

    /**
     * ユーザー1件登録
     */
    @PutMapping("/rest/update")
    public String putUserOne(@RequestBody User user) {

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

        String str = "";

        if(result == true) {

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

        } else {

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

        }

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

    @DeleteMapping("/rest/delete/{id:.+}")
    public String deleteUserOne(@PathVariable("id") String userId) {

        // ユーザーを1件削除
        boolean result = service.delete(userId);

        String str = "";

        if(result == true) {

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

        } else {

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

        }

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

起動して確認

  • ターミナルから新規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"}'
  • 今度は削除
    • curl -XDELETE "http://localhost:8080/rest/delete/yamada@co.jp"
  • jsonで{"result":"ok"}が帰ってきたらうまくいってます!^^
  • ユーザー一覧にもしっかり削除が反映されました🌟
$ curl -XDELETE "http://localhost:8080/rest/delete/yamada@co.jp" 
{"result":"ok"}

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

1
1
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
1
1