繁体   English   中英

React + spring 引导:无法将类型“java.lang.String”的值转换为所需类型“java.util.Date”

[英]React + spring boot : Failed to convert value of type 'java.lang.String' to required type 'java.util.Date'

我有一个代码,我用 React 开发了前端,用 spring 引导开发了后端。

我想通过发送带有开始日期和结束日期的代码来在屏幕上看到返回值。

我使用开始日期和结束日期作为 DateTimeFormat。

当我输入 swagger 文档中的开始日期和结束日期时,我发现我的代码可以正常运行。

但是当我在屏幕上输入这些值时,出现错误。

我输入值的代码屏幕:

export default function Dashboard() {

    const [startDate,   setStartDate] = useState();
    const [stopDate,    setStopDate] = useState();
    const [plantConfig, setPlantConfig] = useState(14);

    
   const[a1rework,setAfterRework] = useState();
        
   function getAllValues(){
       debugger
        let oeeService = new OeeService()
        oeeService.getA1AfterRework(startDate,stopDate,plantConfig).then(result=>
            setAfterRework(result.data.data));
        
        }



    return (
        <div>

        <DatePicker  dateFormat="yyyy-MM-dd"  selected={startDate}   onChange={(date)=>setStartDate(date)} 
            placeholderText="Başlangıç Tarihi Seçiniz" startDate={startDate} endDate={stopDate}  minDate={startDate}    />
        <DatePicker  selected={stopDate} dateFormat="yyyy-MM-dd"  onChange={(date)=>setStopDate(date)} 
             placeholderText="Bitiş Tarihi Seçiniz" startDate={startDate} endDate={stopDate}  minDate={startDate} />
        <Select   options={plantConfigs} onChange={(plantConfig) => setPlantConfig(plantConfig)}/>
        <Button onClick={getAllValues} content='Check'primary></Button>
        </div>
        
    )
}

react中调用服务的代码:

 react - service class : 
    
    import axios from "axios";
    
    export default class OeeService{
        getA1AfterRework(startDate,stopDate,plantConfig){
            return axios.get("http://localhost:8080/api/oeeReports/A1AfterReworkRatio?p_start_date="+startDate+"&p_stop_date="+stopDate+"&V_plant_config_num_id="+plantConfig)
        }

输入值的屏幕:

在此处输入图像描述

java中调用的方法:

@GetMapping("/A1AfterReworkRatio")
DataResult<BigDecimal> a1AfterReworkRatio(@RequestParam int V_plant_config_num_id, @RequestParam @DateTimeFormat(pattern = "yyyy-MM-dd") Date p_start_date ,@RequestParam @DateTimeFormat(pattern = "yyyy-MM-dd") Date p_stop_date) {
    return this.oeeReportService.a1AfterReworkRatio( V_plant_config_num_id , p_start_date,  p_stop_date);
}

当我在 swagger 文档中输入值时:

在此处输入图像描述

当我在 React 中调试代码时,我注意到开始和结束日期以不同的方式发送到后端,如下所示

在此处输入图像描述

java端报错output:

Failed to convert value of type 'java.lang.String' to required type 'java.util.Date';

我如何通过反应以正确的格式将日期参数发送到 java?

可能下面的代码可能会解决您的问题:

请务必调试您在后端接收的格式并使用以下代码更新日期格式

@GetMapping("/A1AfterReworkRatio")
DataResult<BigDecimal> a1AfterReworkRatio(@RequestParam int V_plant_config_num_id, @RequestParam String p_start_date , @RequestParam String p_stop_date) {

r_start_date = new SimpleDateFormat("YYYY-mm-DD").parse(p_start_date);
r_end_date = new SimpleDateFormat("YYYY-mm-DD").parse(p_end_date);

    return this.oeeReportService.a1AfterReworkRatio( V_plant_config_num_id , r_start_date,  r_stop_date);
}

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM