[英]Spring Framework Java
public GetChannelAccountRes getAccounts(GetChannelAccountReq request) {
LawAccountEntity account = new LawAccountEntity();
if(request.getTransactionCode().equals("yyyyyyy") && "1".equals(account.getAccountOwnerType())) {
UserParameter userParameter = userParameterRepository.findByGroupKeyAndKey("xxxxxxx","11111111");
}
return remoteChannelAccountInquiryService.getAccounts(request);
}
嗨,我有这个代码块。 如何在请求中添加这个 userParameter 值。 我想在 if 语句userParameter
值和remoteChannelAccountInquiryService.getAccounts(request)
值中一起返回一些东西。
Java 作为一种语言,不允许多种返回类型,即从同一方法返回两个值。 如果您想/必须从同一方法返回多个内容,则需要更改返回类型。 最简单的方法是返回一个 Map,即
public Map<GetChannelAccountRes,UserParameter>getAccounts(GetChannelAccountReq request) {
Map<GetChannelAccountRes,UserParameter> map = new HashMap<>();
if (your condition) {
map.put(account, userParameter);
} else {
map.put(account, null); // or some other default value for userParameter
}
return map;
}
但随后调用者要么必须迭代映射键/值,要么知道键(即帐户)
老实说,使用 Java 的更好解决方案是在返回 UserParameter 的自己的方法中执行 if 语句逻辑,然后将 UserParameter 作为可选参数添加到现有方法中,以便始终只返回一个对象。
public UserParameter getUserParameter(GetChannelAccountReq request) {
UserParameter userParameter = null; // or some other default value
if(request.getTransactionCode().equals("yyyyyyy") &&
userParameter = "1".equals(account.getAccountOwnerType())) {
userParameterRepository.findByGroupKeyAndKey("xxxxxxx","11111111");
}
return userParameter;
}
public GetChannelAccountRes getAccounts(GetChannelAccountReq request, UserParameter... userParameter) {
if (userParameter[0] != null) {
// whatever logic that requires both request and userParameter goes here
}
return getChannelAccountRes
}
如果同一个调用者同时调用 getUserParameter 和 getAccount,那么它将同时拥有两个对象,这就是我认为您说希望两者一起返回时的意图。 getAccounts
的 varargs (...) 参数是一种风格问题,并且是一个数组,这就是我们检查第一个索引是否为 null 的原因。 你可以不使用 var args 做同样的事情。
public GetChannelAccountRes getAccounts(GetChannelAccountReq request) {
return getAccounts(request, null); // or some default value for UserParameter intstead of null
}
public GetChannelAccountRes getAccounts(GetChannelAccountReq request, UserParameter... userParameter) {
if (userParameter != null) {
// whatever logic that requires both request and userParameter goes here
}
return getChannelAccountRes
LawAccountEntity account = new LawAccountEntity();
GetChannelAccountRes response = remoteChannelAccountInquiryService.getAccounts(request);
if (request.getTransactionCode().equals("xxxxx") && "1".equals(account.getAccountOwnerType())) {
UserParameter userParameter = userParameterRepository.findByGroupKeyAndKey("xxxxxxx", "11111111");
GetChannelAccountDTO channelAccountDTO = new GetChannelAccountDTO();
channelAccountDTO.setAccountNumber(userParameter.getValue());
response.getAccountList().add(channelAccountDTO);
}
return response;
}
块引用
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.