I am using Apache Camel framework of java in which I am fetching the data from oracle db with a column of type timestamp. When I am trying to map this to variable of type java.util.Date, it is throwing the following error mentioned.
Caused by: java.lang.IllegalArgumentException:
Could not find a suitable setter for property lastupdated as there isn't a setter method with same type oracle.sql.TIMESTAMP nor type conversion possible:
No type converter available to convert from type oracle.sql.TIMESTAMP to the required type java.util.Date with value "2022-11-16 19:04:27.067"
Can anyone help me with this?
I tried to use the following datatypes of java-
java.util.Date
oracle.sql.TIMESTAMP
java.sql.Timestamp
But still the error is same.
create a camel convertor and add it camel context annotation can be change for spring + camel or quarkus + camel it is basic camel annotation
#Converter(generateLoader = true)
public class DateConvertor {
#Converter
public static java.sql.Timestamp toTimeStamp(oracle.sql.TIMESTAMP timestamp) {
return timestamp.timestampValue()
}
}
When our application is loading a DBOject/Document instance from our MongoDB, we need to convert all of the UTC dates in it into ZoneDateTime values using the zoneIdName, which is within the DBObject/Document instance. We have a lot of Date fields in the DBObject, so I'd like to avoid having to implement a DBOject level Spring Data MongoDB Converter.
Is it possible to use a DBOject field level Spring Data MongoDB Converter, such as the following, which uses a field in the DBObject (i.e. zoneIdName), to be able to perform the conversion to a ZoneDateTime? If not, I will have to implement an object-level Converter, but it will be tedious...
Thank you for your interest and time.
class ZonedDateTimeReadConverter :
Converter<Date, ZonedDateTime> {
override fun convert(date: Date): ZonedDateTime {
// Need to replace "America/LosAngeles" with
// a value from a field in the object being processed.
return date.toInstant().atZone(ZoneId.of("America/LosAngeles"))
}
}
seems that converter for whole object is only option
I have a SpringBoot JPA Repository that finds a bunch of "things" in a MongoDB instance that has a field called "lastModified" which is an ISODate() as per the below.
{ "name": "a", "lastModified", "ISODate(2018-04-19T19:10:39.356574)" }
When I use a repository function like:
public List<Thing> findByName(String name);
I get a
org.springframework.core.convert.ConversionFailedException: Failed to convert from type [java.lang.String] to type [java.util.Date] for value 'ISODate(2018-04-18T18:38:42.635027)'; nested exception is java.lang.IllegalArgumentException
I have tried
Creating separate setters and constructors that take each java.util.Date and
String and do a converstion with a
SimpleDateFormat("yyyy-MM-dd'T'HH:mm:ss.SSSSSS")
Using
#DateTimeFormat(pattern="yyyy-MM-dd'T'HH:mm:ss.SSSSSS") on the
lastModified parameter on the Entity
Am I missing something about how Mongo does this conversion? I would expect it turns that ISODate into a simple string and passes it into the setLastModified(String name) function but I guess that's not the case?
Turns out it was just because the date was inserted as a string of "ISODate(2018-04-19T19:10:39.356574)" instead as a Date which would be like ISODate("2018-04-17T19:43:00Z")
Once I went and found and fixed the source of this data, it started working properly.
I'm doing a form validation using Bean Validation API with my custom messages:
#NotNull(message = "Please enter your birthday.")
#DateTimeFormat(pattern = "MM/dd/yyyy")
#Past(message = "That's impossible.")
private Date birthday;
Everything works good until I put wrong date format. Then I got such a long and verbose error message on my page:
Failed to convert property value of type [java.lang.String] to required type
[java.util.Date] for property birthday; nested exception is
bla-bla-bla...
As there is no field message in #interface DateTimeFormat from org.springframework.format.annotation.DateTimeFormat I cannot add my message there as I did with other annotations.
How can I specify custom message like "Please use MM/dd/yyyy format" there?
You will have to use MessageSource bean and messages.properties file and there you can add key-value like below.
typeMismatch=Please use MM/dd/yyyy format
If you are using SpringBoot then MessageSource bean will be available by default and you only need to add key-value in messages.properties. For normal Spring application you can use ReloadableResourceBundleMessageSource or ResourceBundleMessageSource.
Property key is resolved in following order.
typeMismatch.[form].[property]
typeMismatch.[form]
typeMismatch
Please refer http://jtuts.com/2014/11/09/validating-dates-in-spring-form-objects/
The correct way to add custom error message is through DefaultMessageCodeResolver
You can use the resolver to bind errors at object + field level(typeMismatch.YourClassName.birthday = custom message.) as mentioned in the javadoc.
Find a detailed usage here
Guys, Well I have done enough research still I can't find the solution to this.
In a nutshell, I'm simply passing url encoded form data to the Controller method and trying to convert it as a domain object which has Date and integers.
#RequestMapping(value = "/savePassport", method = RequestMethod.POST)
public #ResponseBody
AjaxResponse savePassport(#RequestBody StaffPassport passport, HttpServletResponse response) {
// Some operations.
}
The Staff Passport looks like this:
import java.sql.Date;
public class StaffPassport {
private int staffId;
private String passportNumber;
private String placeOfIssue;
private Date issueDate;
private Date expiryDate;
private String spouseName;
private String oldPassportRef;
private String visaInfo;
private String description;
//gets/sets
}
When I invoke the /savePassport, I get unsupported media exception. I guess it's related to casting.
I can't this working right. Of course I can catch individual form data using #RequestParam and manually do the casting but that's not the point of a framework isn't it?
Where am I going wrong? And you are right. I'm a beginner in Spring, but I love it.
Looks like you're using the wrong annotation. #RequestBody is for taking a request that has arbitrary content in its body,such as JSON, some application defined XML, comma separated variables.. whatever. And using a marshaller that you configure in the dispatcher servlet to turn it into objects.
If all you want to do is ask Spring to bind a plain old form post onto the backing object for you, the correct annotation to put on the method parameter is #ModelAttribute.
If you are posting a JSON Object with jQuery and you want Spring to be able to process it with #RequestBody, use JSON.stringify(....) in your data. Here an example:
var data = { "id": 3, "name": "test" }
$.post("processJsonData.html",JSON.stringify(data), function(data){
...
}
);
If you don't use the JSON.stringify() then you will submit the data as form data and Spring will tell you that you have an unsupported media type.
First of all be sure that you have
<mvc:annotation-driven />
in your Spring configuration file. This is mandatory for working with JSOn in SPring MVC.
Second, I recommend you to test wether request to the server has application/json content type. I belive Fiddler2 will help you to do so.
Third, but I'm not sure about it, Try to change Date items in your POJO from SQL type to regular java type.
UPDATE:
just looked at the Form and it seems like your "Accept" HTTP Header should be also application/json. Please test this issue with Fiddler2 as well.
I assume that you are posting JSON and want Spring to convert to StaffPassport. If you are getting an Unsupported media exception, it is because Spring could not figure out an appropriate way to perform the conversion.
For Spring to convert JSON, it needs Jackson -- make sure you have the Jackson jars in your project. If this is a Maven based project you can add the jackson-mapper-asl artifact ID to your pom.xml. This should give you the jackson-mapper and jackson-core jars.
Edit: I should mention that this applies to Spring 3 (I recently ran into this problem). I'm not sure what else is required for previous versions of Spring.
Check into HttpMessageConverter interface and its implementations. You could write your own implementation of it to convert it to the domain model you want. By the time the control gets to your method, you can access it as if your domain model object is passed.
Ok, I think I should refine my answer. I do not have direct experience of using it in a spring-mvc project but spring-integration. I am pretty sure the applicable media type (application/x-url-form-encoded) is already handled and converted to MultiMap by Spring framework; so, retrieve the values from that just like any other map with the key value being your form variable and populate your business model.
HTH.