@ResponseBody returns empty object

拈花ヽ惹草 提交于 2021-01-05 00:58:45

问题


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.


回答1:


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.




回答2:


Since you are declaring the method findByUseruuid() in your AppUserRepository interface then you also need to provide the query on which the data will be returned from db.

For example:

@Repository
public interface AppUserRepository extends JpaRepository<AppUser, Long> {

@Query("select vm FROM table_name vm where vm.useruuid = :useruuid")
AppUser findByUseruuid(String useruuid); 

where AppUser is entity class with proper annotation used.

It seems that you need to fix your controller ,You are missing @QueryParam or @PathParam at your function findOneByUUID ....need to add as mentioned below

 @GetMapping("/findOnebyId")
    @ResponseBody
    public AppUser findOneByUUID(@QueryParam("useruuid") String useruuid)


来源:https://stackoverflow.com/questions/62688472/responsebody-returns-empty-object

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!