@ResponseBody 返回空对象 [英] @ResponseBody returns empty object

查看:117
本文介绍了@ResponseBody 返回空对象的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

当我使用下面来获取用户对象时,它工作得很好.

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);
}

上面给了我一个回复:

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

现在,我正在尝试使用此基于 UUID(4) 进行搜索:

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);

和实体

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

问题:为什么我的控制器方法 @GetMapping("/findOneUsingUUID") 没有返回任何东西?任何相关信息/帮助/链接将不胜感激.

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

更新(针对 sidgate 的评论/问题):我在另一个控制器方法中对此进行了测试.像这样:

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";
        
    }

更新 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

但现在我的浏览器控制台出现 404 错误.

But now getting 404 error in my browser console.

更新 3:

根据 Dale 的建议,我修复了它并将代码签入到 github-repo.现在一切正常.

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

推荐答案

我根据你的 GitHub 代码尝试了这个,并且 API 通过 Postman 完美地为我工作.问题可能在于您调用 API 的方式.

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.

在评论部分您提到您将值作为路径变量传递给控制器​​.但是您还没有在控制器方法中添加 @PathVariable 注释.所以控制器方法需要一个查询参数,而不是一个路径变量.请尝试通过传递 UUID 作为参数来调用 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.

编辑

查看您的 GitHub 代码,您将 API 调用为 url: "/findOnebyId?id="+ id.

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);
    }

在这个控制器方法中,您期望 useruuid 作为查询参数.但是在您的 datatables.html 文件中,您将查询参数作为 id 发送.

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.

因此您可以删除 @PathVariable 注释并更正 AJAX 请求中的查询参数名称.

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

这篇关于@ResponseBody 返回空对象的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆