'I'm using Java Spring framework to map the date data from my data and my pathvariable won't get me access to any data

I'm using the Java Spring framework to map data from a portion of my data and my @pathvariable won't get me access to any data. all I'm getting in the console is *

Trying 127.0.0.1:4001...
* TCP_NODELAY set
* Connected to localhost (127.0.0.1) port 4001 (#0)
> GET /traveladventures/bydate/2017 HTTP/1.1
> Host: localhost:4001
> User-Agent: curl/7.68.0
> Accept: */*
> 
* Mark bundle as not supporting multiuse
< HTTP/1.1 200 
< Content-Type: application/json
< Transfer-Encoding: chunked
< Date: Tue, 22 Mar 2022 17:19:19 GMT
< 
* Connection #0 to host localhost left intact

my current code looks like such

@GetMapping("/bydate/{date}")
  public List<Adventure> getByDate(@PathVariable("date") String date){
    return this.adventureRepository.findByDate(date);
  }

I used this code to access my database

$ curl -v http://localhost:4001/traveladventures/bydate/2017

my interface

public interface AdventureRepository extends CrudRepository<Adventure, Integer> {
  public List<Adventure> findByDate(String date);
  date.substring(7,10);
}

an example of my SQL data looks like so

INSERT INTO ADVENTURES (ID, DATE, COUNTRY, CITY, STATE, NUM_PHOTOS, BLOG_COMPLETED) VALUES (7, '05/16/2017', 'Uruguay', 'Salto', '', 43, false);
INSERT INTO ADVENTURES (ID, DATE, COUNTRY, CITY, STATE, NUM_PHOTOS, BLOG_COMPLETED) VALUES (8, '09/08/2016', 'Greece', 'Ãno Liósia', '', 94, true);
INSERT INTO ADVENTURES (ID, DATE, COUNTRY, CITY, STATE, NUM_PHOTOS, BLOG_COMPLETED) VALUES (9, '12/10/2020', 'Portugal', 'Azenhas do Mar', 'Lisboa', 5, false);
INSERT INTO ADVENTURES (ID, DATE, COUNTRY, CITY, STATE, NUM_PHOTOS, BLOG_COMPLETED) VALUES (11, '07/03/2020', 'China', 'Chishui', '', 9, true);
INSERT INTO ADVENTURES (ID, DATE, COUNTRY, CITY, STATE, NUM_PHOTOS, BLOG_COMPLETED) VALUES (10, '06/22/2019', 'Albania', 'Helmas', '', 7, true);
INSERT INTO ADVENTURES (ID, DATE, COUNTRY, CITY, STATE, NUM_PHOTOS, BLOG_COMPLETED) VALUES (13, '12/06/2016', 'France', 'Alençon', 'Basse-Normandie', 44, false);
INSERT INTO ADVENTURES (ID, DATE, COUNTRY, CITY, STATE, NUM_PHOTOS, BLOG_COMPLETED) VALUES (12, '01/06/2020', 'France', 'Quimper', 'Bretagne', 77, true);
INSERT INTO ADVENTURES (ID, DATE, COUNTRY, CITY, STATE, NUM_PHOTOS, BLOG_COMPLETED) VALUES (14, '04/01/2018', 'Japan', 'Ushiku', '', 13, true);
INSERT INTO ADVENTURES (ID, DATE, COUNTRY, CITY, STATE, NUM_PHOTOS, BLOG_COMPLETED) VALUES (15, '04/28/2021', 'Mexico', 'Fovissste', 'Chihuahua', 26, true);
INSERT INTO ADVENTURES (ID, DATE, COUNTRY, CITY, STATE, NUM_PHOTOS, BLOG_COMPLETED) VALUES (16, '09/21/2017', 'China', 'Xiaojin', '', 7, false);
INSERT INTO ADVENTURES (ID, DATE, COUNTRY, CITY, STATE, NUM_PHOTOS, BLOG_COMPLETED) VALUES (17, '08/31/2019', 'Syria', 'KhÄn ShaykhÅ«n', '', 4, false);
INSERT INTO ADVENTURES (ID, DATE, COUNTRY, CITY, STATE, NUM_PHOTOS, BLOG_COMPLETED) VALUES (18, '12/25/2020', 'Argentina', 'Zapala', '', 54, false);
INSERT INTO ADVENTURES (ID, DATE, COUNTRY, CITY, STATE, NUM_PHOTOS, BLOG_COMPLETED) VALUES (19, '04/25/2016', 'Philippines', 'Unidad', '', 72, false);


Solution 1:[1]

Your date in db is a date, ot at least a string formatted like a proper date, depending on how you declared the type, which is not clear from your question. With your get request - /traveladventures/bydate/2017, you are sending something that looks like a year. Considering dates in your db looks like this - 04/27/2019, 2017 definitely won't match any of them.

Edit: The query generated from this method List<Adventure> findByDate(String date); will look for rows, whose date column is exact match of provided string. In your specific case it will return only rows, whose date is exact match for 2017. The problem should be obvius now. In order to find anything, path variable should be in correct format - <month>/<day>/<year>, for example - 05/16/2017. This will make correct url - http://localhost:4001/traveladventures/bydate/05/16/2017, which is another problem - you have url separatator as part of your path variable, you will probably get 404 response.

Solutions:

  1. Use request parameter:
@GetMapping("/bydate/{date}")
public List<Adventure> getByDate(@PathVariable("date") String date){
  return this.adventureRepository.findByDate(date);
}

Url will look like this - http://localhost:8080/traveladventures/bydate?date=05/16/2017. Downside is you can only search for exact matches.

  1. Change your entity: make date field of some date type. I would to stay away from java.util.Date and use java.time.LocalDate.
@Column(name = "DATE")
private LocalDate date;

Then change your date column in table to be of datatype DATE. Like this you can do some more complex queries - for example get adventures in year 2017.

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1