2017-08-16 102 views
1

我創建了一個簡單的UserManagement寧靜Web服務。其中我創建了3個課程。源代碼如下。訪問寧靜的Web服務時出現404錯誤

用戶類

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的類

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 { 
     public List<User> getAllUsers(){ 

       List<User> userList = null; 
       try { 
       File file = new File("Users.dat"); 
       if (!file.exists()) { 
        User user = new User(1, "Mahesh", "Teacher"); 
        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){ 
       try { 
       File file = new File("Users.dat"); 
       FileOutputStream fos; 
       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類

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(); 
     } 

} 

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

然後,我已經創造了戰爭和部署在Tomcat和啓動Tomcat 但是,當我用郵差擊中網址http://localhost:8080/UserManagement/rest/UserService/users

訪問此問題的REST Web服務它給了404錯誤。

請有人幫我解決這個問題。

+1

你的上下文路徑是什麼? – sunkuet02

+0

@ sunkuet02現在加入 –

+0

嘗試點擊:http:// localhost:8080/rest/UserService/users – sunkuet02

回答

1

嘗試使用com.sun.jersey.spi.container.servlet.ServletContainer更改servlet類。