如果为null,Java Spring不会返回DTO对象吗?

时间:2019-12-13 16:56:21

标签: java nullpointerexception microservices dto

我有一个Java实现Override方法,该方法根据输入的参数从2个微服务返回DTO对象结果。

我可以得到过滤后的搜索结果,但是在不满足第二个服务条件的情况下,我收到“ null”,我想获得一个结果,其中不返回任何null对象。如何返回符合所有指定参数的结果?

下面,我提供了使用“ Swagger”接收到的响应的屏幕截图,并剪切了代码,这些代码可以解决所有问题。
我是否需要使用ObjectMapper或类似@JsonInclude(Include.NON_NULL)的东西,但是我可以在服务植入和DTO对象中使用它吗?
任何其他建议或想法反弹将不胜感激。

我在之前的问题Java pass variable into mapped DTO method?

上做了进一步的说明

昂首阔步

enter image description here

代码段

    @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则返回空对象字段。 enter image description here

2 个答案:

答案 0 :(得分:1)

不能直接从页面中删除作为元素的“对象”。您可以做的是,从页面中获取内容,该内容将是一个列表,然后根据您的条件从列表中删除该元素,然后使用页面所需的数据字段创建一个新的页面。

代替

    @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;
}

尝试

   @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));

    List modifiedAppList = searchProfileData.getContent().stream().filter(v -> v !=null).collect(Collectors.toList()); //Will filter your search result and put it into a list

    Page<ProfileCreditDTO> filteredList = new PageImpl<>(modifiedAppList, new PageRequest(0, searchProfileData.getSize()), searchProfileData.getTotalElements()); //Will put your filtered list back into page

    return  filteredList; //Returns filtered result

}

Spring Data Rest - How to remove an element from a Page?得到这个问题的答案只是修改了代码和措辞,因为答案已经在;)

答案 1 :(得分:0)

添加这个

@JsonInclude(JsonInclude.Include.NON_NULL)