简体   繁体   中英

Pass java.util.Date using as @Param in query method

I have used java.util.Date attribute in query method in spring boot repository. But it giving me " Failed to convert from type java.lang.String to type @org.springframework.data.repository.query.Param java.util.Date for value '2014-11-27" error. I have mentioned code segments the Entity class and Repository class below. What would be the problem here ?

Entity Class

@Column(name="date")
@Temporal(TemporalType.DATE)
private Date date;

Repository

public interface WeatherRepository extends CrudRepository<Weather, String> {
List<Weather> findAllByDate(@Param("date") Date date);
Weather findWeatherByCity(@Param("city") String city);

}

Try sending the date in this format - 11/27/2014 (MM/dd/yyyy) . This should be able to parse it from the request URI in this format. Please try and let me know.

You can use the @org.springframework.format.annotation.DateTimeFormat annotation on the date parameter to specify the format explicitly. In the annotation, you can use either the

  • pattern to specify an arbitrary pattern (following the SimpleDateFormat patterns)
  • iso to use one of the standard ISO formats
  • style for a shorthand two-letter code for date and time formats ( S hort/ M edium/ L ong/ F ull/ - )

Choose only one of the configuration options.

Then you can use any of Date , Calendar , Long , Joda-Time types or (Spring 4 + JDK 8) java.time types as your parameter type.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM