Rest Web Service返回404错误

时间:2016-02-10 22:32:32

标签: java rest web service jersey

我正在尝试学习简单的restful Web服务。所以,我查了一个例子并一步一步地跟着它,但在尝试部署项目时我遇到了错误404。知道为什么吗?

文件:web.xml

 <?xml version="1.0" encoding="UTF-8"?>
    <web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" 
        xmlns="http://java.sun.com/xml/ns/javaee" 
        xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_3_0.xsd" 
        id="WebApp_ID" version="3.0">
      <display-name>UserManagement</display-name>
      <servlet>
          <servlet-name>Jersey RESTful Application</servlet-name>
          <servlet-class>org.glassfish.jersey.servlet.ServletContainer</servlet-class>
             <init-param>
                <param-name>jersey.config.server.provider.packages</param-name>
                <param-value>com.tutorialspoint</param-value>
             </init-param>
          </servlet>
       <servlet-mapping>
       <servlet-name>Jersey RESTful Application</servlet-name>
          <url-pattern>/rest/*</url-pattern>
       </servlet-mapping>  
    </web-app>

文件:User.java:

    package com.tutorialspoint;

    import java.io.Serializable;

    import javax.xml.bind.annotation.XmlElement;
    import javax.xml.bind.annotation.XmlRootElement;

    @XmlRootElement(name = "user")
    public class User implements Serializable {

        private static final long serialVersionUID = 1L;
        private int id;
        private String name;
        private String profession;

        public User(){}

        public User (int id, String name, String profession){
            this.id = id;
            this.name = name;
            this.profession = profession;
        }

        public int getID(){
            return id;
        }

        @XmlElement
        public void setId (int id){
            this.id = id;
        }

        public String getName(){
            return name;
        }

        @XmlElement
        public void setName(String name){
            this.name = name;
        }

        public String getProfession(){
            return profession;
        }

        @XmlElement
        public void setProfession(String profession){
            this.profession = profession;
        }
    }

文件:UserDao.java

package com.tutorialspoint;

import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.ObjectInputStream;
import java.io.ObjectOutputStream;
import java.util.ArrayList;
import java.util.List;

public class UserDao {

    @SuppressWarnings("unchecked")
    public List<User> getAllUsers(){
        List<User> userList = null;
        try {
            File file = new File("Users.dat");
            if (!file.exists()) {
                User user = new User(1, "Mahesh", "Developer");
                userList = new ArrayList<User>();
                userList.add(user);
                saveUserList(userList);
            }
            else {
                FileInputStream fis = new FileInputStream(file);
                ObjectInputStream ois = new ObjectInputStream(fis);
                userList = (List<User>) ois.readObject();
                ois.close();
            }
        } catch (IOException e) {
            e.printStackTrace();
        } catch (ClassNotFoundException e){
            e.printStackTrace();
        }
        return userList;
    }

    private void saveUserList(List<User> userList) {
        // TODO Auto-generated method stub
        try {
            File file = new File ("User.dat");

            FileOutputStream fos = new FileOutputStream (file);
            ObjectOutputStream oos = new ObjectOutputStream (fos);
            oos.writeObject(userList);
            oos.close();
        } catch (FileNotFoundException e) {
            e.printStackTrace();
        } catch (IOException e) {
            e.printStackTrace();
        }
    }
}

文件:UserService.java

package com.tutorialspoint;

import java.util.List;

import javax.ws.rs.GET;
import javax.ws.rs.Path;
import javax.ws.rs.Produces;
import javax.ws.rs.core.MediaType;

@Path("/UserService")
public class UserService {

    UserDao userDao = new UserDao();

    @GET
    @Path("/users")
    @Produces(MediaType.APPLICATION_XML)
    public List<User> getUsers() {
        return userDao.getAllUsers();
    }
}

这是我要回来的 Error 404 screenshot

2 个答案:

答案 0 :(得分:1)

除了甚至没有部署您的应用程序的可能性(也是404):

您配置休息服务以回答的路径是

<Your application context>/rest/UserService/users

<Your application context>似乎是/UserManagement

  • /rest已在jersey servlet映射<{li>中的web.xml中配置 {li> /UserService已在UserService.java注释
  • @Path文件中配置
  • /users已在UserService.java文件中的方法@Path注释中配置。这应该是@Path("users")

您的屏幕截图显示您正在调用/UserManagement,这可能是您的应用程序的上下文,但不是您服务的路径。

答案 1 :(得分:-1)

您可以更改web.xml文件。 webxml文件存在一些问题。使用此web.xml文件使用以下代码。

<?xml version="1.0" encoding="UTF-8"?>
 <web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns="http://java.sun.com/xml/ns/javaee" xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_3_0.xsd" id="WebApp_ID" version="3.0">
  <display-name>User Management</display-name>
  <servlet>
  <servlet-name>Jersey RESTful Application</servlet-name>
  <servlet-class>org.glassfish.jersey.servlet.ServletContainer</servlet-class>
     <init-param>
        <param-name>jersey.config.server.provider.packages</param-name>
        <param-value>com.tutorialspoint</param-value>
       </init-param>
   </servlet>
  <servlet-mapping>
 <servlet-name>Jersey RESTful Application</servlet-name>
  <url-pattern>/rest/*</url-pattern>
</servlet-mapping>  
</web-app>