Springboot-空获取休息响应

时间:2018-06-29 09:38:00

标签: json spring-boot get restful-architecture

我正在从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);
    }
}

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

enter image description here

这是我点击URL后收到的回复

enter image description here

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

1 个答案:

答案 0 :(得分:0)

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

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

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

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

无论哪种方式,都以编码后的网址作为 /test-get/email=a@b.com 命中?或 /test-get/a%40b.com 用于您以前的代码。