这个错误的意思是: "java.util.stream.ReferencePipeline$3" 无法强制转换为 "org.springframework.data.domain.Page"。

4

我想根据工作ID筛选我的任务。

但运行代码时,出现以下错误:

java.util.stream.ReferencePipeline$3无法转换为org.springframework.data.domain.Page

我该如何解决这个问题?

这是我的组件。

我的服务在这里:

public Page<TaskDataDTO> findAll(String workOrderId,Pageable pageable) {
    log.debug("Request to get all TaskData");
    if(workOrderId!=null)
    return (Page<TaskDataDTO>) taskDataRepository.findAll(pageable).stream().filter(x-> x.getWorkOrderId()==workOrderId).map(taskDataMapper::toDto);
    else
        return null;
}

我的控制器在这里:

 @GetMapping("/task-data/{workOrderId}")
public ResponseEntity<List<TaskDataDTO>> getAllTaskData(@PathVariable String workOrderId,Pageable pageable) {
    if(workOrderId!=null)
    {
        log.debug("REST request to get a page of TaskData");
        Page<TaskDataDTO> page = taskDataService.findAll(workOrderId,pageable);
        HttpHeaders headers = PaginationUtil.generatePaginationHttpHeaders(ServletUriComponentsBuilder.fromCurrentRequest(), page);
        return ResponseEntity.ok().headers(headers).body(page.getContent());
    }
    else {
        return null;
    }
}

任务实体:
   package com.asc.skyalign.domain;

import org.springframework.data.annotation.Id;
import org.springframework.data.mongodb.core.mapping.Document;
import org.springframework.data.mongodb.core.mapping.Field;

import java.io.Serializable;

/**
 * A TaskData.
 */
@Document(collection = "task_data")
public class TaskData implements Serializable {

    private static final long serialVersionUID = 1L;

    @Id
    private String id;

    @Field("roll")
    private String roll;

    @Field("azimuth")
    private String azimuth;

    @Field("tilt")
    private String tilt;

    @Field("sector_location_lat")
    private String sectorLocationLat;

    @Field("amt_imei")
    private String amtImei;

    @Field("wakeu_up_interval")
    private String wakeuUpInterval;

    @Field("sector_id")
    private String sectorID;

    @Field("sector_location_long")
    private String sectorLocationLong;

    @Field("task_name")
    private String taskName;

    @Field("task_description")
    private String taskDescription;

    @Field("work_order_id")
    private String workOrderId;

    private WorkOrder workOrder;

    public void setWorkOrder(WorkOrder workOrder) {
        this.workOrder = workOrder;
    }

    public WorkOrder getWorkOrder() {
        return workOrder;
    }

    // jhipster-needle-entity-add-field - JHipster will add fields here
    public String getId() {
        return id;
    }

    public void setId(String id) {
        this.id = id;
    }

    public String getRoll() {
        return roll;
    }

    public TaskData roll(String roll) {
        this.roll = roll;
        return this;
    }

    public void setRoll(String roll) {
        this.roll = roll;
    }

    public String getAzimuth() {
        return azimuth;
    }

    public TaskData azimuth(String azimuth) {
        this.azimuth = azimuth;
        return this;
    }

    public void setAzimuth(String azimuth) {
        this.azimuth = azimuth;
    }

    public String getTilt() {
        return tilt;
    }

    public TaskData tilt(String tilt) {
        this.tilt = tilt;
        return this;
    }

    public void setTilt(String tilt) {
        this.tilt = tilt;
    }

    public String getSectorLocationLat() {
        return sectorLocationLat;
    }

    public TaskData sectorLocationLat(String sectorLocationLat) {
        this.sectorLocationLat = sectorLocationLat;
        return this;
    }

    public void setSectorLocationLat(String sectorLocationLat) {
        this.sectorLocationLat = sectorLocationLat;
    }

    public String getAmtImei() {
        return amtImei;
    }

    public TaskData amtImei(String amtImei) {
        this.amtImei = amtImei;
        return this;
    }

    public void setAmtImei(String amtImei) {
        this.amtImei = amtImei;
    }

    public String getWakeuUpInterval() {
        return wakeuUpInterval;
    }

    public TaskData wakeuUpInterval(String wakeuUpInterval) {
        this.wakeuUpInterval = wakeuUpInterval;
        return this;
    }

    public void setWakeuUpInterval(String wakeuUpInterval) {
        this.wakeuUpInterval = wakeuUpInterval;
    }

    public String getSectorID() {
        return sectorID;
    }

    public TaskData sectorID(String sectorID) {
        this.sectorID = sectorID;
        return this;
    }

    public void setSectorID(String sectorID) {
        this.sectorID = sectorID;
    }

    public String getSectorLocationLong() {
        return sectorLocationLong;
    }

    public TaskData sectorLocationLong(String sectorLocationLong) {
        this.sectorLocationLong = sectorLocationLong;
        return this;
    }

    public void setSectorLocationLong(String sectorLocationLong) {
        this.sectorLocationLong = sectorLocationLong;
    }

    public String getTaskName() {
        return taskName;
    }

    public TaskData taskName(String taskName) {
        this.taskName = taskName;
        return this;
    }

    public void setTaskName(String taskName) {
        this.taskName = taskName;
    }

    public String getTaskDescription() {
        return taskDescription;
    }

    public TaskData taskDescription(String taskDescription) {
        this.taskDescription = taskDescription;
        return this;
    }

    public void setTaskDescription(String taskDescription) {
        this.taskDescription = taskDescription;
    }

    public String getWorkOrderId() {
        return workOrderId;
    }

    public TaskData workOrderId(String workOrderId) {
        this.workOrderId = workOrderId;
        return this;
    }

    public void setWorkOrderId(String workOrderId) {
        this.workOrderId = workOrderId;
    }
    // jhipster-needle-entity-add-getters-setters - JHipster will add getters and setters here

    @Override
    public boolean equals(Object o) {
        if (this == o) {
            return true;
        }
        if (!(o instanceof TaskData)) {
            return false;
        }
        return id != null && id.equals(((TaskData) o).id);
    }

    @Override
    public int hashCode() {
        return 31;
    }

    // prettier-ignore
    @Override
    public String toString() {
        return "TaskData{" +
            "id=" + getId() +
            ", roll='" + getRoll() + "'" +
            ", azimuth='" + getAzimuth() + "'" +
            ", tilt='" + getTilt() + "'" +
            ", sectorLocationLat='" + getSectorLocationLat() + "'" +
            ", amtImei='" + getAmtImei() + "'" +
            ", wakeuUpInterval='" + getWakeuUpInterval() + "'" +
            ", sectorID='" + getSectorID() + "'" +
            ", sectorLocationLong='" + getSectorLocationLong() + "'" +
            ", taskName='" + getTaskName() + "'" +
            ", taskDescription='" + getTaskDescription() + "'" +
            ", workOrderId='" + getWorkOrderId() + "'" +
            "}";
    }
}

而且这里有TaskDto:

    package com.asc.skyalign.service.dto;

import java.io.Serializable;

/**
 * A DTO for the {@link com.asc.skyalign.domain.TaskData} entity.
 */
public class TaskDataDTO implements Serializable {

    private String id;

    private String roll;

    private String azimuth;

    private String tilt;

    private String sectorLocationLat;

    private String amtImei;

    private String wakeuUpInterval;

    private String sectorID;

    private String sectorLocationLong;

    private String taskName;

    private String taskDescription;

    private String workOrderId;

    private WorkOrderDTO workOrderDTO;

    public void setWorkOrderDTO(WorkOrderDTO workOrderDTO) {
        this.workOrderDTO = workOrderDTO;
    }

    public WorkOrderDTO getWorkOrderDTO() {
        return workOrderDTO;
    }

    public String getId() {
        return id;
    }

    public void setId(String id) {
        this.id = id;
    }

    public String getRoll() {
        return roll;
    }

    public void setRoll(String roll) {
        this.roll = roll;
    }

    public String getAzimuth() {
        return azimuth;
    }

    public void setAzimuth(String azimuth) {
        this.azimuth = azimuth;
    }

    public String getTilt() {
        return tilt;
    }

    public void setTilt(String tilt) {
        this.tilt = tilt;
    }

    public String getSectorLocationLat() {
        return sectorLocationLat;
    }

    public void setSectorLocationLat(String sectorLocationLat) {
        this.sectorLocationLat = sectorLocationLat;
    }

    public String getAmtImei() {
        return amtImei;
    }

    public void setAmtImei(String amtImei) {
        this.amtImei = amtImei;
    }

    public String getWakeuUpInterval() {
        return wakeuUpInterval;
    }

    public void setWakeuUpInterval(String wakeuUpInterval) {
        this.wakeuUpInterval = wakeuUpInterval;
    }

    public String getSectorID() {
        return sectorID;
    }

    public void setSectorID(String sectorID) {
        this.sectorID = sectorID;
    }

    public String getSectorLocationLong() {
        return sectorLocationLong;
    }

    public void setSectorLocationLong(String sectorLocationLong) {
        this.sectorLocationLong = sectorLocationLong;
    }

    public String getTaskName() {
        return taskName;
    }

    public void setTaskName(String taskName) {
        this.taskName = taskName;
    }

    public String getTaskDescription() {
        return taskDescription;
    }

    public void setTaskDescription(String taskDescription) {
        this.taskDescription = taskDescription;
    }

    public String getWorkOrderId() {
        return workOrderId;
    }

    public void setWorkOrderId(String workOrderId) {
        this.workOrderId = workOrderId;
    }

    @Override
    public boolean equals(Object o) {
        if (this == o) {
            return true;
        }
        if (!(o instanceof TaskDataDTO)) {
            return false;
        }

        return id != null && id.equals(((TaskDataDTO) o).id);
    }

    @Override
    public int hashCode() {
        return 31;
    }

    // prettier-ignore
    @Override
    public String toString() {
        return "TaskDataDTO{" +
            "id=" + getId() +
            ", roll='" + getRoll() + "'" +
            ", azimuth='" + getAzimuth() + "'" +
            ", tilt='" + getTilt() + "'" +
            ", sectorLocationLat='" + getSectorLocationLat() + "'" +
            ", amtImei='" + getAmtImei() + "'" +
            ", wakeuUpInterval='" + getWakeuUpInterval() + "'" +
            ", sectorID='" + getSectorID() + "'" +
            ", sectorLocationLong='" + getSectorLocationLong() + "'" +
            ", taskName='" + getTaskName() + "'" +
            ", taskDescription='" + getTaskDescription() + "'" +
            ", workOrderId='" + getWorkOrderId() + "'" +
            "}";
    }
}

谢谢。

1个回答

2

return (Page<TaskDataDTO>) taskDataRepository.findAll(pageable).stream().filter(x-> x.getWorkOrderId()==workOrderId).map(taskDataMapper::toDto).collect(Collectors.toList());

上述代码中漏掉了一个终止操作,需要添加类似.collect(Collectors.toList())这样的内容来消费流。

这里是java.util.stream的包描述


这个对我很有帮助。 - horoyoi o

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接