简体   繁体   English

泽西岛REST服务

[英]Jersey REST services

I'm new to Jersey and RESTful web services. 我是Jersey和RESTful Web服务的新手。 I have a simple service that gets a user ID, searches for associated activities, and returns a User POJO with two fields: userId and userName . 我有一个简单的服务,该服务获取用户ID,搜索关联的活动并返回带有两个字段的User POJO: userIduserName Activity is also a simple POJO that contains the fields duration and description , as well as a User reference. Activity也是一个简单的POJO,其中包含字段durationdescription以及User参考。

I use the annotation @XmlRootElement for both classes ( User and Activity ). 我对两个类( 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;
}

When i try to run the service, it prints the result in the console but displays an HTTP 500 error my browser. 当我尝试运行该服务时,它将在控制台中打印结果,但在浏览器中显示HTTP 500错误。 No error is written to the application log. 没有错误写入应用程序日志。

 <!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> 

Why are you using /user again in the Path annotation. 为什么在Path注释中再次使用/ user。 Can you please provide the complete class definition. 您能否提供完整的类定义。

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

If you have already defined /user as Path annotation for class, you just need to have userID as below for Path annotation of Get message. 如果您已经将/ user定义为类的Path注释,则只需为Get message的Path注释具有如下的userID。

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

}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM