首页 > 解决方案 > Spring Boot 应用程序如何在 JSONformate 中发送多个响应

问题描述

在按钮响应如何重用代码时,当我每次响应消息时都会遇到一些问题,如果条件返回响应,如果有任何解决方案,请解释我 package com.kk.springbootrestapp.controller;

import java.util.regex.Pattern;

import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.bind.annotation.RestController;

import com.kk.springbootrestapp.controller.bean.EmployeeBean;
import com.kk.springbootrestapp.controller.response.EmployeeResponse;

@RestController
public class EmployeeController {

    @RequestMapping(value = "regmep", method = RequestMethod.POST)
    public @ResponseBody EmployeeResponse registerEmp(@RequestBody EmployeeBean emp) {
        EmployeeResponse response = new EmployeeResponse();
        try {
            if (!Pattern.matches("^\\S{3,}$", emp.getEname())) {
//`enter code here`
                response.setResponseMessage("Invalid name");
                response.setResposeCode("400");
                return response;
            } else if (!Pattern.matches("^\\S{4,}$", emp.getEjob())) {
                response.setResponseMessage("Invalid job");
                response.setResposeCode("400");
                return response;
            } else if (!Pattern.matches("^[0-9]{2}", emp.getEage().toString())) {
//`enter code here`
                response.setResponseMessage("Invalid age");
                response.setResposeCode("400");
                return response;
            } else {
                response.setResponseMessage("success");
                response.setResposeCode("200");
            }

        } catch (NullPointerException e) {
            response.setResponseMessage("bad request");
            response.setResposeCode("400");
            return response;
        }
        return response;
    }
}

标签: java

解决方案


解决方案一

快速解决。您必须在代码中连接消息:

EmployeeResponse response = new EmployeeResponse();
String errorMessage = "";
if (emp == null) {
    responseMessage = "bad request"
} else {
    if (!Pattern.matches("^\\S{3,}$", emp.getEname())) {
        responseMessage += "Invalid name";
    if (!Pattern.matches("^\\S{4,}$", emp.getEjob())) {
        responseMessage += "Invalid job";
    if (!Pattern.matches("^[0-9]{2}", emp.getEage().toString())) {
        responseMessage += "Invalid age";
    }
}
if (StringUtils.isBlank(responseMessage)) {
    response.setResponseMessage("success");
    response.setResposeCode("200");
} else {
    response.setResponseMessage(responseMessage);
    response.setResposeCode("400");
}
return respone;

但这仍然是丑陋的代码!

解决方案二。

您可以通过注释使用 spring 验证器,甚至可以编写您的自定义 spring 验证器。使用起来很简单,但不是所有你都可以通过注解做的,会有情况,你必须手动验证数据。因此,您将在两个地方(注释和验证器类)有验证逻辑,当您阅读旧代码时会感到困惑。

解决方案三。

一切都在手写验证器。阅读有关全局异常处理@ControllerAdvice的信息

首先将此逻辑移动到单独的类,例如EmployeeBeanValidator。而不是使用响应直接收集有关验证的数据,最后抛出异常,例如ValidationException内部的错误列表。然后你必须在@ControllerAdvice注解的类中定义异常处理程序,并在那里修改 HTTP。


推荐阅读