Skip to content
Advertisement

@ResponseBody returns empty object

When I use below to get the user object it works just fine.

@GetMapping("/findOne") 
@ResponseBody
public Optional<AppUser> findOne (Long id) {
    return appUserRepository.findById(id);
}

Above gives me a response back as:

{"id":1,"useruuid":"863db606-9af6-48a8-963a-07b9fe0fc4fc","useremail":"user1@mydomain.com"}

Now, I am trying to search based on UUID(4) using this:

@GetMapping("/findOneUsingUUID") 
@ResponseBody
public AppUser findOne (String useruuid) {
    return appUserRepository.findByUseruuid(useruuid);
}

This doesn’t return anything. No error, no warning whatsoever. While this gives me all the details I need:

AppUser appUser = appUserRepository.findByUseruuid("863db606-9af6-48a8-963a-07b9fe0fc4fc");
    System.out.println(" >>>      " + appUser.getUseruuid());
    System.out.println(" >>>      " + appUser.getId());
    System.out.println(" >>>      " + appUser.getUseremail());

This is what I have in my Repository

@Repository
public interface AppUserRepository extends JpaRepository<AppUser, Long> {
AppUser findByUseruuid(String useruuid);

and Entity

@NotEmpty
@Column(name = "useruuid")
@GenericGenerator(name = "uuid", strategy = "uuid4")
private String useruuid;

Question: Why my controller method @GetMapping("/findOneUsingUUID") is not returning anything? Any relevant info/help/link will be greatly appreciated.

Update (for sidgate’s comment/question): I tested this inside another controller method. Like this:

@RequestMapping(value = { "tableusers" }, method = RequestMethod.GET)
    public String listUsers(ModelMap model) {
       
        AppUser appUser = appUserRepository.findByUseruuid("863db606-9af6-48a8-963a-07b9fe0fc4fc");
        System.out.println(" >>>      " + appUser.getUseruuid());
        System.out.println(" >>>      " + appUser.getId());
        System.out.println(" >>>      " + appUser.getUseremail());
        
        String tableusers = "tableusers";
        model.addAttribute("tableusers", tableusers);

        List<AppUser> users = appUserService.findAllUsers();
        model.addAttribute("users", users);
        model.addAttribute("metaTitle", "All Users");
        return "user_table_data_table";
        
    }

Update 2:

Now I have the following in my Controller:

@RequestMapping(value = "/findOnebyId/{uuid}", method = RequestMethod.GET)
    @ResponseBody
    public AppUser findOneByUUID2(@PathVariable final String useruuid) {
        return appUserRepository.findByUseruuid(useruuid);
    }

So my URL becomes similar to this:

http://localhost:8080/findOnebyId?id=eeadf17f-13f2-4939-bab6-2534fcc1f4d4

But now getting 404 error in my browser console.

Update 3:

Per Dale’s suggestion, I fixed it and checked in code to github-repo. All works fine now.

Advertisement

Answer

I tried this out based on you GitHub code and the API worked flawlessly for me via Postman. The problem might be with the way you are invoking the API.

In the comments section you mentioned that your are passing values to controllers as path variables. But you haven’t added the @PathVariable annotation in the controller method. So the controller method is expecting a query paramter, not a path variable. Please try invoking the API by passing UUID as parameter.

EDIT

Looking at your GitHub code, you are calling the API as url: "/findOnebyId?id="+ id.

@GetMapping("/findOnebyId")
    @ResponseBody
    public AppUser findOneByUUID(String useruuid) {
        return appUserRepository.findByUseruuid(useruuid);
    }

In this controller method, you are expecting useruuid as query parameter. But in your datatables.html file, you are sending the query parameter as id.

So you can remove the @PathVariable annotation and correct the query parameter name in the AJAX request.

User contributions licensed under: CC BY-SA
3 People found this is helpful
Advertisement