You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

请求完善支持算术运算的POST API端点实现(附现有代码)

修复算术运算API实现方案

需求回顾

需要实现一个POST类型API端点,接收如下JSON请求体:

{ "operation_type": "addition | subtraction | multiplication" , "x": Integer, "y": Integer }

根据operation_type对x和y执行对应算术运算,返回格式如下:

{ "slackUsername": String, "operation_type" : Enum值, "result": Integer }

原代码存在的问题

  • Model类缺少getter/setter,无法正确接收请求参数
  • Service类逻辑错误:乘法运算误用加法,返回的Operator依赖未初始化的this.operation_type,且未使用传入的x/y参数
  • Controller未接收请求体,未调用Service的运算逻辑,返回值不符合要求
  • 缺少请求/响应的DTO封装,无法自动完成JSON序列化/反序列化

完整修复代码

1. 请求DTO(接收前端参数)

public class OperationRequest {
    private String operation_type;
    private Integer x;
    private Integer y;

    // Getter和Setter
    public String getOperation_type() {
        return operation_type;
    }

    public void setOperation_type(String operation_type) {
        this.operation_type = operation_type;
    }

    public Integer getX() {
        return x;
    }

    public void setX(Integer x) {
        this.x = x;
    }

    public Integer getY() {
        return y;
    }

    public void setY(Integer y) {
        this.y = y;
    }
}

2. 响应DTO(返回结果)

public class OperationResponse {
    private String slackUsername;
    private String operation_type;
    private Integer result;

    // 构造方法
    public OperationResponse(String slackUsername, String operation_type, Integer result) {
        this.slackUsername = slackUsername;
        this.operation_type = operation_type;
        this.result = result;
    }

    // Getter(序列化需要)
    public String getSlackUsername() {
        return slackUsername;
    }

    public String getOperation_type() {
        return operation_type;
    }

    public Integer getResult() {
        return result;
    }
}

3. Operator枚举类

public enum Operator {
    ADDITION("addition"),
    SUBTRACTION("subtraction"),
    MULTIPLICATION("multiplication"),
    UNKNOWN("unknown");

    private final String value;

    Operator(String value) {
        this.value = value;
    }

    // 根据字符串获取枚举
    public static Operator fromValue(String value) {
        for (Operator op : Operator.values()) {
            if (op.value.equalsIgnoreCase(value)) {
                return op;
            }
        }
        return UNKNOWN;
    }

    public String getValue() {
        return value;
    }
}

4. Service业务逻辑类

import org.springframework.stereotype.Service;

@Service
public class OperationService {
    private static final String SLACK_USERNAME = "Ajava";

    public OperationResponse calculate(OperationRequest request) {
        Operator operator = Operator.fromValue(request.getOperation_type());
        Integer result = 0;

        switch (operator) {
            case ADDITION:
                result = request.getX() + request.getY();
                break;
            case SUBTRACTION:
                result = request.getX() - request.getY();
                break;
            case MULTIPLICATION:
                result = request.getX() * request.getY();
                break;
            default:
                throw new IllegalArgumentException("不支持的运算类型: " + request.getOperation_type());
        }

        return new OperationResponse(SLACK_USERNAME, operator.getValue(), result);
    }
}

5. Controller控制器类

import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RestController;

@RestController
public class OperationController {
    private final OperationService operationService;

    // 构造注入Service
    public OperationController(OperationService operationService) {
        this.operationService = operationService;
    }

    @PostMapping(path = "/operation", consumes = "application/json", produces = "application/json")
    public ResponseEntity<OperationResponse> postOperation(@RequestBody OperationRequest request) {
        OperationResponse response = operationService.calculate(request);
        return ResponseEntity.ok(response);
    }
}

代码说明

  • 使用DTO类明确请求和响应结构,Spring会自动完成JSON与对象的转换
  • 枚举类增加了字符串映射方法,避免硬编码字符串判断
  • Service类专注业务逻辑,通过switch处理不同运算,修复了乘法运算错误
  • Controller通过@RequestBody接收请求参数,注入Service并调用运算方法,返回符合要求的响应

内容的提问来源于stack exchange,提问作者Plus Ultra

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.08.13 14:30:40