Best practice for returning multiple values in Java?(在 Java 中返回多个值的最佳实践?)
问题描述
今天,我在登录表单后面添加了额外的安全检查,以减缓暴力攻击.我有多个登录表单,并制作了一个易于调用的函数,该函数执行所有检查,然后返回结果.
Today I've added a extra security check behind my login forms, to slow down brute force attacks. I've got multiple login forms and made a nice easy to call function that does all the checking and then returns the result.
public static ValidateLoginResult validateLogin(HttpServletRequest request, String email, String password) {
问题是结果不是单个值,结果包括:
The problem is the result is not a single value, the result consists of:
boolean ok
String errorMessage
boolean displayCaptcha
为此,我创建了一个新类.这一切都很好.
For this I created a new class. This all works fine.
但我经常有方便的实用函数返回多个值,并且开始发现每次为结果创建一个新类有点烦人.
But I often have handy utility functions that return multiple values and start to find it a bit annoying to create a new class for the result every time.
有没有更好的方法来返回多个值?还是我只是懒惰?:)
Is there a better way of returning multiple values? Or am I just lazy? :)
推荐答案
不,这种结构在Java中是天生不存在的,但是你可以看看JavaTuples 库 可能适合您的需要并提供非常优雅的解决方案.使用 Triplet<Boolean, String, Boolean>
No, this kind of structure doesn't exists nativily in Java, but you can look at JavaTuples library that may suit your need and provide a quite elegant solution. Using a Triplet<Boolean, String, Boolean>
这篇关于在 Java 中返回多个值的最佳实践?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持编程学习网!
本文标题为:在 Java 中返回多个值的最佳实践?
基础教程推荐
- 在java中使用xpath和selenium解析HTML表格数据 2022-01-01
- 在springboot中如何给mybatis加拦截器 2023-04-29
- 将 Windows 证书导入 Java 2022-01-01
- 控制台应用程序中的 Java 键盘输入解析 2022-01-01
- Java ECDSAwithSHA256 签名长度不一致 2022-01-01
- Maven:无效的目标版本:10 2022-01-01
- 如何在相机中应用自定义滤镜 [Surfaceview 预览]. 2022-01-01
- 将 double 转换为 Int,向下舍入 2022-01-01
- doFilter()是在servlet的工作完成之前还是之后执行的? 2022-01-01
- JPA惰性列表上的流 2022-01-01
