【发布时间】:2019-12-13 16:56:21
【问题描述】:
我有一个 Java 实现 Override 方法,它根据输入的参数从 2 个微服务返回 DTO 对象结果。
我能够获得过滤的搜索结果,但是在不满足第二个服务条件的情况下,我收到“null”,我希望获得不返回任何 null 对象的结果。我如何能够返回满足所有指定参数的结果?
下面我提供了我使用“Swagger”收到的响应的屏幕截图,并且代码截断了所有的魔法。
我是否需要使用 ObjectMapper 或 @JsonInclude(Include.NON_NULL) 之类的东西,但是我可以在服务植入和 DTO 对象中使用它吗?
任何进一步的建议或想法反弹将不胜感激。
我在之前的问题Java pass variable into mapped DTO method?
大摇大摆的回应
代码片段
@Override
public Page<ProfileCreditDTO> findProfileBySelectedParameters(String username, Integer gender, Integer profileType, Integer orientation, Boolean online, Double profileCredit, Integer creditMode, Double creditTotal, Pageable pageable) {
Page<Profile> searchData= profileRepository.findByAllParameters(username, gender, profileType, orientation, online, pageable);
Page<ProfileCreditDTO> searchProfileData=null;
if(searchData != null)
searchProfileData = searchData.map(x -> this.convertProfileToProfileCreditDTO(x, profileCredit, creditMode, creditTotal));
return searchProfileData;
}
public ProfileCreditDTO convertProfileToProfileCreditDTO(final Profile theProfile, Double profileCredit, Integer creditMode, Double creditTotal) {
if(theProfile == null)
return null;
ProfileCreditDTO theDTO= new ProfileCreditDTO();
theDTO.setProfile(theProfile);
CreditDTO theCreditDto = profileCreditClient.findClientByProfileId(theProfile.getId(), profileCredit, creditMode, creditTotal);
log.error(String.valueOf(theCreditDto));
if (theCreditDto != null)
theDTO.setCredit(theCreditDto);
else {
return null;
}
return theDTO;
}
更新
我尝试将@JsonInclude(Include.NON_NULL) 添加到 ProfileCredit DTO 中仍然没有,也将相同的内容添加到 CreditDTO 中。
ProfileCredit DTO
@Data
@AllArgsConstructor
@NoArgsConstructor
@ToString
@JsonInclude(JsonInclude.Include.NON_NULL)
public class ProfileCreditDTO {
// profile fields
private Long profileId;
@Size(min = 2, max = 50)
private String username;
private Integer gender;
private Integer profileType;
private Integer orientation;
private boolean online;
// Credit fields
private Long creditId;
@Column(unique = true)
private Double profileCredit; //X
private Integer creditMode;
private Integer creditCheck;
private Double creditTotal;
/**
* set the profile derrived data
* @param profile
*/
public void setProfile(final Profile profile){
this.setProfileId(profile.getId());
this.setUsername(profile.getUsername());
this.setGender(profile.getGender());
this.setProfileType(profile.getProfileType());
this.setOrientation(profile.getOrientation());
this.setOnline(profile.isOnline());
}
/**
* Set the credit aspect
* @param credit
*/
public void setCredit(final CreditDTO credit){
this.setCreditId(credit.getId());
this.setProfileId(credit.getProfileId());
this.setProfileCredit(credit.getProfileCredit());
this.setCreditMode(credit.getCreditMode());
this.setCreditTotal(credit.getCreditTotal());
}
当我没有在实现中指定return null; 时,响应看起来像这样。正如您所看到的,所有满足条件 1 但不满足条件 2 的对象都返回了空对象字段。
【问题讨论】:
-
如您所说,只需在 DTO 级别添加 @JsonInclude(Include.NON_NULL) 即可。
-
@dassum 添加
@JsonInclude(JsonInclude.Include.NON_NULL)什么都不做 -
你能分享你的 DTO 吗?你把@JsonInclude(Include.NON_NULL) 放在哪里了?
-
@Lemmy 我已经更新了问题
-
“creditId”及之后的字段来自不同的微服务。在我的控制器中(目前尚未显示)包含可选字段,您可以在其中提供将给出过滤结果的参数。因此,如果您输入参数 creditMode(profileCredit) 0 和 username(profile) 您应该根据您的要求返回数据@ChristopherSchneider
标签: java nullpointerexception microservices dto