Search code examples
javaspringjpacrud-repository

Getting 500 error when trying to query my db with JPA


Here is the error I get:

Parameter value [\] did not match expected type [java.lang.String (n/a)]; nested exception is java.lang.IllegalArgumentException: Parameter value [\] did not match expected type [java.lang.String (n/a)]

I have a basic site that allows you to create a "song" that contains a title, artist and rating. You can edit/delete the songs and the app will display all the songs on the main page. It has a function to order the songs by their rating in descending order which works and a feature to search which only half works.

I don't understand what's wrong because it works when I use the feature to search the first time, then on the second search it always throws this error.

Digging a little deeper I think these error lines are relevant:

    at jdk.proxy3/jdk.proxy3.$Proxy112.findByArtistContaining(Unknown Source)
    at com.cshannon.lookify.services.SongService.findByArtist(SongService.java:50)
    at com.cshannon.lookify.controllers.MainController.search(MainController.java:105)

I can't understand why it thinks the string I am entering is an unkown source when it works the first time I do it without errors. I have tried changing the route from a Get to a Post and that doesn't make a difference.

The controller code:

@GetMapping("/search")
    public String search(
            Model model,
            @RequestParam(value="search") String search
            ) {
        
        if (search.isEmpty()) {
            return "redirect:/dashboard";
        }
        
        ArrayList<Song> songs = ss.findByArtist(search);
        model.addAttribute("songs", songs);
        
        return "search.jsp";
        
    }

The service:

// Find by artist
    public ArrayList<Song> findByArtist(String search) {
        
        return (ArrayList<Song>) sr.findByArtistContaining(search);
        
    }

The repository:

ArrayList<Song> findByArtistContaining(String search);

And if it helps here is the form I am using to get the string from the user:

<form action="/search" class="d-flex">
    <input type="search" name="search" class="form-control me-2" placeholder="Search" aria-label="Search"/>
    <input type="submit" value="Search Artists" class="btn btn-outline-success"/>
</form>

Solution

  • If you want to fix for both security issue CVE-2022-22968 and this bug, you can use spring-boot version 2.5.13