无法在 swagger 上将“java.lang.String"类型的值转换为所需类型“java.util.Date" [英] Failed to convert value of type 'java.lang.String' to required type 'java.util.Date' on swagger
问题描述
我想使用spring boot api访问数据库中的一个函数并获取它返回的值.当我们在swagger中输入参数时,它在日期部分出现错误.
I want to reach a function in the database with the spring boot api and get the value it returns. When we enter the parameters in swagger, it gives an error in the date part.
在oracle中调用函数的日期参数为01-apr-2021时,没有报错,但是从spring开始就不能这样发送了.
When I call the date parameters to the function in oracle as 01-apr-2021, there is no error, but I cannot send it this way from spring.
Oracle 函数代码:
Oracle funtion code :
CREATE OR REPLACE PACKAGE BODY MET.Z_PKG_OEE_NEW
FUNCTION Z_OEE_A1AfterReworkRatio(V_plant_config_num_id IN number, p_start_date in date, p_stop_date in date) RETURN NUMBER IS
v_result NUMBER;
p_cur001 SYS_REFCURSOR;
BEGIN
Z_OEE_A1AfterReworkRatio_Detail(V_plant_config_num_id,p_start_date,p_stop_date,p_cur001, v_result);
RETURN round(v_result,4);
END Z_OEE_A1AfterReworkRatio;
end;
ooeController:
ooeController:
@RestController
@RequestMapping("/api/oeeReports")
@CrossOrigin
public class OeeController {
private OeeReportService oeeReportService;
@Autowired
public OeeController(OeeReportService oeeReportService) {
this.oeeReportService=oeeReportService;
}
@GetMapping("A1AfterReworkRatio")
BigDecimal A1AfterReworkRatio(@RequestParam int V_plant_config_num_id, @RequestParam @DateTimeFormat(iso = DateTimeFormat.ISO.DATE) Date p_start_date ,@RequestParam @DateTimeFormat(iso = DateTimeFormat.ISO.DATE) Date p_stop_date) {
return this.oeeReportService.A1AfterReworkRatio( V_plant_config_num_id , p_start_date, p_stop_date);
}
}
oeeservice:
oeeservice:
@Service
public class OeeReportManager implements OeeReportService {
private OeeDao oeeDao;
@Autowired
public OeeReportManager(OeeDao oeeDao) {
super();
this.oeeDao=oeeDao;
}
@Override
public BigDecimal A1AfterReworkRatio(int V_plant_config_num_id, Date p_start_date, Date p_stop_date) {
// TODO Auto-generated method stub
return this.oeeDao.A1AfterReworkRatio(V_plant_config_num_id, p_start_date, p_stop_date);
}
}
oeedao :
@Repository
public class OeeDao {
@Autowired
private EntityManager entitymanager;
public BigDecimal A1AfterReworkRatio(int V_plant_config_num_id,Date p_start_date,Date p_stop_date) {
BigDecimal commentCount = (BigDecimal) entitymanager
.createNativeQuery(
"SELECT Z_OEE_A1AfterReworkRatio(:V_plant_config_num_id:p_start_date:p_stop_date) FROM DUAL"
)
.setParameter("V_plant_config_num_id", V_plant_config_num_id).setParameter("p_start_date", p_start_date).setParameter("p_stop_date", p_stop_date)
.getSingleResult();
return commentCount;
}
}
招摇:
错误:
{
"timestamp": "2021-08-26T07:00:23.487+00:00",
"status": 500,
"error": "Internal Server Error",
"trace": "org.springframework.dao.InvalidDataAccessApiUsageException: Could not locate named parameter [V_plant_config_num_id], expecting one of [V_plant_config_num_id:p_start_date:p_stop_date]; nested exception is java.lang.IllegalArgumentException: Could not locate named parameter [V_plant_config_num_id], expecting one of [V_plant_config_num_id:p_start_date:p_stop_date]\r\n\tat
如何解决这个问题?
推荐答案
根据 https://www.baeldung.com/spring-date-parameters您可以在 OeeController
中注释您的日期参数(来自:spring 启动应用程序 {while 创建 beans 错误}) 与 @DateTimeFormat(iso = DateTimeFormat.ISO.DATE)
:
According to https://www.baeldung.com/spring-date-parameters
you can annotate your date parameters in OeeController
(from: spring boot application {while creating beans error}) with @DateTimeFormat(iso = DateTimeFormat.ISO.DATE)
:
@GetMapping("A1AfterReworkRatio")
int A1AfterReworkRatio(@RequestParam int V_plant_config_num_id,
@RequestParam @DateTimeFormat(iso = DateTimeFormat.ISO.DATE) Date p_start_date,
@RequestParam @DateTimeFormat(iso = DateTimeFormat.ISO.DATE) Date p_stop_date) {
return this.oeeReportService.A1AfterReworkRatio( V_plant_config_num_id , p_start_date, p_stop_date);
}
如果您通读以上文章,还描述了实现它的其他方法.
The above article describes also other methods of achieving it if you read through it.
这篇关于无法在 swagger 上将“java.lang.String"类型的值转换为所需类型“java.util.Date"的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!