泽西岛 REST 服务

Jersey REST services

我是 Jersey 和 RESTful 网络服务的新手。我有一个简单的服务,它获取用户 ID、搜索关联的活动和 returns 一个 User POJO,其中包含两个字段:userIduserNameActivity 也是一个简单的 POJO,它包含字段 durationdescription,以及一个 User 引用。

我对 类(UserActivity)都使用注释 @XmlRootElement

@GET
@Path("/user/{userId}")
@Produces(MediaType.APPLICATION_JSON)
public User getActivityUser(@PathParam("userId") String userId) {
    for (Activity activity : activities) {
        if (activity.getUser().getId().equals(userId)) {
            System.out.println(activity.getUser().toString());
            return activity.getUser();
        }
    }
    return null;
}

当我尝试 运行 该服务时,它在控制台中打印结果,但在我的浏览器中显示 HTTP 500 错误。没有错误写入应用程序日志。

<!doctype html><html lang="en"><head><title>HTTP Status 500  Internal Server Error</title><style type="text/css">h1 {font-family:Tahoma,Arial,sans-serif;color:white;background-color:#525D76;font-size:22px;} h2 {font-family:Tahoma,Arial,sans-serif;color:white;background-color:#525D76;font-size:16px;} h3 {font-family:Tahoma,Arial,sans-serif;color:white;background-color:#525D76;font-size:14px;} body {font-family:Tahoma,Arial,sans-serif;color:black;background-color:white;} b {font-family:Tahoma,Arial,sans-serif;color:white;background-color:#525D76;} p {font-family:Tahoma,Arial,sans-serif;background:white;color:black;font-size:12px;} a {color:black;} a.name {color:black;} .line {height:1px;background-color:#525D76;border:none;}</style></head><body><h1>HTTP Status 500  Internal Server Error</h1><hr class="line" /><p><b>Type</b> Status Report</p><p><b>Message</b> Internal Server Error</p><p><b>Description</b> The server encountered an unexpected condition that prevented it from fulfilling the request.</p><hr class="line" /><h3>Apache Tomcat/8.5.23</h3></body></html>

为什么在 Path 注释中再次使用 /user。 您能否提供完整的 class 定义。

@Path("/user/{userId}")

如果您已经为class定义了/user作为Path注解,您只需要为Get message的Path注解设置如下userID即可。

@GET
@Path("{userId}")
@Produces(MediaType.APPLICATION_JSON)
public User getActivityUser(@PathParam("userId") String userId) {
  //Code goes here

}