java - 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;
}
}
解决方案
解决方案一
快速解决。您必须在代码中连接消息:
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。
推荐阅读
- vhdl - 如何将 4 位全加器和 4:1 多路复用器加在一起?
- python - Python 3 中的递归
- c++ - 试图移动字符串中的每一位
- python - 如何从 pandas 中的多个数据框创建一个 Csv 文件,其中数据框的名称作为每列的标题?
- c# - Dynamics 365 CRM 集成
- laravel - 如何知道谷歌地图中多边形的某个区域
- c# - WPF - 正在读取 Cookie 文件但在分配的文件夹中看不到它?
- python - Python 3 问题:处理服务器/客户端问题
- c# - 为什么`dynamicMethod.CreateDelegate(typeof(Action)).Method.Invoke(null,new object[0]);`会抛出异常?
- c++ - 为什么或在什么情况下,您会将参数作为 C++ 中的引用(或指针)传递给函数?