我正在Spring Boot上的一个项目上工作,需要调用第三方API并从中获取数据。该应用程序涉及货币兑换。在执行货币转换时,出现错误。
Field error in object 'currencyConversion' on field 'value': rejected value [{10.0}]; codes [typeMismatch.currencyConversion.value,typeMismatch.value,typeMismatch.java.lang.Double,typeMismatch]; arguments [org.springframework.context.support.DefaultMessageSourceResolvable: codes [currencyConversion.value,value]; arguments []; default message [value]]; default message [Failed to convert property value of type 'java.lang.String' to required type 'java.lang.Double' for property 'value'; nested exception is java.lang.NumberFormatException: For input string: "{10.0}"]]
CurrencyConversion.java
public class CurrencyConversion {
private String to;
private String from;
private Double value;
//Getters and Setters
服务等级
public Optional<Double> convert(CurrencyConversion currencyConversion) {
Optional<Currency> toOptional =
this.currencyRepository.findById(currencyConversion.getTo().toUpperCase());
Optional<Currency> fromOptional =
this.currencyRepository.findById(currencyConversion.getFrom().toUpperCase());
if(toOptional.isPresent() && fromOptional.isPresent()) {
if(currencyConversion.getValue() < 0) {
return Optional.empty();
}
Currency to = toOptional.get();
Currency from = fromOptional.get();
Double toValue = to.getValues();
Double fromValue = from.getValues();
Double result = toValue * currencyConversion.getValue() / fromValue;
return Optional.of(result);
}
return Optional.empty();
}
控制器类
@RequestMapping(value = "/convert/from{from}/to{to}/value{value}", produces = { "application/json" }, method = RequestMethod.POST)
public ResponseEntity<Double> convertCurrencies(CurrencyConversion currencyConversion){
Optional<Double> resultOptional = this.currencyService.convert(currencyConversion);
if(resultOptional.isPresent()) {
return new ResponseEntity<>(resultOptional.get(), HttpStatus.OK);
}
return new ResponseEntity<>(HttpStatus.BAD_REQUEST);
}
我缺少什么,导致发生错误?