【问题标题】:Springboot - empty get rest responseSpringboot - 空获取休息响应
【发布时间】:2018-12-08 11:20:12
【问题描述】:

我正在从 MySQL 数据库构建一个简单的 get rest 调用,问题是它返回一个空对象。

电话本身是通过电子邮件接收的(我知道这不是最好的方法),这是我的代码:

实体:

@Entity
public class User implements Serializable {

    @Id
    @GeneratedValue(strategy = GenerationType.AUTO)
    @Column(name = "user_id")
    private int id;
    private String email;
    private String password;
    private String firstName;
    private String userName;
    private String lastName;
    private boolean active;
    @Temporal(TemporalType.DATE)
    private Date createDate;
    @Temporal(TemporalType.DATE)
    private Date updateDate;
    @ManyToMany(cascade = CascadeType.ALL)
    @JoinTable(name = "user_role", joinColumns = @JoinColumn(name = "user_id"), inverseJoinColumns = @JoinColumn(name = "role_id"))
    private Collection<Role> roles;

// constructor
// get and setter

}

存储库:

public interface UserRepository extends JpaRepository<User, Long> {
//    User findById (Integer Id);

    @Query("SELECT u.id FROM User u where u.id = :id") 
    User findById(@Param("id") Integer id);

    User findByEmail (String email);
}

服务:

@Service("userService")
public class UserService {

    private String status, message;
    private final HashMap map = new HashMap();

    @Autowired
    private UserRepository userRepository;
//    @Autowired
//    private RoleRepository roleRepository;

    public User findByUserEmail (String email) {
        return userRepository.findByEmail(email);
    }

}

控制器:

@RestController("userControllerService")
@RequestMapping("/user/account")
public class UserController {

    @Autowired
    private UserService userService;

    @GetMapping("/test-get/{email}")
    public User jj(@PathVariable("email") String email){
        return userService.findByUserEmail(email);
    }
}

而我的数据库恰好有以下数据:

这是我点击 URL 后得到的响应

我不知道为什么我的回复是空的!

【问题讨论】:

  • 请更新问题,以便可以看到邮递员中的请求网址
  • 我对您在发布新问题(包括这个问题)时所做的大量编辑工作投了反对票。引用块用于引用,即其他人在其他地方说或写的东西。它们不适用于您自己声音的材料,也不适用于代码/日志/图像的介绍性标题。
  • 看起来至少有两个用户以前修复过这种格式。请不要以这种方式使用引号块,除非您的摘录确实是引号。

标签: json spring-boot get restful-architecture


【解决方案1】:

您不能在 URI 路径中包含 @。使用 %40 对其进行编码。

参考:Can I use an at symbol (@) inside URLs?

另外,正确的方法是用作查询参数,因为这更像是一个很好的标识符,并允许 @ 解析为字符串

    @GetMapping("/test-get")
    public User jj(@RequestParam("email") String email){
        return userService.findByUserEmail(email);
    }

无论哪种方式,作为编码的 url 命中 /test-get/email=a@b.com ?或 /test-get/a%40b.com 获取您之前的代码。

【讨论】:

  • 为哪一个。您可以尝试将其作为请求参数吗?它就像我刚刚测试过的那样工作
猜你喜欢
  • 1970-01-01
  • 2013-12-04
  • 1970-01-01
  • 2023-03-07
  • 2018-09-28
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多