简体   繁体   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'

I have a code that I developed the front side with React and the back side with spring boot.我有一个代码,我用 React 开发了前端,用 spring 引导开发了后端。

I want to see the return value on the screen by sending a code with the start date and end date by react.我想通过发送带有开始日期和结束日期的代码来在屏幕上看到返回值。

I am using start date and end date as DateTimeFormat.我使用开始日期和结束日期作为 DateTimeFormat。

When I enter the start and end days from the swagger document, I see that my code works without any problems.当我输入 swagger 文档中的开始日期和结束日期时,我发现我的代码可以正常运行。

but when I enter these values on the screen, I get the error.但是当我在屏幕上输入这些值时,出现错误。

the screen of the code where I entered the values:我输入值的代码屏幕:

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

The code that calls the service in react: 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)
        }

screen where values are entered:输入值的屏幕:

在此处输入图像描述

method called in java: 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);
}

when i enter value in swagger document:当我在 swagger 文档中输入值时:

在此处输入图像描述

When I debug the code in React, I noticed that the start and end dates are sent differently to the backed side as follows当我在 React 中调试代码时,我注意到开始和结束日期以不同的方式发送到后端,如下所示

在此处输入图像描述

output of error on java side: java端报错output:

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

how can i send date paramaters to java in correct format with react?我如何通过反应以正确的格式将日期参数发送到 java?

May be below code might solve your issue:可能下面的代码可能会解决您的问题:

Be sure to debug the format you are receiving at the back end and update the date format with the below code请务必调试您在后端接收的格式并使用以下代码更新日期格式

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

相关问题 无法将 java.lang.String 类型的属性值转换为所需类型 java.util.Date - Failed to convert property value of type java.lang.String to required type java.util.Date 无法将类型&#39;java.lang.String&#39;的属性值转换为必需类型&#39;java.util.Date&#39; - Failed to convert property value of type 'java.lang.String' to required type 'java.util.Date' 无法将“java.lang.String”类型的值转换为所需类型“java.util.Date” - Failed to convert value of type 'java.lang.String' to required type 'java.util.Date' 无法将 java.lang.String 类型的值转换为所需的 java.util.Date 类型 - Failed to convert value of type java.lang.String to required type java.util.Date 如何将“java.lang.String”类型的值转换为所需的“java.util.Date”类型? - How to convert value of type 'java.lang.String' to required type 'java.util.Date'? 使用@DateTimeFormat(pattern =“ HH:mm”)无法将类型[java.lang.String]的属性值转换为所需的类型[java.util.Date] - Failed to convert property value of type [java.lang.String] to required type [java.util.Date] with @DateTimeFormat(pattern = “HH:mm”) 错误:无法将类型java.lang.String的属性值转换为属性dob的必需类型java.util.Date? - Error: Failed to convert property value of type java.lang.String to required type java.util.Date for property dob? 无法将属性“ dateOfBirth”的类型“ java.lang.String”的属性值转换为所需的类型“ java.util.Date”; - Failed to convert property value of type 'java.lang.String' to required type 'java.util.Date' for property 'dateOfBirth'; 发送 GET 请求时无法将“java.lang.String”类型的值转换为所需类型“java.util.Date” - Failed to convert value of type 'java.lang.String' to required type 'java.util.Date' when sending GET request 无法将属性fromDate的java.lang.String类型的属性值转换为所需的java.util.Date类型; - Failed to convert property value of type java.lang.String to required type java.util.Date for property fromDate;
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM