-->

Java DTO Object search mechanism?

2020-07-28 02:21发布

问题:

I have produced a DTO object from 2 microservices. Profile and ProfileCredit.
I am able to successfully retrieve a populated DTO object with relevant data. However I am further curious is it possible to query or do conditional filter on the generated DTO object? and if so what is the approach to achieve just that?

For example using 'swagger' this is what gets returned


Is it possible to filter by profileCredit field which is present in the dto but the data is retrieved within separate microservice?



Any help, suggestions or references to any other posts or pages would be truly helpful.



Controller

    @GetMapping(path="/profile/search/username/{username}", produces =  MediaType.APPLICATION_JSON_UTF8_VALUE)
public ResponseEntity<Page<ProfileCreditDTO>> findProfileByUsername(@PathVariable String username, Pageable pageable) {
    Page<ProfileCreditDTO> results= profileCreditService.findProfileBySelectedParameters(username,pageable);
    if(results== null){
        return new ResponseEntity<>(HttpStatus.NOT_FOUND);
    } else
        return new ResponseEntity<>(results,HttpStatus.OK);
}

Query within profileCreditService

    @Query("SELECT p from PROFILES p where lower(p.username) LIKE :username%")
 Page<ProfileCreditDTO> findProfileBySelectedParameters(String username, Pageable pageable);

ProfileCreditServiceImpl

  public ProfileCreditDTO findProfileCreditByProfileId(final Long profileId){
    log.info("Start of findProfileCreditByProfileId method {}",profileId);

    ProfileCreditDTO rc= new ProfileCreditDTO();
    Profile profile=profileRepository.findOne(profileId);
    if(profile == null){
        return null; }
    CreditDTO creditDto= profileCreditClient.findClientByProfileId(profile.getId());
    if(creditDto == null){
        return null; }

    rc.setProfile(profile);
    rc.setCredit(creditDto);

    return rc;
}

   private ProfileCreditDTO convertProfileToProfileCreditDTO(final Profile theProfile){
        if(theProfile == null)
            return null;
        ProfileCreditDTO theDTO= new ProfileCreditDTO();
        theDTO.setProfile(theProfile);
        CreditDTO theCreditDto= profileCreditClient.findClientByProfileId(theProfile.getId());
        if(theCreditDto != null )
            theDTO.setCredit(theCreditDto);
        return theDTO;
    }

Profile Domain

    @Entity(name = "PROFILES")
@Data @NoArgsConstructor @AllArgsConstructor
@ToString
public class Profile implements Serializable {

@Id
@GeneratedValue(strategy= GenerationType.AUTO)
private Long id;
@Size(min = 2, max = 20)
private String username;
private Integer profileType;
private Integer gender;
private Integer orientation;
private boolean online;
@JsonFormat(pattern="uuuu-MM-dd'T'HH:mm:ss.SSS")
private LocalDateTime created;
@JsonFormat(pattern="uuuu-MM-dd'T'HH:mm:ss.SSS")
private LocalDateTime lastEdited;

Profile Credit DTO

    @Data
@AllArgsConstructor
@NoArgsConstructor
@ToString

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;




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());
}

public void setCredit(final CreditDTO credit){
    this.setCreditId(credit.getId());
    this.setProfileCredit(credit.getProfileCredit());
}

ProfileCreditClient (feign)

     @Component
        @FeignClient(name = "profileCreditService")

    public interface ProfileCreditClient {


        @GetMapping("/api/credit/profile/{profileId}")
         CreditDTO findClientByProfileId(@PathVariable("profileId") Long clientId);

}

Profile Repository Query

    @Query("SELECT p from PROFILES p where lower(p.username) LIKE :username%")
Page<Profile> findByAllParameters(@Param("username") String username, Pageable pageable);