Results 1 to 3 of 3

Thread: With ref to use of java.util.date and java.sql.date in Spring based REST API

  1. #1

    Post With ref to use of java.util.date and java.sql.date in Spring based REST API

    Hello,

    First of all, I apologise if my question sounds silly/dumb to you

    I am working to create an API using Spring's REST Support.

    One of the API functions utilises a date value--now, in the service function (which actually inserts data into JDBC data source) i have used java.sql.date.

    I have created a rowmapper for the class that uses a single date value as one of its members-- I have defined this member as a java.sql.date value and hence the getter for the date value returns a java.sql.date value.

    Now, in the API function where I want to insert a new row into table, I am specifying the class member as the Request Body- from which I extract the date value using the getter.

    What I am confused about is, since I am defining the date value as a java.sql.date and not java.util.date, will this cause problems when I actually use this API function from a consumer, which I want to create as a javascript based web app? I mean, should the date value necessarily be declared as java.util.date? Or is using java.sql.date a permissible approach? If this approach is permissible, then what should be the format of the date value as specified by a call from the javascript function?

    Thanks,
    Arvind.

  2. #2
    Join Date
    Jun 2006
    Location
    The Netherlands
    Posts
    13,632

    Default

    In general don't use javax.sql.Date in your APIs... It is more trouble then it is worth next to that you don't want to leak SQL abstractions into your API layer. Use java.util.Date or use something like jodatime to represent your date and time in your API but avoid the use of javax.sql. classes.
    Marten Deinum
    Java Consultant / Pragmatist / Open Source Enthousiast / Author


    Pro Spring MVC: With Web Flow
    Conspect

    Have you read the reference guide.
    Use the [ code ] tags, young padawan

  3. #3

    Default

    Hello Marten,

    OK.... what I am doing now is, using javax.sql.date only in the actual service function--> the controller function (which will be invoked as part of the API by API consumers) asks for date in java.util.date format, and then that date is converted to javax.sql.date (within the controller function)--> after this the service function is called with javax.sql.date value being passed as parameter to the service function.

    Is the above approach correct? Or can you suggest some other/better means?

    Thanks,
    Arvind.

Tags for this Thread

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •