泽西 - 休息:404未找到错误

时间:2014-09-09 11:46:53

标签: java rest jersey

我正在做一个简单的程序,任务是从Html表单中获取(用户ID),并且应该显示与之关联的用户名。我是 加载index.html页面时出现404错误。

但是当我在网址上提供值(userid)时,它可以正常工作。

这是代码

<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<title>Index page</title>
</head>
<body>
    <form action="http://localhost:8080/rest/my/first/rest/users" method="GET">
        <label for="uid">ID</label>
        <input name="uid" />
                <input type="submit" value="Submit" />
    </form>
</body>
</html>  

这是资源类

@Path("users") //attach client request to resource: .../users
public class RestResource {

    Map<String,User> listUsers;  

    //initialize some resources
    public RestResource(){
        listUsers = new HashMap();
        listUsers.put("1",new User("1", "John"));
        listUsers.put("2",new User("2", "Peter"));
    }

    //return list of users
    @GET
    @Produces(MediaType.TEXT_PLAIN)
    public String listOfUsersInText(){
        String list="";
        for (Entry<String,User> u:listUsers.entrySet()){
            list += u.getValue().getName() + "\n";
        }
        return list;
    }

    //return user information corresponding to the requested uid.
    @GET
    @Path("{uid}") //attach client request to resource: .../users/<uid>
    @Produces(MediaType.TEXT_PLAIN)
    public String getUID(@PathParam("uid") String uid){

        if (!listUsers.containsKey(uid))
            return "User not exist!";

        return listUsers.get(uid).getID()+":"+listUsers.get(uid).getName();
    }   
}

WEB.XML

 <display-name>rest</display-name>

  <servlet>
    <servlet-name>Jersey REST Service</servlet-name>
    <servlet-class>com.sun.jersey.spi.container.servlet.ServletContainer</servlet-class>
    <init-param>
      <param-name>com.sun.jersey.config.property.packages</param-name>
      <param-value>my.first.rest</param-value>
    </init-param>
    <load-on-startup>1</load-on-startup>
  </servlet>

  <servlet-mapping>
    <servlet-name>Jersey REST Service</servlet-name>
    <url-pattern>/*</url-pattern>
  </servlet-mapping>

1 个答案:

答案 0 :(得分:0)

您遵循的方法是错误的我们有几个休息客户端api您可以使用其中任何一个并且可以将请求发送到休息资源

您可以在jsp页面中显示的响应数据。