Java DTO对象搜索机制?

问题描述 投票:2回答:1

我已经从2个微服务产生了一个DTO对象。个人资料和ProfileCredit。我能够成功检索包含相关数据的DTO对象。但是我进一步好奇是否可以对生成的DTO对象进行查询或进行条件过滤?如果是的话,实现这一目标的方法是什么?例如,使用“ swagger”,这就是返回的内容enter image description here

是否有可能通过dto中存在的profileCredit字段进行过滤,但在单独的微服务中检索到数据?] >>

任何帮助,建议或对任何其他帖子或页面的引用都将真正有帮助。

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

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

个人资料域

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

个人资料信用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(伪装)

     @Component
        @FeignClient(name = "profileCreditService")

    public interface ProfileCreditClient {


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

}

配置文件存储库查询

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

我已经从2个微服务产生了一个DTO对象。个人资料和ProfileCredit。我能够成功检索包含相关数据的DTO对象。但是我进一步好奇是否有可能...

java spring hibernate microservices dto
1个回答
0
投票

您可以使用Spring的org.springframework.beans.BeanUtils将属性从实体复制到DTO或任何其他库,例如Mapstruct

© www.soinside.com 2019 - 2024. All rights reserved.