无法启动RESTful Web服务

时间:2017-10-11 05:08:27

标签: java rest web-services

我一直在学习RESTful网络服务,我正在关注this tutorial。此外,我已经实现了该教程中给出的示例代码,但是当我通过Postman运行它时,我得到Error Exception while loading assemblies: System.IO.FileNotFoundException: Could not load assembly 'SQLite-net, Version=1.0.0.0, Culture=neutral, PublicKeyToken='. Perhaps it doesn't exist in the Mono for Android profile? File name: 'SQLite-net.dll' at Java.Interop.Tools.Cecil.DirectoryAssemblyResolver.Resolve(AssemblyNameReference reference, ReaderParameters parameters) at Xamarin.Android.Tasks.ResolveAssemblies.AddAssemblyReferences(DirectoryAssemblyResolver resolver, ICollection1 assemblies, AssemblyDefinition assembly, Boolean topLevel) at Xamarin.Android.Tasks.ResolveAssemblies.AddAssemblyReferences(DirectoryAssemblyResolver resolver, ICollection1 assemblies, AssemblyDefinition assembly, Boolean topLevel) at Xamarin.Android.Tasks.ResolveAssemblies.Execute(DirectoryAssemblyResolver resolver) CouchPortfolioApp.Android 错误。 Postman中的我的请求网址为404 Not Foundhttp://localhost:8080/UserManagement/rest/UserService/users。因此,请告诉我的代码中可能存在哪些问题?

请求和回复:

enter image description here

项目目录:

User.java

GET

UserDao.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; 
   }   
} 

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

Web.xml中

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

3 个答案:

答案 0 :(得分:0)

你是如何运行你的申请的? 对于Eclipse,请按照以下步骤启动应用程序 右键单击您的项目 - &gt;运行方式 - &gt;在服务器上运行
希望这对你有帮助〜

答案 1 :(得分:0)

您确定您的根应用程序上下文是 / UserManagement 吗? 默认情况下,对于tomcat Web服务器 - 根应用程序上下文等于Web工件的名称。因此,请确保将UserManagement.war复制到&#39; tomcat_distribution_folder&#39; \ webapps

我已在本地检查 - 您的web.xml描述符已正确配置。 由于您收到HTTP 404错误 - 表示您的Web服务器已启动并运行。

您是否可以尝试使用空根上下文调用端点: http://localhost:8080/rest/UserService/users

或检查您的网络服务器的配置,并检查部署的应用程序的使用必要的根上下文

答案 2 :(得分:0)

当我尝试在邮递员中运行时,我遵循相同的教程并面对来自服务器的500响应。后来我在服务器上执行了运行,运行正常,但文件未发现异常,Users.dat文件的访问被拒绝。我将.dat文件替换为.txt,然后在服务器上执行了运行。它在控制台中没有任何错误,最后在邮递员中也可以使用。我建议您检查tomcat apache服务器的状态。