Failed to convert value of type 'java.lang.String' to required type 'java.util.Date' on swagger - spring

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.
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 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:
#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:
#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;
}
}
swagger :
error :
{
"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
How can solve this problem?

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.

Related

Unable to convert pathvariable to object

I want to have an object as a path variable but I get the below exception when testing. How can I fix this
#Validated
#RestController
#RequestMapping("/test/api")
public class MyRestController {
#GetMapping("/data/{id}")
public Data getData(#PathVariable #Valid IdData id) {
return new Data();
}
}
#Data
public class IdData {
private Integer id;
public IdData(Integer id) {
this.id = id;
}
}
Exception:
org.springframework.web.method.annotation.MethodArgumentConversionNotSupportedException:
Failed to convert value of type 'java.lang.String' to required type
'com.test.IdData'; nested exception is
java.lang.IllegalStateException: Cannot convert value of type
'java.lang.String' to required type 'com.test.IdData': no matching
editors or conversion strategy found
From "/data/{id}" you will get an id which is an integer but the method parameter is trying to get IdData value which is incompatible.
#Validated
#RestController
#RequestMapping("/test/api")
public class MyRestController {
#GetMapping("/data/{id}")
public Data getData(#Valid #PathVariable int id) {
return new Data();
}
}
Output:-
{
"repository": {
"metricName": "spring.data.repository.invocations",
"autotime": {
"enabled": true,
"percentilesHistogram": false,
"percentiles": null
}
}
}
you could change your #PathVariable Data Type from IdData to an Integer. Just add some logic to get the IdData by the id in path, which can be done by using JPA's findById() method. It might also be easier to pass in an integer in the path rather than an entire object.

Extra field coming in get response in spring boot application

In my spring boot application I have booking controller which has corresponding service,repository and controller.
My booking Model looks like this :
#JsonFormat(pattern = "yyyy-MM-dd")
#Column(name = "datetime")// corresponds to value
private Date date;
public Date getDatetime() {
return this.date;
}
public void setDateTime(Date dateTime) {
this.date = dateTime;
}
Controller
#GetMapping("api/booking_details/{userEmail}")
#ResponseBody
public ResponseEntity<List<Booking>> getDetails(
#PathVariable #Email String userEmail) {
return new ResponseEntity<>(bookService.findByEmail(userEmail), HttpStatus.OK);
}
My corresponding get request is
api/booking_details/
the response I am getting is :
{
"datetime": "2021-09-12T16:01:04.000+00:00",
"date": "2021-09-12"
}
Can any one let me know what could be reason for having two values in response?
The problem is getter and setter method are not identified as date parameter's getter and setter methods. Two things you can do,
Rename getter and setter method name as follows,
public Date getDate() {
return this.date;
}
public void setDate(Date date) {
this.date = date;
}
Or annotate getter and setter method using #JsonProperty("date") annotation. Added this annotation for only getter method is also sufficient.
#JsonProperty("date")
public Date getDatetime() {
return this.date;
}
#JsonProperty("date")
public void setDateTime(Date dateTime) {
this.date = dateTime;
}
It looks like both the "date" property and the return value of the getDateTime() method are being serialized.
I wouldn't normally expect the "date" property, which is private, to get serialzied, but perhaps it is because you have the #JsonFormat annotation on it?
The getDateTIme() return value is serialized because it's name indicates that it's a Java Bean property.
If what you are looking for is just the formatted date, I'd try moving the #JsonFormat annotation to the method.

Timestamp converter not working in Spring Data Rest with Spanner

I'm trying to convert the input timestamp which will be in the string format to cloud timestamp with the help of a Spring Data Rest custom converter which is not working. Need an help on the same in understanding why custom converters are not invoked.
Input: http://localhost:8080/apipromocentral/promotions
RequestBody : {"startDateTime": "2019-11-07 15:53:00"}
POJO:
#ApiModel
#Data
#AllArgsConstructor
#Table(name = "PROMOTIONS")
public class Promotion {
/**
* promotion id
*/
#ApiModelProperty(notes = "Id of the Promotion", required = true)
#PrimaryKey
#Column(name = "PROMO_ID")
private String promotionId;
#ApiModelProperty(notes = "Start Date Time of a promotion", allowableValues="yyyy-MM-dd HH:mm:ss", required = true)
#JsonFormat(shape = JsonFormat.Shape.STRING, pattern = "yyyy-MM-dd HH:mm:ss")
#Column(name = "START_DATE_TIME")
private Timestamp startDateTime; //java.sql.timestamp;
}
converter code
#Component
public class TimestampWriteConverter implements Converter<java.sql.Timestamp, Timestamp> {
#Override
public Timestamp convert(java.sql.Timestamp sqlTimestamp) {
//Return com.google.cloud.Timestamp;
return Timestamp.of(sqlTimestamp);
}
}
exception
"message": "FAILED_PRECONDITION: com.google.api.gax.rpc.FailedPreconditionException: io.grpc.StatusRuntimeException: FAILED_PRECONDITION: Invalid value for column START_DATE_TIME in table PROMOTIONS: Expected TIMESTAMP.",
"trace": "com.google.cloud.spanner.SpannerException: FAILED_PRECONDITION: com.google.api.gax.rpc.FailedPreconditionException: io.grpc.StatusRuntimeException: FAILED_PRECONDITION: Invalid value for column START_DATE_TIME in table PROMOTIONS: Expected TIMESTAMP.\r\n\tat com.google.cloud.spanner.SpannerExceptionFactory.newSpannerExceptionPreformatted(SpannerExceptionFactory.java:156)\r\n\tat com.google.cloud.spanner.SpannerExceptionFactory.newSpannerException(SpannerExceptionFactory.java:45)\r\n\tat com.google.cloud.spanner.SpannerExceptionFactory.newSpannerException(SpannerExceptionFactory.java:112)\r\n\tat
Looking at the documentation, looks like you need pass the TimestampWriteConverter converter to ConverterAwareMappingSpannerEntityProcessor.
#Configuration
public class ConverterConfiguration {
#Bean
public SpannerEntityProcessor spannerEntityProcessor(SpannerMappingContext spannerMappingContext) {
return new ConverterAwareMappingSpannerEntityProcessor(spannerMappingContext,
Arrays.asList(new TimestampWriteConverter()),
Arrays.asList());
}
}

Spring Data Rest and Oracle Between 2 dates query giving nothing

Spring Data rest is not able to fetch the data in between 2 dates from data base table.
Collection<XXXX> findByCreatedOnBetween(LocalDate fromDate, LocalDate todayDate);
From Bean
private LocalDate createdOn;
Collection<XXXX> findByCreatedOnBetween(LocalDate fromDate, LocalDate todayDate);
private LocalDate createdOn;
I want the data in between 2 dates:
SELECT
*
FROM
testing testing
WHERE
testing.created_on BETWEEN ? AND ? ;
I believe Spring Data Rest accepts only ISO 8601 date format by default (like 2018-10-22).
If you want to accept the date in different format you need to add a converter.
#Configuration
public class RepositoryRestConfig extends RepositoryRestConfigurerAdapter {
#Autowired
CustomDateConverter customDateConverter;
#Override
public void configureConversionService(ConfigurableConversionService conversionService) {
conversionService.addConverter(customDateConverter);
super.configureConversionService(conversionService);
}
}
#Component
public class CustomDateConverter implements Converter<String, LocalDate > {
#Override
public LocalDate convert(String source) {
return LocalDate.from(DateTimeFormatter.ofPattern("dd-MMM-yy").parse(source));
}
}

Validate input before Jackson in Spring Boot

I've built a REST endpoint using Spring Boot. JSON is posted to the endpoint. Jackson converts the JSON giving me an object.
The JSON look like this:
{
"parameterDateUnadjusted": "2017-01-01",
"parameterDateAdjusted": "2017-01-02"
}
Jackson converts the JSON to an object based on this class:
public class ParameterDate {
#NotNull(message = "Parameter Date Unadjusted can not be blank or null")
#DateTimeFormat(pattern = "yyyy-MM-dd")
private Date parameterDateUnadjusted;
#NotNull(message = "Parameter Date Adjusted can not be blank or null")
#DateTimeFormat(pattern = "yyyy-MM-dd")
private Date parameterDateAdjusted;
private Date parameterDateAdded;
private Date parameterDateChanged;
}
This all works fine. The issue I'm having is that I would like to validate the data before Jackson converts the data. For instance if I post
{
"parameterDateUnadjusted": "2017-01-01",
"parameterDateAdjusted": "2017-01-40"
}
Where parameterDateAdjusted is not a valid date (there is no month with 40 days in it). Jackson converts this to 2017-02-09. One way of getting around this is to have a class that is only strings let's call it ParameterDateInput. Validate each filed with Hibernate Validator in the parameterDateInput object and then copy the parameterDateInput object to parameterDate where each field has the correct type (dates are of type Date and not of type String). This to me doesn't look like a very elegant solution. Is there some other way I can solve this? How is data generally validated in Spring Boot when posted as JSON? I like to be able to send back a message to the user/client what is wrong with the data that is being posted.
How about a custom JSON deserializer where you can write down the logic you want:
#RestController
public class JacksonCustomDesRestEndpoint {
#RequestMapping(value = "/yourEndPoint", method = RequestMethod.POST, consumes = MediaType.APPLICATION_JSON_VALUE, produces = MediaType.APPLICATION_JSON_VALUE)
#ResponseBody
public Object createRole(#RequestBody ParameterDate paramDate) {
return paramDate;
}
}
#JsonDeserialize(using = RoleDeserializer.class)
public class ParameterDate {
// ......
}
public class RoleDeserializer extends JsonDeserializer<ParameterDate> {
#Override
public ParameterDate deserialize(JsonParser jp, DeserializationContext ctxt) throws IOException, JsonProcessingException {
ObjectCodec oc = jsonParser.getCodec();
JsonNode node = oc.readTree(jsonParser);
String parameterDateUnadjusted = node.get("parameterDateUnadjusted").getTextValue();
//Do what you want with the date and set it to object from type ParameterDate and return the object at the end.
//Don't forget to fill all the properties to this object because you do not want to lose data that came from the request.
return something;
}
}
There is a way to check the dates. setLenient() method
public static boolean isValidDate(String inDate, String format) {
SimpleDateFormat dateFormat = new SimpleDateFormat(format);
dateFormat.setLenient(false);
try {
dateFormat.parse(inDate.trim());
} catch (ParseException pe) {
return false;
}
return true;
}
Just define own annotation to validate the value
#Target({ FIELD, METHOD, PARAMETER, ANNOTATION_TYPE })
#Retention(RUNTIME)
#Constraint(validatedBy = MyDateFormatCheckValidator.class)
#Documented
public #interface MyDateFormatCheck {
String pattern();
...
and the validator class
public class MyDateFormatCheckValidator implements ConstraintValidator<MyDateFormatCheck, String> {
private MyDateFormatCheck check;
#Override
public void initialize(MyDateFormatCheck constraintAnnotation) {
this.check= constraintAnnotation;
}
#Override
public boolean isValid(String object, ConstraintValidatorContext constraintContext) {
if ( object == null ) {
return true;
}
return isValidDate(object, check.pattern());
}
}

Resources