Http状态404:源服务器没有找到目标资源的当前表示,或者不愿意透露存在该目标资源

时间:2017-08-07 07:32:51

标签: java rest web-services jersey

我是创建Web服务的新手,我正在关注TutorialsPoint.com编写RESTFUL服务。我按照它的例子,它工作。但是,我尝试创建自己的Web服务,它给了我错误

Http Status 404: The origin server did not find a current representation for the target resource or is not willing to disclose that one exists.

下面是我的文件

的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>ToDoListManagement</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.todolist</param-value> 
      </init-param> 
   </servlet> 
   <servlet-mapping> 
      <servlet-name>Jersey RESTful Application</servlet-name> 
      <url-pattern>/rest/*</url-pattern> 
   </servlet-mapping>
</web-app>

TaskService.java

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

    TaskDao taskDao = new TaskDao();
    private static final String SUCCESS_RESULT = "<result>success</result>";
    private static final String FAILURE_RESULT = "<result>failure</result>";

    @GET
    @Path("/tasks")
    @Produces(MediaType.APPLICATION_XML)
    public String getTasks() {
        return SUCCESS_RESULT;
    }

}

TaskDao.java

public class TaskDao {

public List<Task> getAllTasks() {
    List<Task> taskList = null;
    try {
        File file = new File("Tasks.dat");
        if (!file.exists()) {
            Task task = new Task(1, "Eat");
            taskList = new ArrayList<Task>();
            taskList.add(task);
            saveTaskList(taskList);
        } else {
            FileInputStream fis = new FileInputStream(file);
            ObjectInputStream ois = new ObjectInputStream(fis);
            taskList = (List<Task>) ois.readObject();
            ois.close();
        }
    } catch (IOException e) {
        // TODO Auto-generated catch block
        e.printStackTrace();
    } catch (ClassNotFoundException e) {
        // TODO Auto-generated catch block
        e.printStackTrace();
    }
    return taskList;
}

public Task getTask(int id) {
    List<Task> tasks = getAllTasks();

    for (Task task : tasks) {
        if (task.getId() == id) {
            return task;
        }
    }
    return null;
}

private void saveTaskList(List<Task> taskList) {
    File file = new File("Tasks.dat");
    FileOutputStream fos;
    try {
        fos = new FileOutputStream(file);
        ObjectOutputStream oos = new ObjectOutputStream(fos);
        oos.writeObject(taskList);
        oos.close();
    } catch (FileNotFoundException e) {
        // TODO Auto-generated catch block
        e.printStackTrace();
    } catch (IOException e) {
        // TODO Auto-generated catch block
        e.printStackTrace();
    }
}

}

Task.java

@XmlRootElement(name="task")
public class Task implements Serializable{
    private static final long serialVersionUID = 1L; 
    private int id;
    private String description;

    public Task(){}

    public Task(int id, String description) {
        this.id = id;
        this.description = description;
    }

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

    public String getDescription() {
        return description;
    }
    @XmlElement
    public void setDescription(String description) {
        this.description = description;
    }

       @Override
        public boolean equals(Object obj) {
            // TODO Auto-generated method stub
            if(obj == null){
                return false;
            }else if(!(obj instanceof Task)){
                return false;
            }else{
                Task task = (Task) obj;
                if(id == task.getId() 
                        && description.equals(task.getDescription())){
                    return true;
                }
            }
            return false;
        }

}

仅供参考,我不是在创建任何网页,而是想直接从浏览器或POSTMAN中搜索它。我创建了一个WAR文件并将其导出到Tomcat webapp目录。仍然不明白我在哪里弄错了。另外我也在使用jersey API。请帮忙

0 个答案:

没有答案