Im trying to use Vaadin as SPA i made this search function but it doesn't work ! can anyone helps me?

i’m trying to make search textbox that searches for User-Name … but i can’t find todo that … i tried this code:
SearchFilterClass


public class SearchFilter {
    String filterValue;
    public String getFilterValue() {
        return filterValue;
    }
}

Vaadin frontend:

public User[] findUsers(String filterValue) {
        if(filterValue.isEmpty()){
            return null;
        }else if(filterValue.length()<3){
            return null;
        }else {
            return webClient
                    .post()
                    .uri("/users/findusers")
                    .body(Mono.just(filterValue), SearchFilter.class)
                    .retrieve()
                    .bodyToMono(User[].class)
                   // .toEntity(User[].class);
                    .block();
        }

Backend is working fine in Postman:

    @PostMapping("/findusers")
    public List<User> findUsers(RequestBody SearchFilter filterValue){
        //String FilterValue = SerializationUtils.serialize(filterValue).toString();
        String filterRequest = filterValue.getFilterValue();
        return userService.findUsers(filterRequest);
    }

Did you check this “Calling REST from Java with Spring WebClient” video yet? https://youtu.be/-U_dDUAw_OM?si=i4MFN_7zOfWU7O0F

yes i check it , it has only HttpGet from back-end , I wish that nice tutorial has a full crud ops. can you help me with that , please ?

Could you copy, and/or show your userService as well, please?
Are you encountering any errors, or are you simply not receiving any values based on your filterText?

If you’re unable to make progress, it would be best if you could put your code on GitHub or a similar platform. Then, you can share the URL here, and we could take a look and suggest some solutions.

Cheers,
Peter

Hi @muslehaj,

Calling other Http methods with Spring Web Client should be pretty straightforward. There’s a good amount of documentation on that regard. I like these 2:

1 Like

MuslehAbulHaj/Addaliil-frontend (github.com)

MuslehAbulHaj/Addaliil-backend (github.com)

thanks a lot for your kind reply , Boss, those links are very useful :+1::ok_hand:, now it’s sending the request as i wanted to, but i had to change the back end , now the problem in back end :joy:

1 Like

I wish someone can help me to find a course show the beauty of making a full stack API And SPA application !? I found Vaadin isn’t famous in Udemy and Youtube channel is good , but none of them is like a mentioned earlier.

the problem is in the Back-end , it’s http post method as Below:

Controller

 @ PostMapping("/findusers") 
 public List<User> findUsers(@ RequestBody String filterValue){
     return userService.findUsers(filterValue);
 }

User-Service interface

public interface UserService {
    List<User> findUsers(String substring);
}

User-Service Implementation

public List<User> findUsers(String substring) {
    return userRepository.findUsers(substring);
}

User-Repository

@ Repository
public interface UserRepository extends JpaRepository<User,UUID> {
    @ Query("select u from User u where lower(u.name) like lower(concat('%',:substring,'%'))")
    List<User> findUsers(String substring);
}

the Fornt-End code that calls the above method by Json format is like this:

public User[ ,] findUsers(String filterValue) {
    if(filterValue.isEmpty()){
        return null;
    }else if(filterValue.length()<3){
        return null;
    }else {
        return webClient
                .post()
                .uri("/users/findusers")
                .contentType(MediaType.APPLICATION_JSON) // Set Content-Type header
                .body(BodyInserters.fromValue(filterValue)) // Convert JsonObject to String
                .retrieve()
                .bodyToMono(User[,].class)
                .block();
    }

None of your problems are even remotely related to Vaadin - remove Vaadin from your equation / search and fix your problem.

Afterwards just use a Grid to display those Users; for this the documention of the page is the perfect start.

1 Like

You are right , I’m totally new to Java world and Vaadin as well … the problem isn’t in Vaadin , it’s spring-boot stuff… btw, I can’t find delete button to delete this post

I got back only now, how is it going? Do you still need some help? Also, I agree with Knoobie this is not related to Vaadin :), but I can probably check your code in short if you still have some problems.

These are not reachable right now (probably you did not gave permission to make them public, so I cannot check them :(.

Thanks friend I found the problem was in the Webclient in Front-end and the way to send the request to back-end and i resolve it.

1 Like

Glad to hear it @muslehaj! Thanks for the heads-up!
Feel free to post in the forum next time you encounter something :)!