SSMプロジェクトにおけるレスポンス結果の統一化実装

レスポンス結果の統一化

1. Codeクラス・Resultクラスの作成とControllerの修正

Controllerクラスで統一されたレスポンスを返すため、CodeクラスとResultクラスを作成し、Controllerを修正します。

package com.example.controller;

public class StatusCode {
    public static final Integer CREATE_SUCCESS = 20101;
    public static final Integer CREATE_FAILURE = 20100;
    public static final Integer REMOVE_SUCCESS = 20201;
    public static final Integer REMOVE_FAILURE = 20200;
    public static final Integer MODIFY_SUCCESS = 20301;
    public static final Integer MODIFY_FAILURE = 20300;
    public static final Integer QUERY_SUCCESS = 20401;
    public static final Integer QUERY_FAILURE = 20400;
}
package com.example.controller;

public class ApiResponse {
    private Object payload;
    private Integer statusCode;
    private String description;

    public ApiResponse() {
    }

    public ApiResponse(Integer statusCode, Object payload) {
        this.statusCode = statusCode;
        this.payload = payload;
    }

    public ApiResponse(Integer statusCode, Object payload, String description) {
        this.statusCode = statusCode;
        this.payload = payload;
        this.description = description;
    }

    public Object getPayload() {
        return payload;
    }

    public void setPayload(Object payload) {
        this.payload = payload;
    }

    public Integer getStatusCode() {
        return statusCode;
    }

    public void setStatusCode(Integer statusCode) {
        this.statusCode = statusCode;
    }

    public String getDescription() {
        return description;
    }

    public void setDescription(String description) {
        this.description = description;
    }

    @Override
    public String toString() {
        return "ApiResponse{" +
                "payload=" + payload +
                ", statusCode=" + statusCode +
                ", description='" + description + '\'' +
                '}';
    }
}
package com.example.controller;

import com.example.entity.Member;
import com.example.service.DataAccessService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@RestController
@RequestMapping("/members")
public class MemberController {
    
    @Autowired
    private DataAccessService dataAccessService;

    @PostMapping
    public ApiResponse register(@RequestBody Member member) {
        boolean completed = dataAccessService.insert(member);
        return new ApiResponse(
            completed ? StatusCode.CREATE_SUCCESS : StatusCode.CREATE_FAILURE, 
            completed
        );
    }

    @DeleteMapping("/{id}")
    public ApiResponse remove(@PathVariable Integer id) {
        boolean completed = dataAccessService.delete(id);
        return new ApiResponse(
            completed ? StatusCode.REMOVE_SUCCESS : StatusCode.REMOVE_FAILURE, 
            completed
        );
    }

    @PutMapping
    public ApiResponse modify(@RequestBody Member member) {
        boolean completed = dataAccessService.update(member);
        return new ApiResponse(
            completed ? StatusCode.MODIFY_SUCCESS : StatusCode.MODIFY_FAILURE, 
            completed
        );
    }

    @GetMapping("/{id}")
    public ApiResponse fetchMember(@PathVariable Integer id) {
        Member member = dataAccessService.findById(id);
        if (member == null) {
            return new ApiResponse(StatusCode.QUERY_FAILURE, null);
        }
        return new ApiResponse(StatusCode.QUERY_SUCCESS, member);
    }

    @GetMapping
    public ApiResponse fetchAll() {
        List<Member> memberList = dataAccessService.findAll();
        if (memberList == null || memberList.isEmpty()) {
            return new ApiResponse(StatusCode.QUERY_FAILURE, null);
        }
        return new ApiResponse(StatusCode.QUERY_SUCCESS, memberList);
    }
}

2. 動作確認結果

タグ: SSM Spring MVC MyBatis Java

7月31日 07:58 投稿